import React, { useEffect, useMemo, useState } from "react"; import Chart from "react-apexcharts"; import { useExpenseAnalysis } from "../../hooks/useDashboard_Data"; import { useSelectedProject } from "../../slices/apiDataManager"; import { DateRangePicker1 } from "../common/DateRangePicker"; import { FormProvider, useForm } from "react-hook-form"; import { formatCurrency, localToUtc } from "../../utils/appUtils"; const ExpenseAnalysis = () => { const projectId = useSelectedProject(); const methods = useForm({ defaultValues: { startDate: "", endDate: "", }, }); const { watch } = methods; const [startDate, endDate] = watch(["startDate", "endDate"]); const { data, isLoading, isError, error, isFetching } = useExpenseAnalysis( projectId, startDate ? localToUtc(startDate) : null, endDate ? localToUtc(endDate) : null ); if (isError) return
{error.message}
; const report = data?.report ?? []; const { labels, series, total } = useMemo(() => { const labels = report.map((item) => item.projectName); const series = report.map((item) => item.totalApprovedAmount || 0); const total = formatCurrency(data?.totalAmount || 0); return { labels, series, total }; }, [report, data?.totalAmount]); const donutOptions = { chart: { type: "donut" }, labels, legend: { show: false }, dataLabels: { enabled: true, formatter: (val) => `${val.toFixed(0)}%` }, colors: ["#7367F0", "#28C76F", "#FF9F43", "#EA5455", "#00CFE8", "#FF78B8"], plotOptions: { pie: { donut: { size: "70%", labels: { show: true, total: { show: true, label: "Total", fontSize: "16px", formatter: () => `${total}`, }, }, }, }, }, }; if (data?.report === 0) { return
No data found
; } return ( <>
Expense Breakdown

Category Wise Expense Breakdown

{/* Initial loading: show full loader */} {isLoading && (
Loading...
)} {/* Data display */} {!isLoading && report.length === 0 && (
No data found
)} {!isLoading && report.length > 0 && ( <> {/* Overlay spinner for refetch */} {isFetching && (
Loading...
)}
item.totalApprovedAmount || 0)} type="donut" width="320" />
{report.map((item, idx) => (
{item.projectName} {formatCurrency(item.totalApprovedAmount)}
))}
)}
); }; export default ExpenseAnalysis;