Skip to content

Commit

Permalink
フロントエンドを新しいAPIに合わせた
Browse files Browse the repository at this point in the history
  • Loading branch information
MurakawaTakuya committed Dec 28, 2024
1 parent 1e80a1f commit ee9e328
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 32 deletions.
6 changes: 3 additions & 3 deletions functions/src/routers/postRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ router.post("/", async (req: Request, res: Response) => {
let submittedAt: PostWithGoalId["submittedAt"];

try {
({ userId, storedURL, text, goalId, submittedAt } = req.body);
({ userId, storedURL, text = "", goalId, submittedAt } = req.body);
} catch (error) {
return res.status(400).json({ message: "Invalid request body" });
}

if (!userId || !storedURL || !text || !goalId || !submittedAt) {
if (!userId || !storedURL || !goalId || !submittedAt) {
return res.status(400).json({
message: "userId, storedURL, text, goalId, and submittedAt are required",
message: "userId, storedURL, goalId, and submittedAt are required",
});
}

Expand Down
46 changes: 42 additions & 4 deletions functions/src/routers/resultRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,35 @@ import { GoalWithIdAndUserData, User } from "./types";
const router = express.Router();
const db = admin.firestore();

const getResults = async (limit: number, offset: number, userId?: string) => {
const getResults = async (
limit: number,
offset: number,
userId?: string,
includeSuccess = true,
includeFailed = true,
includePending = true
) => {
let goalQuery = db.collection("goal").limit(limit).offset(offset);
if (userId) {
goalQuery = goalQuery.where("userId", "==", userId);
}

if (!includeSuccess) {
goalQuery = goalQuery.where("post", "==", null);
}

if (!includeFailed) {
goalQuery = goalQuery
.where("post", "!=", null)
.where("deadline", ">", new Date());
}

if (!includePending) {
goalQuery = goalQuery
.where("post", "==", null)
.where("deadline", "<=", new Date());
}

const goalSnapshot = await goalQuery.get();

const goals = goalSnapshot.docs.map((doc) => {
Expand Down Expand Up @@ -56,7 +80,7 @@ const getResults = async (limit: number, offset: number, userId?: string) => {
const post = goal.post;
if (post) {
if (post.submittedAt > goal.deadline) {
failedResults.push(goal, {
failedResults.push({
...goal,
userData,
});
Expand All @@ -79,7 +103,11 @@ const getResults = async (limit: number, offset: number, userId?: string) => {
}
}

return { successResults, failedResults, pendingResults };
return {
successResults,
failedResults,
pendingResults,
};
};

// GET: 全ての目標または特定のユーザーの目標に対する結果を取得
Expand All @@ -88,9 +116,19 @@ router.get("/:userId?", async (req: Request, res: Response) => {

const limit = parseInt(req.query.limit as string) || 10;
const offset = parseInt(req.query.offset as string) || 0;
const includeSuccess = req.query.success !== "false";
const includeFailed = req.query.failed !== "false";
const includePending = req.query.pending !== "false";

try {
const results = await getResults(limit, offset, userId);
const results = await getResults(
limit,
offset,
userId,
includeSuccess,
includeFailed,
includePending
);
return res.json(results);
} catch (error) {
return res.status(500).json({ message: "Error fetching results" });
Expand Down
9 changes: 8 additions & 1 deletion src/Components/GoalModal/CopyGoalAfterPostButton.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Add } from "@mui/icons-material";
import { Button } from "@mui/joy";
import { useState } from "react";
import { showSnackBar } from "../SnackBar/SnackBar";
import CreateGoalModal from "./CreateGoalModal";

export default function CopyGoalAfterPostButton({
Expand All @@ -18,7 +19,13 @@ export default function CopyGoalAfterPostButton({
variant="solid"
color="success"
startDecorator={<Add />}
onClick={() => setOpen(true)}
onClick={() => {
setOpen(true);
showSnackBar({
message: "1日後の同じ時間で同じ目標を作成できます",
type: "success",
});
}}
>
この目標を次の日も達成する
</Button>
Expand Down
9 changes: 8 additions & 1 deletion src/Components/GoalModal/CopyGoalButton.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import LibraryAddOutlinedIcon from "@mui/icons-material/LibraryAddOutlined";
import { useState } from "react";
import { showSnackBar } from "../SnackBar/SnackBar";
import CreateGoalModal from "./CreateGoalModal";

export default function CopyModalButton({
Expand All @@ -14,7 +15,13 @@ export default function CopyModalButton({
return (
<>
<LibraryAddOutlinedIcon
onClick={() => setOpen(true)}
onClick={() => {
setOpen(true);
showSnackBar({
message: "1日後の同じ時間で同じ目標を作成できます",
type: "success",
});
}}
sx={{ cursor: "pointer", fontSize: "23px" }}
/>

Expand Down
13 changes: 11 additions & 2 deletions src/Components/GoalModal/CreateGoalModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { showSnackBar } from "@/Components/SnackBar/SnackBar";
import { Goal } from "@/types/types";
import { createGoal, handleCreateGoalError } from "@/utils/API/Goal/createGoal";
import { useUser } from "@/utils/UserContext";
import SendIcon from "@mui/icons-material/Send";
import AddIcon from "@mui/icons-material/Add";
import {
Button,
DialogContent,
Expand Down Expand Up @@ -43,6 +43,15 @@ export default function CreateGoalModal({
);
localDate.setDate(localDate.getDate() + 1); // 1日後にする
setDueDate(localDate.toISOString().slice(0, 16));
} else {
// 初期値の指定が無い場合は次の日の23時に設定
const nextDay = new Date();
nextDay.setDate(nextDay.getDate() + 1);
nextDay.setHours(23, 0, 0, 0);
const localNextDay = new Date(
nextDay.getTime() - nextDay.getTimezoneOffset() * 60000
);
setDueDate(localNextDay.toISOString().slice(0, 16));
}
}, [defaultText, defaultDeadline]);

Expand Down Expand Up @@ -146,7 +155,7 @@ export default function CreateGoalModal({
disabled={
!user || user?.loginType === "Guest" || !user?.isMailVerified
}
endDecorator={<SendIcon />}
endDecorator={<AddIcon />}
>
作成
</Button>
Expand Down
48 changes: 27 additions & 21 deletions src/Components/Progress/Progress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ export default function Progress({
...pendingResults.map((result) => ({ ...result, type: "pending" })),
];

console.log("allResults: ", allResults);

// typeがsuccessの場合はsubmittedAtでソートし、それ以外の場合はdeadlineでソートする
allResults.sort((a, b) => {
const getUpdatedTime = (item: typeof a) => {
Expand All @@ -74,29 +72,36 @@ export default function Progress({
: getUpdatedTime(a) - getUpdatedTime(b); // 最古が上位
});

console.log("allResults: ", allResults);

return (
<>
{allResults.map((result) => {
if (result.type === "success") {
<SuccessStep
key={result.goalId}
result={result as GoalWithIdAndUserData}
user={user as User}
/>;
return (
<SuccessStep
key={result.goalId}
result={result as GoalWithIdAndUserData}
user={user as User}
/>
);
} else if (result.type === "failed") {
<FailedStep
key={result.goalId}
result={result as GoalWithIdAndUserData}
user={user as User}
/>;
return (
<FailedStep
key={result.goalId}
result={result as GoalWithIdAndUserData}
user={user as User}
/>
);
} else if (result.type === "pending") {
<PendingStep
key={result.goalId}
result={result as GoalWithIdAndUserData}
user={user as User}
/>;
return (
<PendingStep
key={result.goalId}
result={result as GoalWithIdAndUserData}
user={user as User}
/>
);
}
return null;
})}
</>
);
Expand Down Expand Up @@ -165,8 +170,8 @@ const SuccessStep = ({
style={{
objectFit: "contain",
maxWidth: "100%",
maxHeight: "70vh",
borderRadius: "6px 6px 0 0",
maxHeight: "50vh",
borderRadius: "5px 5px 0 0",
}}
loading="lazy"
alt=""
Expand Down Expand Up @@ -306,7 +311,8 @@ const GoalCard = ({
variant="outlined"
size="sm"
sx={{
width: "93%",
width: "100%",
boxSizing: "border-box",
borderColor:
resultType == "success"
? innerBorderColors.success
Expand Down

0 comments on commit ee9e328

Please sign in to comment.