From 162d28bcdef8d08e9b360ed1cbc6b0e843ba310e Mon Sep 17 00:00:00 2001 From: StackCanary Date: Fri, 20 Dec 2024 15:43:05 +0300 Subject: [PATCH] feat: add user statistics card --- src/App.jsx | 20 ++++++- src/components/UserStatisticsCard.jsx | 79 +++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 src/components/UserStatisticsCard.jsx diff --git a/src/App.jsx b/src/App.jsx index 6b0e7ac..d087264 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -3,12 +3,25 @@ import viteLogo from "/logo.png"; import { Button, Stack, Typography } from "@mui/material"; import AuthContext from "./contexts/auth-context"; import useUser from "./hooks/use-user"; +import UserStatisticsCard from "./components/UserStatisticsCard"; +import useSWR from "swr"; +import { getAuthFetcher } from "./fetcher"; +import PageLoader from "./components/PageLoader"; function App() { const [count, setCount] = useState(0); + const { token } = useContext(AuthContext); const { user } = useUser(); - const { logout } = useContext(AuthContext); + + const { data: userStats, isLoading } = useSWR( + "/stats/", + getAuthFetcher(token) + ); + + if (isLoading) { + return ; + } return ( - Welcome, {user.username} + + Welcome, {user.username} + + ); } diff --git a/src/components/UserStatisticsCard.jsx b/src/components/UserStatisticsCard.jsx new file mode 100644 index 0000000..7134b4c --- /dev/null +++ b/src/components/UserStatisticsCard.jsx @@ -0,0 +1,79 @@ +import React from "react"; +import { Card, CardContent, Typography, Grid, Divider } from "@mui/material"; + +const UserStatisticsCard = ({ stats }) => { + const { total_average, total_activity_days, activities_stats } = stats; + + console.log("statoos", activities_stats); + + return ( + + + + User Statistics + + + Total Average: {total_average ? total_average.toFixed(2) : "No data"} + + + Total Activity Days: {total_activity_days || "No data"} + + + {/* TODO: Refactor this to isEmpty method */} + {Object.keys(activities_stats).length !== 0 ? ( + <> + + Activities Stats + + + {Object.entries(activities_stats).map(([activity, data]) => ( + + + + {activity} + + Days Spent: {data.days_spent || "No data"} + + + Average Title Duration:{" "} + {data.average_title_duration !== null + ? data.average_title_duration + : "No data"} + + + Least Long Lasting Days:{" "} + {data.least_long_lasting_days !== null + ? data.least_long_lasting_days + : "No data"} + + + Least Long Lasting Name:{" "} + {data.least_long_lasting_name || "No data"} + + + Most Long Lasting Days:{" "} + {data.most_long_lasting_days !== null + ? data.most_long_lasting_days + : "No data"} + + + Most Long Lasting Name:{" "} + {data.most_long_lasting_name || "No data"} + + + + + ))} + + + ) : ( + + Sorry, you have no stats + + )} + + + ); +}; + +export default UserStatisticsCard;