2023-06-15 12:52:46 +03:00
|
|
|
/* eslint-disable */
|
2023-06-05 18:58:59 +03:00
|
|
|
"use client";
|
|
|
|
import { redirect } from "next/navigation";
|
|
|
|
import { useEffect, useState } from "react";
|
2023-06-15 12:52:46 +03:00
|
|
|
|
|
|
|
import Spinner from "@/lib/components/ui/Spinner";
|
2023-06-20 17:17:13 +03:00
|
|
|
import { useAxios } from "@/lib/hooks";
|
2023-06-15 12:52:46 +03:00
|
|
|
import { UserStats } from "@/lib/types/User";
|
|
|
|
|
2023-06-20 17:17:13 +03:00
|
|
|
import { useSupabase } from "@/lib/context/SupabaseProvider";
|
2023-06-06 18:32:48 +03:00
|
|
|
import { UserStatistics } from "./components/UserStatistics";
|
2023-06-05 18:58:59 +03:00
|
|
|
|
2023-06-15 12:52:46 +03:00
|
|
|
const UserPage = (): JSX.Element => {
|
2023-06-05 18:58:59 +03:00
|
|
|
const [userStats, setUserStats] = useState<UserStats>();
|
|
|
|
const { session } = useSupabase();
|
2023-06-10 00:20:51 +03:00
|
|
|
const { axiosInstance } = useAxios();
|
2023-06-05 18:58:59 +03:00
|
|
|
if (session === null) {
|
|
|
|
redirect("/login");
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const fetchUserStats = async () => {
|
|
|
|
// setIsPending(true);
|
|
|
|
try {
|
|
|
|
console.log(
|
|
|
|
`Fetching user stats from ${process.env.NEXT_PUBLIC_BACKEND_URL}/user`
|
|
|
|
);
|
2023-06-10 00:20:51 +03:00
|
|
|
const response = await axiosInstance.get<UserStats>(
|
2023-06-05 18:58:59 +03:00
|
|
|
`${process.env.NEXT_PUBLIC_BACKEND_URL}/user`,
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${session.access_token}`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
setUserStats(response.data);
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error fetching user stats", error);
|
|
|
|
setUserStats(undefined);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
fetchUserStats();
|
|
|
|
}, [session.access_token]);
|
|
|
|
|
|
|
|
return (
|
2023-06-11 11:44:23 +03:00
|
|
|
<main className="w-full flex flex-col pt-10">
|
2023-06-05 18:58:59 +03:00
|
|
|
<section className="flex flex-col justify-center items-center flex-1 gap-5 h-full">
|
2023-06-06 18:32:48 +03:00
|
|
|
<div className="p-5 max-w-3xl w-full min-h-full flex-1 mb-24">
|
|
|
|
{userStats ? (
|
2023-06-05 18:58:59 +03:00
|
|
|
<>
|
|
|
|
<UserStatistics {...userStats} />
|
|
|
|
</>
|
2023-06-06 18:32:48 +03:00
|
|
|
) : (
|
|
|
|
<div className="flex items-center justify-center">
|
|
|
|
<span>Fetching your data...</span>
|
|
|
|
<Spinner />
|
|
|
|
</div>
|
2023-06-05 18:58:59 +03:00
|
|
|
)}
|
2023-06-06 18:32:48 +03:00
|
|
|
</div>
|
2023-06-05 18:58:59 +03:00
|
|
|
</section>
|
|
|
|
</main>
|
|
|
|
);
|
2023-06-15 12:52:46 +03:00
|
|
|
};
|
|
|
|
export default UserPage;
|