From 88a4d186046ae79fb3ca87fb91efc79b28c54bff Mon Sep 17 00:00:00 2001 From: Wind-Explorer Date: Tue, 13 Aug 2024 13:31:31 +0800 Subject: [PATCH] Connected springboard to HBC --- client/src/icons.tsx | 20 ++++- client/src/pages/Ranking.tsx | 24 +++--- client/src/pages/SpringboardPage.tsx | 115 ++++++++++++++++++++++++--- server/routes/users.js | 2 + 4 files changed, 138 insertions(+), 23 deletions(-) diff --git a/client/src/icons.tsx b/client/src/icons.tsx index 7bea6e9..a1ec024 100644 --- a/client/src/icons.tsx +++ b/client/src/icons.tsx @@ -727,6 +727,24 @@ export const ImageIcon = () => { d="m2.25 15.75 5.159-5.159a2.25 2.25 0 0 1 3.182 0l5.159 5.159m-1.5-1.5 1.409-1.409a2.25 2.25 0 0 1 3.182 0l2.909 2.909m-18 3.75h16.5a1.5 1.5 0 0 0 1.5-1.5V6a1.5 1.5 0 0 0-1.5-1.5H3.75A1.5 1.5 0 0 0 2.25 6v12a1.5 1.5 0 0 0 1.5 1.5Zm10.5-11.25h.008v.008h-.008V8.25Zm.375 0a.375.375 0 1 1-.75 0 .375.375 0 0 1 .75 0Z" /> - + ); +}; + +export const ArrowTrendingUpIcon = () => { + return ( + + + ); }; diff --git a/client/src/pages/Ranking.tsx b/client/src/pages/Ranking.tsx index 708804f..7c94e7a 100644 --- a/client/src/pages/Ranking.tsx +++ b/client/src/pages/Ranking.tsx @@ -27,7 +27,7 @@ interface User { townCouncil: string; } -interface FormData { +export interface FormData { id: string; electricalBill: number; waterBill: number; @@ -60,6 +60,17 @@ interface UserVoucher { voucherId: string; } +// Sort form data based on descriptor +export const sortFormData = (list: FormData[], descriptor: SortDescriptor) => { + const { column } = descriptor; + + if (column === "avgBill") { + return [...list].sort((a, b) => a.avgBill - b.avgBill); + } + + return list; +}; + export default function Ranking() { const [originalFormData, setOriginalFormData] = useState([]); const [filteredFormData, setFilteredFormData] = useState([]); @@ -157,17 +168,6 @@ export default function Ranking() { setTop3Users(topUsersByTownCouncil); }, [filteredFormData, userData]); - // Sort form data based on descriptor - const sortFormData = (list: FormData[], descriptor: SortDescriptor) => { - const { column } = descriptor; - - if (column === "avgBill") { - return [...list].sort((a, b) => a.avgBill - b.avgBill); - } - - return list; - }; - const sortedFormData = sortFormData(filteredFormData, sortDescriptor); const combinedData: FormDataWithUser[] = sortedFormData.map((data) => { diff --git a/client/src/pages/SpringboardPage.tsx b/client/src/pages/SpringboardPage.tsx index b361706..4a02b04 100644 --- a/client/src/pages/SpringboardPage.tsx +++ b/client/src/pages/SpringboardPage.tsx @@ -1,23 +1,26 @@ import { useNavigate } from "react-router-dom"; import { useEffect, useState } from "react"; -import { Button, Card, Link } from "@nextui-org/react"; -import { PencilSquareIcon } from "../icons"; +import { Button, Card, Link, SortDescriptor } from "@nextui-org/react"; +import { ArrowTrendingUpIcon, PencilSquareIcon, VoucherIcon } from "../icons"; import SpringboardButton from "../components/SpringboardButton"; import { getTimeOfDay } from "../utilities"; import { retrieveUserInformation } from "../security/users"; import UserProfilePicture from "../components/UserProfilePicture"; import instance from "../security/http"; import config from "../config"; +import { FormData, sortFormData } from "./Ranking"; export default function SpringboardPage() { const navigate = useNavigate(); - let accessToken = localStorage.getItem("accessToken"); + const accessToken = localStorage.getItem("accessToken"); if (!accessToken) { navigate("/signin"); } const [userInformation, setUserInformation] = useState(); const [events, setEvents] = useState([]); - let timeOfDay = getTimeOfDay(); + const [userVouchers, setUserVouchers] = useState(); + const [contestRanking, setContestRanking] = useState(-1); + const timeOfDay = getTimeOfDay(); let greeting = ""; if (timeOfDay === 0) { @@ -34,7 +37,27 @@ export default function SpringboardPage() { if (response.accountType == 2) { navigate("/admin"); } + let tmpUserInfo = response; setUserInformation(response); + instance + .get("/user-vouchers/user/" + response.id) + .then((vouchersResponse) => { + setUserVouchers(vouchersResponse.data); + }); + instance.get("/hbcform/").then((formResponse) => { + let rankings: FormData[] = formResponse.data; + rankings = sortFormData(rankings, { + column: "avgBill", + direction: "ascending", + }); + for (let i = 0; i < rankings.length; i++) { + console.log("checking", rankings[i].userId, tmpUserInfo.id); + if (rankings[i].userId == tmpUserInfo.id) { + setContestRanking(i + 1); + break; + } + } + }); }) .catch((_) => { navigate("/account-inaccessible"); @@ -116,14 +139,11 @@ export default function SpringboardPage() {
{events.length > 0 && (
-

Registered events

+

Registered Events

{events.map((event, index) => ( - -
+ +
)}
+
+
+

Your Contest Statistics

+
+
+
+
+
+ +
+ {userVouchers && ( +

+ + {userVouchers.userVouchers.length} + {" "} + vouchers available +

+ )} +
+
+ +
+
+
+
+
+ +
+

+ {!(contestRanking > 0) && ( + + No + + )} + {contestRanking > 0 && ( + + {contestRanking} + {contestRanking == 1 + ? "st" + : contestRanking == 2 + ? "nd" + : contestRanking == 3 + ? "rd" + : "th"} + + )}{" "} + place in the leaderboard +

+
+
+ +
+
+
+
)}
diff --git a/server/routes/users.js b/server/routes/users.js index f293339..9213814 100644 --- a/server/routes/users.js +++ b/server/routes/users.js @@ -368,6 +368,8 @@ router.put("/request-reset-password/:email", async (req, res) => { { resetPasswordToken: token, resetPasswordExpireTime: Date.now() + 3600000, + // resetPasswordExpireTime: Date.now() + 3600000, + // GENERICMARKER }, { where: { id: user.id },