Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swrを利用してfetchする #83

Merged
merged 3 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"react-dom": "^18",
"react-icons": "^5.3.0",
"sonner": "^1.7.0",
"swr": "^2.2.5",
"tailwind-merge": "^2.5.4",
"tailwindcss-animate": "^1.0.7"
},
Expand Down
31 changes: 10 additions & 21 deletions app/src/components/view/ranking/ImageList.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Button } from "@/components/ui/button";
import { Card } from "@/components/ui/card";
import { fetcher } from "@/functions/fetcher";
import type { ScoreDetail } from "@/types";
import { useEffect, useState } from "react";
import { useState } from "react";
import { VscListOrdered } from "react-icons/vsc";
import useSWR from "swr";
import { LoadingSpinner } from "../LoadingSpinner";

type ImageListProps = {
Expand All @@ -15,31 +16,19 @@ const MODE = {
} as const;

export const ImageList = ({ setMode }: ImageListProps) => {
const [isLoading, setIsLoading] = useState<boolean>(true);
const { data, error, isLoading } = useSWR("/api/score/imagelist", fetcher);

const [scoreDetails, setScoreDetails] = useState<ScoreDetail[]>([]);
useEffect(() => {
const fetchData = () => {
setIsLoading(true);
return fetch("/api/score/imagelist")
.then((response) => {
if (!response.ok) {
throw new Error("Failed to fetch data");
}
return response.json();
})
.then((result) => setScoreDetails(result))
.catch((error) => console.error("Error fetching data:", error))
.finally(() => setIsLoading(false));
};

fetchData();
}, []);
// アサーションすみません
const scoreDetails = data as ScoreDetail[];

if (isLoading) {
return <LoadingSpinner />;
}

if (error) {
console.error("Failed to fetch data");
}

return (
<div className="p-4">
<div className="flex justify-end mb-4">
Expand Down
29 changes: 7 additions & 22 deletions app/src/components/view/ranking/RankingListAll.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,15 @@
import { Card } from "@/components/ui/card";
import { fetcher } from "@/functions/fetcher";
import type { RankingScores } from "@/types";
import { useEffect, useState } from "react";
import useSWR from "swr";
import { LoadingSpinner } from "../LoadingSpinner";

export default function RankingListAll() {
const [data, setData] = useState<RankingScores[]>([]);
const [isLoading, setIsLoading] = useState<boolean>(true);

useEffect(() => {
const fetchData = () => {
setIsLoading(true);
return fetch("/api/score/all")
.then((response) => {
if (!response.ok) {
throw new Error("Failed to fetch data");
}
return response.json();
})
.then((result) => setData(result))
.catch((error) => console.error("Error fetching data:", error))
.finally(() => setIsLoading(false));
};

fetchData();
}, []);
export default function RankingListWeekly() {
const { data, error, isLoading } = useSWR("/api/score/all", fetcher);

if (error) {
console.error("Failed to fetch data");
}
if (isLoading) {
return <LoadingSpinner />;
}
Expand Down
26 changes: 6 additions & 20 deletions app/src/components/view/ranking/RankingListWeekly.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
import { Card } from "@/components/ui/card";
import { fetcher } from "@/functions/fetcher";
import type { RankingScores } from "@/types";
import { useEffect, useState } from "react";
import useSWR from "swr";
import { LoadingSpinner } from "../LoadingSpinner";

export default function RankingListWeekly() {
const [data, setData] = useState<RankingScores[]>([]);
const [isLoading, setIsLoading] = useState<boolean>(true);
const { data, error, isLoading } = useSWR("/api/score/week", fetcher);

useEffect(() => {
const fetchData = () => {
setIsLoading(true);
return fetch("/api/score/week")
.then((response) => {
if (!response.ok) {
throw new Error("Failed to fetch data");
}
return response.json();
})
.then((result) => setData(result))
.catch((error) => console.error("Error fetching data:", error))
.finally(() => setIsLoading(false));
};

fetchData();
}, []);
if (error) {
console.error("Failed to fetch data");
}

const getEmoji = (rank: number) => {
switch (rank) {
Expand Down
2 changes: 2 additions & 0 deletions app/src/functions/fetcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const fetcher = (...args: [RequestInfo, RequestInit?]) =>
fetch(...args).then((res) => res.json());
Loading