diff --git a/src/pages/dashboards/home/DashboardHomePage.tsx b/src/pages/dashboards/home/DashboardHomePage.tsx index 4884ebb..856d7d1 100644 --- a/src/pages/dashboards/home/DashboardHomePage.tsx +++ b/src/pages/dashboards/home/DashboardHomePage.tsx @@ -16,14 +16,26 @@ import { useFetchYear } from './hooks/useFetchYear'; import { get5LastYear } from '@/utils/Date'; import { staticChartData } from './staticChart'; import { Helmet } from 'react-helmet'; +import BalanceCard from './blocks/BalanceCard'; +import { getAuth } from '@/auth'; +import { useCallApi } from '@/hooks'; +import { apiConfig } from '@/config/api.config'; // sum -> nominal, count-> total type CountType = 'sum' | 'count'; type ChartType = 'line' | 'bar'; type ChartLegend = 'true' | 'false'; +type RoleType = { + id: string; + name: string; +}; + +const API_URL_DASHBOARD = apiConfig.service_dashboard; + const DashboardHomePage = () => { const selectYear = get5LastYear(); + const { GetData } = useCallApi(); const [initialYear, setInitialYear] = useState(''); const [selectedYear, setSelectedYear] = useState(''); const [count, setCount] = useState('sum'); @@ -33,6 +45,31 @@ const DashboardHomePage = () => { from: new Date(), to: new Date() }); + const [roles, setRoles] = useState([]); + const parsedUser = getAuth()?.user; + const idCustomer = parsedUser?.customer?.id; + const roleCustomer = parsedUser.idRole; + const currentRole = roles.find((role) => role.id === roleCustomer)?.name?.toLowerCase(); + console.log(currentRole); + + useEffect(() => { + async function fetchRoles(sorting: any) { + try { + const response = await GetData(`${API_URL_DASHBOARD}/user_role/list`, { + limit: 100, + page: 1, + with_deleted: false, + order_field: sorting[0].id, + order_direction: sorting[0].desc == false ? 'ASC' : 'DESC' + }); + setRoles(response?.data.list); + } catch (error) { + console.error('Error fetching roles', error); + } + } + + fetchRoles([{ id: 'name', desc: false }]); + }, []); // Menyusun tanggal awal dan akhir berdasarkan selectedYear useEffect(() => { @@ -144,6 +181,13 @@ const DashboardHomePage = () => { TPAY | Dashboard + {/* Account Balance Cards */} + {currentRole === 'escrow' || currentRole === 'master agent' ? ( +
+ +
+ ) : null} +
diff --git a/src/pages/dashboards/home/blocks/BalanceCard.tsx b/src/pages/dashboards/home/blocks/BalanceCard.tsx new file mode 100644 index 0000000..87aaaf6 --- /dev/null +++ b/src/pages/dashboards/home/blocks/BalanceCard.tsx @@ -0,0 +1,94 @@ +import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; +import { apiConfig } from '@/config/api.config'; +import { useCallApi } from '@/hooks'; +import { CreditCard, Wallet } from 'lucide-react'; +import { useEffect, useState } from 'react'; + +type WalletResponse = { + amount: number; + id_wallet: string; + wallet: string; + credit_limit: number; + monthly_limit: number; +}; + +interface BalanceCardProps { + id: string; +} + +const API_URL_WALLET = apiConfig.service_wallet; + +const BalanceCard = ({ id }: BalanceCardProps) => { + const { GetData } = useCallApi(); + const [wallets, setWallets] = useState([]); + + const formatCurrency = (value: number) => { + return new Intl.NumberFormat('en-US', { + style: 'currency', + currency: 'USD' + }).format(value); + }; + + const getColorClass = (amount: number) => { + if (amount > 5000) return 'bg-emerald-500'; + if (amount > 0) return 'bg-blue-500'; + return 'bg-red-500'; + }; + + useEffect(() => { + async function fetchAccountBalances() { + try { + const response = await GetData(`${API_URL_WALLET}/dashboard/balance/account/${id}`, { + id: id + }); + if (response?.status === true) { + setWallets(response.data); + } + } catch (error) { + console.error('Error fetching account balances', error); + } + } + + fetchAccountBalances(); + }, [id]); + + return ( +
+ {wallets.map((wallet) => { + const colorClass = getColorClass(wallet.amount); + const isNegative = wallet.amount < 0; + + return ( +
+
+
+
+

{wallet.wallet}

+ {wallet.wallet.includes('Credit') ? ( + + ) : ( + + )} +
+

+ {formatCurrency(wallet.amount)} +

+ +
+
Credit Limit:
+
{wallet.credit_limit}
+
Monthly Limit:
+
{wallet.monthly_limit}
+
+
+
+ ); + })} +
+ ); +}; + +export default BalanceCard;