From 703604b9a71c047a1453abdce7ae643fa1fa65ad Mon Sep 17 00:00:00 2001 From: Ameya Shenoy Date: Wed, 22 Mar 2023 23:13:50 +0530 Subject: [PATCH] feat: load components before making backend request Signed-off-by: Ameya Shenoy --- web/app/components/BankAccounts.tsx | 54 ++++++++++++++ web/app/components/CenterCol.tsx | 71 ++++--------------- web/app/components/CenterLoading.tsx | 27 +++++++ web/app/components/Dashboard.tsx | 39 +++++----- web/app/components/RecentTransactionsList.tsx | 4 +- 5 files changed, 113 insertions(+), 82 deletions(-) create mode 100644 web/app/components/BankAccounts.tsx create mode 100644 web/app/components/CenterLoading.tsx diff --git a/web/app/components/BankAccounts.tsx b/web/app/components/BankAccounts.tsx new file mode 100644 index 0000000..5937356 --- /dev/null +++ b/web/app/components/BankAccounts.tsx @@ -0,0 +1,54 @@ + +import React from 'react' +import BankAccount from './BankAccount' + +export default function BankAccounts({ bankDetails }) { + const bankAccounts = [] + const infoTitles = [ + { + "title": "Account Number", + "key": "account_number", + }, + { + "title": "IFSC Code", + "key": "ifsc_code", + }, + { + "title": "Swift BIC", + "key": "swift_bic", + }, + { + "title": "Holder's Name", + "key": "holders_name", + }, + ] + + + bankDetails.forEach((bank) => { + const infoLines = [] + infoTitles.forEach((option) => { + infoLines.push({ + "title": option.title, + "value": bank[option.key], + }) + }) + + bankAccounts.push( + + ) + }) + + + return ( + <> + {bankAccounts} + + ) +} diff --git a/web/app/components/CenterCol.tsx b/web/app/components/CenterCol.tsx index 99ea1fd..c323ebb 100644 --- a/web/app/components/CenterCol.tsx +++ b/web/app/components/CenterCol.tsx @@ -1,21 +1,9 @@ -"use client" - import styles from './centerCol.module.css' -import Dashboard from './Dashboard' -import BankAccount from './BankAccount' import React from 'react' -import useSWR from 'swr' +import CenterLoading from './CenterLoading' export default function CenterCol() { - const fetcher = (...args) => fetch(...args).then((res) => res.json()) - const { data, error } = useSWR(`http://localhost:8000/api/v1/accounts/`, fetcher) - - if (error) return
Error: Failed to load
- if (!data) return
Loading...
- - const bankDetails = data.results - // const bankDetails = { // banks: [{ // bankName: "SBI", @@ -91,58 +79,25 @@ export default function CenterCol() { // }] // }] // } + let today = new Date() + let curHr = today.getHours() + let greet = "Good " - const bankAccounts = [] - const infoTitles = [ - { - "title": "Account Number", - "key": "account_number", - }, - { - "title": "IFSC Code", - "key": "ifsc_code", - }, - { - "title": "Swift BIC", - "key": "swift_bic", - }, - { - "title": "Holder's Name", - "key": "holders_name", - }, - ] - - bankDetails.forEach((bank) => { - const infoLines = [] - infoTitles.forEach((option) => { - infoLines.push({ - "title": option.title, - "value": bank[option.key], - }) - }) - - bankAccounts.push( - - ) - }) + if (curHr < 12) { + greet += 'morning' + } else if (curHr < 18) { + greet += 'afternoon' + } else { + greet += 'evening' + } return (
-
Good morning, Nishant
+
{greet}, Nishant
It's 3rd of January, a Monday
- -
- {bankAccounts} -
+
) } diff --git a/web/app/components/CenterLoading.tsx b/web/app/components/CenterLoading.tsx new file mode 100644 index 0000000..e4bc38c --- /dev/null +++ b/web/app/components/CenterLoading.tsx @@ -0,0 +1,27 @@ +"use client" + +import React from 'react' +import useSWR from 'swr' +import Dashboard from './Dashboard' +import BankAccounts from './BankAccounts' +import styles from './centerCol.module.css' + + +export default function CenterLoading() { + const fetcher = (...args) => fetch(...args).then((res) => res.json()) + const { data, error } = useSWR(`http://localhost:8000/api/v1/accounts/`, fetcher) + + if (error) return
Error: Failed to load
+ if (!data) return
+ + const bankDetails = data.results + + return ( + <> + +
+ +
+ + ) +} diff --git a/web/app/components/Dashboard.tsx b/web/app/components/Dashboard.tsx index eaaf8fc..96d679d 100644 --- a/web/app/components/Dashboard.tsx +++ b/web/app/components/Dashboard.tsx @@ -7,7 +7,21 @@ import { AiOutlineBank } from 'react-icons/ai'; import { BiRupee } from 'react-icons/bi'; -export default function Dashboard() { +export default function Dashboard({ bankDetails }) { + const banks = [] + let total_balance = 0 + bankDetails.forEach((bank) => { + total_balance += bank.balance + banks.push( + + ) + }) + + return (
@@ -20,26 +34,7 @@ export default function Dashboard() { /> Banks
- - - - + {banks}
-
3,49,904
+
{total_balance.toLocaleString('en-IN')}
↑20% compared to previous month diff --git a/web/app/components/RecentTransactionsList.tsx b/web/app/components/RecentTransactionsList.tsx index 9724abc..3408f5c 100644 --- a/web/app/components/RecentTransactionsList.tsx +++ b/web/app/components/RecentTransactionsList.tsx @@ -9,7 +9,7 @@ export default function RecentTransactionsList() { const { data, error } = useSWR(`http://localhost:8000/api/v1/transactions/`, fetcher) if (error) return
Error: Failed to load
- if (!data) return
Loading...
+ if (!data) return
const transactions = data.results @@ -55,7 +55,7 @@ export default function RecentTransactionsList() { title={transaction.to_account.holders_name} tag={transaction.tag.title} datetime={relateive_date(new Date(transaction.created_at))} - amount={transaction.amount} + amount={transaction.amount.toLocaleString('en-IN')} icon_type={transaction.tag.icon_type} /> )