Files
lg-cxdash-front/src/components/lge-dashboard/sub-page/category-page/PurchaseChannel.tsx

77 lines
2.4 KiB
TypeScript

'use client'
import {useLayoutFilter} from "@/components/layout/LayoutFilterContext";
import {
apiPurchaseChannelByYear,
apiPurchaseChannelSummaryByYear
} from "@/config/sample.data";
import {useEffect, useMemo} from "react";
import {PageData, ServerBannerRow} from "@/types/lg";
import {CategoryByData} from "@/components/lge-dashboard";
import {BANNER_GROUP, BANNER_KEY_MAP, SITE_TITLE, YEARS} from "@/const/lgConst";
import {CategoryTypeValue} from "@/components/layout/CategoryFilter";
import {buildHeader_Banner, exportDashboardExcel, Section} from "@/lib/lg-export-excel";
const PurchaseChannel = () => {
const { filterValue, registerExcelExporter } = useLayoutFilter();
console.log(filterValue);
const tableDataArray = useMemo<PageData<ServerBannerRow>[]>(() => [
{
title: "구매 채널(온/오프라인)",
chartMode: "reverseStack",
data: apiPurchaseChannelSummaryByYear,
},
{
title: "구매 채널(상세)",
chartMode: "horizontal",
data: apiPurchaseChannelByYear,
chartHeight: 550,
},
], []);
useEffect(() => {
registerExcelExporter(async (payload) => {
const sheetName = `${filterValue.product}_${(filterValue.type as CategoryTypeValue).label}_구매 채널`
const h3 = buildHeader_Banner(BANNER_GROUP, BANNER_KEY_MAP);
type AnyRow = Record<string, any>;
const sections: Section<AnyRow>[] = tableDataArray.flatMap((tableData) => {
const years = [YEARS.at(-1), YEARS.at(-2)];
return years.map((year) => ({
title: `${tableData.title}_${year}년`,
header: h3,
rows: tableData.data.filter((d) => d.year === year) as AnyRow[],
rowGap: 1,
}));
});
await exportDashboardExcel({
fileName: `${SITE_TITLE}(${sheetName}).xlsx`,
sheetName: sheetName,
sections: sections,
});
});
return () => registerExcelExporter(null);
}, [registerExcelExporter]);
return (
<div>
{tableDataArray.map((tableData) => (
<CategoryByData
key={`cbd-${tableData.title.replaceAll(/\s+/g, "_")}`}
selectedYear={filterValue.year}
title={tableData.title}
chartMode={tableData.chartMode}
allTableData={tableData.data}
chartHeight={tableData.chartHeight}
chartWidth={tableData.chartWidth}
/>
))}
</div>
);
}
export default PurchaseChannel;