Skip to content

Commit

Permalink
dashboardを自動で更新
Browse files Browse the repository at this point in the history
  • Loading branch information
MurakawaTakuya committed Dec 29, 2024
1 parent a7c9dc6 commit 90315c2
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 10 deletions.
10 changes: 10 additions & 0 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ app.use(
const key = req.headers["x-forwarded-for"] || req.ip || "unknown";
return Array.isArray(key) ? key[0] : key;
},
handler: (req, res) => {
return res
.status(429)
.json({ message: "Too many requests, please try again later." });
},
})
);
// 1時間で最大300回に制限
Expand All @@ -81,6 +86,11 @@ app.use(
const key = req.headers["x-forwarded-for"] || req.ip || "unknown";
return Array.isArray(key) ? key[0] : key;
},
handler: (req, res) => {
return res
.status(429)
.json({ message: "Too many requests, please try again later." });
},
})
);

Expand Down
2 changes: 1 addition & 1 deletion functions/src/routers/resultRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ const processGoals = async (
router.get("/:userId?", async (req: Request, res: Response) => {
const userId = req.params.userId;

let limit = parseInt(req.query.limit as string) || 100;
let limit = parseInt(req.query.limit as string) || 10;
if (limit < 1 || limit > 100) {
limit = 100;
}
Expand Down
17 changes: 14 additions & 3 deletions src/Components/DashBoard/DashBoard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import { useEffect, useState } from "react";
import Progress from "../Progress/Progress";
import styles from "./DashBoard.module.scss";

// 投稿を取得してProgress
// eslint-disable-next-line @typescript-eslint/no-empty-function
let rerenderDashBoard: () => void = () => {};

export default function DashBoard({
userId = "",
success = true,
Expand All @@ -37,7 +39,8 @@ export default function DashBoard({
const [noResult, setNoResult] = useState<boolean>(false);
const [isLoading, setIsLoading] = useState<boolean>(true);

useEffect(() => {
const fetchData = () => {
setIsLoading(true);
fetchResult({ userId, success, failed, pending })
.then((data) => {
setSuccessResults(data.successResults);
Expand All @@ -54,10 +57,14 @@ export default function DashBoard({
type: "warning",
});
});
};

useEffect(() => {
rerenderDashBoard = fetchData;
fetchData();
}, [userId, success, failed, pending]);

useEffect(() => {
// 表示したい項目にデータがない場合はnoResultをtrueにする
setNoResult(
((success && successResults.length === 0) || !success) &&
((failed && failedResults.length === 0) || !failed) &&
Expand Down Expand Up @@ -92,3 +99,7 @@ export default function DashBoard({
</>
);
}

export function triggerDashBoardRerender() {
rerenderDashBoard();
}
2 changes: 2 additions & 0 deletions src/Components/DeleteGoalModal/DeleteGoalModal.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use client";
import { appCheckToken, functionsEndpoint } from "@/app/firebase";
import { triggerDashBoardRerender } from "@/Components/DashBoard/DashBoard"; // インポートパスを修正
import DeleteOutlineIcon from "@mui/icons-material/DeleteOutline";
import { DialogContent, DialogTitle, Modal, ModalDialog } from "@mui/joy";
import JoyButton from "@mui/joy/Button";
Expand Down Expand Up @@ -31,6 +32,7 @@ export default function DeleteGoalModal({ goalId }: { goalId: string }) {
message: "目標を削除しました",
type: "success",
});
triggerDashBoardRerender();
}
};

Expand Down
2 changes: 2 additions & 0 deletions src/Components/DeletePostModal/DeletePostModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import JoyButton from "@mui/joy/Button";
import Button from "@mui/material/Button";
import Stack from "@mui/material/Stack";
import { useState } from "react";
import { triggerDashBoardRerender } from "../DashBoard/DashBoard";
import { showSnackBar } from "../SnackBar/SnackBar";

export default function DeletePostModal({
Expand Down Expand Up @@ -37,6 +38,7 @@ export default function DeletePostModal({
message: "目標を削除しました",
type: "success",
});
triggerDashBoardRerender();
}
};

Expand Down
2 changes: 2 additions & 0 deletions src/Components/GoalModal/CreateGoalModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
Typography,
} from "@mui/joy";
import React, { useEffect, useState } from "react";
import { triggerDashBoardRerender } from "../DashBoard/DashBoard";

export default function CreateGoalModal({
open,
Expand Down Expand Up @@ -81,6 +82,7 @@ export default function CreateGoalModal({
message: "目標を作成しました",
type: "success",
});
triggerDashBoardRerender();

setText("");
setDueDate("");
Expand Down
4 changes: 3 additions & 1 deletion src/Components/NameUpdate/NameUpdate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ export default function NameUpdate() {
);

if (!response.ok) {
throw new Error("Network response was not ok");
const status = response.status;
const data = await response.json();
throw new Error(`Error ${status}: ${data.message}`);
}

// Firebase AuthenticationのdisplayNameを更新
Expand Down
4 changes: 3 additions & 1 deletion src/utils/API/Goal/createGoal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export const createGoal = async (postData: Goal) => {
});

if (!response.ok) {
throw new Error(`Network response was not ok: ${response.statusText}`);
const status = response.status;
const data = await response.json();
throw new Error(`Error ${status}: ${data.message}`);
}

return await response.json();
Expand Down
4 changes: 3 additions & 1 deletion src/utils/API/Post/createPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export const createPost = async (postData: PostWithGoalId) => {
});

if (!response.ok) {
throw new Error(`Network response was not ok: ${response.statusText}`);
const status = response.status;
const data = await response.json();
throw new Error(`Error ${status}: ${data.message}`);
}

return await response.json();
Expand Down
5 changes: 4 additions & 1 deletion src/utils/API/Result/fetchResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ export const fetchResult = async ({
);

if (!response.ok) {
throw new Error("Network response was not ok");
const status = response.status;
const data = await response.json();
throw new Error(`Error ${status}: ${data.message}`);
}

const data = await response.json();
return data;
};
Expand Down
5 changes: 4 additions & 1 deletion src/utils/API/User/createUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ export const createUser = async (name: string, userId: string) => {
});

if (!response.ok) {
throw new Error("Network response was not ok");
const status = response.status;
const data = await response.json();
throw new Error(`Error ${status}: ${data.message}`);
}

const data = await response.json();
console.log("Success:", data);
};
8 changes: 7 additions & 1 deletion src/utils/API/User/fetchUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ export const fetchUserById = async (userId: string): Promise<User> => {
});

if (!response.ok) {
throw new Error("Network response was not ok");
const status = response.status;
const data = await response.json();
throw new Error(`Error ${status}: ${data.message}`);
}

const data = await response.json();
return data;
};
Expand All @@ -40,6 +43,9 @@ export const handleFetchUserError = (error: unknown) => {
if (error.message.includes("500")) {
snackBarMessage = "サーバーエラーが発生しました";
}
if (error.message.includes("429")) {
snackBarMessage = "リクエストが多すぎます。数分後に再度お試しください";
}
} else {
console.error("An unknown error occurred");
snackBarMessage = "不明なエラーが発生しました";
Expand Down

0 comments on commit 90315c2

Please sign in to comment.