import { Container, KeenIcon, DefaultTooltip } from '@/components'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { DateRange } from 'react-day-picker'; import { useState, useEffect, useCallback } from 'react'; import { Card, Chart, YearPicker } from './blocks'; import moment from 'moment'; import { useFetchCardData, useFetchChartData } from './hooks'; import { Button } from '@/components/ui/button'; 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'; import TransactionValue from './blocks/TransactionValue'; import TransactionPieChart from './blocks/TransactionPieChart'; import MemberActivity from './blocks/MemberActivity'; import BankSaldo from './blocks/BankSaldo'; // sum -> nominal, count-> total type CountType = 'sum' | 'count'; type ChartType = 'line' | 'bar'; type ChartLegend = 'true' | 'false'; const DashboardHomePage = () => { const selectYear = get5LastYear(); const [initialYear, setInitialYear] = useState(''); const [selectedYear, setSelectedYear] = useState(''); const [count, setCount] = useState('sum'); const [chartType, setChartType] = useState('line'); const [chartLegend, setChartLegend] = useState('true'); const [dateRange, setDateRange] = useState<{ from: Date; to: Date }>({ from: new Date(), to: new Date() }); const getFirstDayOfMonth = () => { const now = new Date(); return new Date(now.getFullYear(), now.getMonth(), 1); }; const getToday = () => { return new Date(); }; const [fromDate, setFromDate] = useState(getFirstDayOfMonth()); const [toDate, setToDate] = useState(getToday()); const API_URL = apiConfig.api_dashboard; const { GetData } = useCallApi(); const API_URL_BANK = apiConfig.service_wallet; const [responseStatisticCard, setResponseStatisticCard] = useState(null); const fetchData = async () => { const res = await GetData(`${API_URL}/card-statistic`, {}); setResponseStatisticCard(res); }; const [bankaccount, setbankaccount] = useState(null); const fetchDataBankAccount = async () => { const res = await GetData(`${API_URL_BANK}/dashboard/balance/account/${getAuth()?.user?.customer?.id}`, {}); setbankaccount(res); }; useEffect(() => { fetchData(); }, []); useEffect(() => { fetchDataBankAccount(); }, []); const [responseGraphic, setresponseGraphic] = useState(null); useEffect(() => { const fetchDataGraphic = async () => { const res = await GetData(`${API_URL}/cashin-vs-cashout`, { date_from: fromDate.toISOString(), date_to: toDate.toISOString() }); setresponseGraphic(res); }; fetchDataGraphic(); }, [fromDate, toDate]); let staticChartDataApiFetch = []; // Make map for date => { cashin: 0, cashout: 0 } const dataMap: Record = {}; const current = moment(fromDate); const end = moment(toDate); // Inisialization dataMap with all dates while (current.isSameOrBefore(end, 'day')) { const dateStr = current.format('DD-MM-YYYY'); dataMap[dateStr] = { cashin: 0, cashout: 0 }; current.add(1, 'day'); } // Add cashin if (Array.isArray(responseGraphic?.data.total_cashin)) { responseGraphic.data.total_cashin.forEach((item: any) => { const date = moment(item.created_date).format('DD-MM-YYYY'); // Only update cashin if it's not already set (meaning no value was added before) if (dataMap[date]) { dataMap[date].cashin = Number(item.total_count ?? 0); } }); } // Add cashout if (Array.isArray(responseGraphic?.data.total_cashout)) { responseGraphic.data.total_cashout.forEach((item: any) => { const date = moment(item.created_date).format('DD-MM-YYYY'); // Only update cashout if it's not already set (meaning no value was added before) if (dataMap[date]) { dataMap[date].cashout = Number(item.total_count ?? 0); } }); } // Change to array for chart staticChartDataApiFetch = Object.entries(dataMap).map(([month, values]) => ({ month, cashin: values.cashin, cashout: values.cashout })); // Change to array for chart staticChartDataApiFetch = Object.entries(dataMap).map(([month, values]) => ({ month, cashin: values.cashin, cashout: values.cashout })); const currentRole = getAuth()?.role_name; const idCustomer = getAuth()?.user.customer?.id; // console.log(currentRole); // Menyusun tanggal awal dan akhir berdasarkan selectedYear useEffect(() => { if (selectYear.length > 0 && !selectedYear) { let latestYear = Math.max(...selectYear.map((item) => parseInt(item, 10))).toString(); // console.log('latestYear :', latestYear); setSelectedYear(latestYear); setInitialYear(latestYear); } }, [selectYear, selectedYear]); useEffect(() => { if (selectedYear) { setDateRange({ from: moment(`${selectedYear}-01-01`, 'YYYY-MM-DD').toDate(), to: moment(`${selectedYear}-12-31`, 'YYYY-MM-DD').toDate() }); } }, [selectedYear]); useEffect(() => { if (initialYear) { setDateRange({ from: moment(`${initialYear}-01-01`, 'YYYY-MM-DD').toDate(), to: moment(`${initialYear}-12-31`, 'YYYY-MM-DD').toDate() }); } }, [initialYear]); const handleYearChange = (year: string) => { setSelectedYear(year); }; const handleCountType = (value: CountType) => { setCount(value); }; const handleChartType = (value: ChartType) => { setChartType(value); }; const handleChartLegend = (value: ChartLegend) => { setChartLegend(value); }; const resetFilter = useCallback(() => { const firstDay = getFirstDayOfMonth(); const today = getToday(); setFromDate(firstDay); setToDate(today); // reset filter yang lain kalau perlu setDateRange({ from: firstDay, to: today }); setSelectedYear(initialYear); setCount('sum'); setChartType('line'); setChartLegend('true'); }, [initialYear]); const { cardData } = useFetchCardData( moment(dateRange.from).format('YYYY-MM-DD'), moment(dateRange.to).format('YYYY-MM-DD'), count ); const { chartData } = useFetchChartData( moment(dateRange.from).format('YYYY-MM-DD'), moment(dateRange.to).format('YYYY-MM-DD'), count ); const toolbar = (
); const number: number = responseStatisticCard?.data.total_cash_in ?? 0; const formattedNumber: number = parseFloat(number.toFixed(2)); const numbercashout: number = responseStatisticCard?.data.total_cash_out ?? 0; const formattedNumbercashout: number = parseFloat(numbercashout.toFixed(2)); return ( <> TPAY | Dashboard {/* Account Balance Cards */} {/* {currentRole === 'Escrow' || currentRole === 'Master Agent' ? (
) : null} */}
{bankaccount?.data && bankaccount?.data.length > 0 && getAuth()?.statusbalance=='Y' ? ( bankaccount?.data.map((bankaccountdatas: { id_balance:string,amount: string, credit_limit: string, monthly_limit: string; wallet: string; }, index: number) => ( )) ) : ( "" )}
{/* Cards */}
{/* Chart */}
); }; export default DashboardHomePage;