Skip to content

Commit

Permalink
swrを利用
Browse files Browse the repository at this point in the history
  • Loading branch information
Kubosaka committed Nov 16, 2024
1 parent 81a921c commit d1ef636
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 62 deletions.
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
30 changes: 9 additions & 21 deletions app/src/components/view/ranking/ImageList.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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 { VscListOrdered } from "react-icons/vsc";
import useSWR from "swr";
import { LoadingSpinner } from "../LoadingSpinner";

type ImageListProps = {
Expand All @@ -15,31 +15,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>
<div className="flex justify-end">
Expand Down
27 changes: 6 additions & 21 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 RankingListWeekly() {
const [data, setData] = useState<RankingScores[]>([]);
const [isLoading, setIsLoading] = useState<boolean>(true);

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();
}, []);
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());

0 comments on commit d1ef636

Please sign in to comment.