|
1 | 1 | import { JSX, useEffect, useState } from "react"; |
2 | 2 | import PieChart from "@/components/pie-chart"; |
3 | | -import { AccountType, getAccountInfo, DonationsListType, getUserDonations } from "@/util/request"; |
| 3 | +import { AccountType, getAccountInfo, isLoggedIn } from "@/util/request"; |
4 | 4 | import { useRouter } from "next/navigation"; |
| 5 | +import { useCookies } from "react-cookie"; |
5 | 6 |
|
6 | 7 |
|
7 | 8 | const Account = (): JSX.Element => { |
8 | 9 | const router = useRouter(); |
9 | 10 | const [accountInfo, setAccountInfo] = useState<AccountType>(); |
| 11 | + const [cookie] = useCookies(["token"]) |
10 | 12 | useEffect(() => { |
11 | | - getAccountInfo().then((response) => setAccountInfo(response.data)).catch(() => |
12 | | - router.push("login")) |
13 | | - }, []); |
14 | | - |
15 | | - |
16 | | - |
17 | | - const [donations, setDonations] = useState<DonationsListType>(); |
18 | | - useEffect(() => { |
19 | | - getUserDonations().then((response) => setDonations(response.data)); |
20 | | - }, []); |
| 13 | + isLoggedIn().then(() => |
| 14 | + getAccountInfo().then((response) => setAccountInfo(response.data)).catch(() => router.push("login")) |
| 15 | + ).catch( |
| 16 | + () => router.push("login") |
| 17 | + ); |
| 18 | + }, [cookie]); |
21 | 19 |
|
22 | 20 | // Variables are hard coded for now to demo until backend is implemented. |
23 | 21 | // const stocks_owned = ["Stock 1", "Stock 2", "Stock 3", "Stock 4", "Stock 5", "Stock 6", "Stock 7"]; |
@@ -54,15 +52,15 @@ const Account = (): JSX.Element => { |
54 | 52 | {/* Stocks owned */} |
55 | 53 | <div className="bg-gray-300 p-2 rounded-md"> |
56 | 54 | <h3 className="text-lg font-bold">Donations:</h3> |
57 | | - <p>Total Donations: {accountInfo?.total_donations?.toLocaleString('en-US', { style: 'currency', currency: 'CAD' })}</p> |
| 55 | + <p>Total Donations: {accountInfo?.total_donations?.toLocaleString('en-US', { style: 'currency', currency: 'CAD' })}</p> |
58 | 56 | <br></br> |
59 | | - {donations?.donations_list?.[0]?.map((donation, index) => ( |
| 57 | + {accountInfo?.donations.map((donation, index) => ( |
60 | 58 | <p key={index}> |
61 | | - {donation.toLocaleString('en-US', { style: 'currency', currency: 'CAD' })} on {new Date(donations?.donations_list?.[1]?.[index]).toLocaleDateString('en-US')} |
| 59 | + {donation.amount.toLocaleString('en-US', { style: 'currency', currency: 'CAD' })} on {new Date(donation.date).toLocaleDateString('en-US')} |
62 | 60 | </p> |
63 | 61 | ))} |
64 | 62 |
|
65 | | - |
| 63 | + |
66 | 64 |
|
67 | 65 | </div> |
68 | 66 |
|
|
0 commit comments