Files
lg-cxdash-front/src/app/(pages)/(main)/total/purchase/page.tsx

146 lines
4.8 KiB
TypeScript

"use client";
import {useEffect, useMemo} from "react";
import {
PRODUCT_KEYS,
PRODUCTS,
} from "@/const/lg-product.const";
import { PRODUCT_KEY_MAP } from "@/const/lg-schema.const";
import {
SITE_TITLE,
USE_CACHE,
API_DECIMAL_POINT,
} from "@/const/lg-common.const";
import { AXIS_HIDE_STYLE, GRID_X_NOT_PADDING } from "@/components/lge-dashboard/chart/chart.config";
import {
BottomDescription,
type ChartData,
signStyleGapRow,
TopChartTable,
type TopChartTableRow,
VerticalBar,
} from "@/components/lge-dashboard";
import { useLayoutFilter } from "@/components/layout/LayoutFilterContext";
import {
calcGapProductRow,
formatRowLabel,
normalizeFilterCondition,
withGapRow,
wrapSampleRow
} from "@/lib/lg-dashboard";
import {buildHeader_ProductKeyMap, exportDashboardExcel} from "@/lib/lg-export-excel";
import {useDebouncedValue} from "@/hooks/useDebouncedValue";
import {SelectedMap} from "@/types/common";
import {StatisticsRequest} from "@/types/lg.types";
import {useProductStatisticsQuery} from "@/query/statistics";
const PurchasePage = () => {
const { filterValue, registerExcelExporter } = useLayoutFilter();
const debouncedFilterValue = useDebouncedValue<SelectedMap>(filterValue as SelectedMap);
const req = useMemo<StatisticsRequest>(() => {
return {
variableIdMap: { purchase: "KQB0"},
bannerIdList: ["product"],
filterCondition: normalizeFilterCondition(debouncedFilterValue),
option: { useCache: USE_CACHE, lastYear: false, decimalPoint: API_DECIMAL_POINT },
};
}, [debouncedFilterValue]);
const { data, isError, error } = useProductStatisticsQuery(req);
if(isError){
console.error(error);
}
const purchaseRows =
data?.statisticsDataMap?.purchase
.filter((r) => r.rowLabel == `구매`)
.map((row) => {
const rowSample = data?.statisticsDataMap?.purchase
.find(r => r.rowLabel == '사례수' && r.year == row.year)?.total ?? 0;
return {
...row,
rowLabel: row.year,
sample: `(${rowSample})`,
}
}) ?? [];
const purchaseRowsWithGap = useMemo(() => {
return withGapRow(wrapSampleRow(purchaseRows, false), calcGapProductRow, -1, -2);
}, [purchaseRows]);
const purchaseTableRows = useMemo<TopChartTableRow[]>(() => {
return purchaseRowsWithGap.map((row) => ({
label: formatRowLabel(row.rowLabel),
sample: row.sample,
total: row.total,
values: PRODUCT_KEYS.map((k) => (row as any)[k] ?? null),
}));
}, [purchaseRowsWithGap]);
const purchaseChartData = useMemo<ChartData>(() => {
return {
categories: [...PRODUCTS],
series: purchaseRows.filter((r) => !r.rowLabel.includes("사례수"))
.map((r) => ({
name: r.rowLabel,
data: PRODUCT_KEYS.map((k) => (r as any)[k] ?? null),
})),
};
}, [purchaseRows]);
const description = useMemo(() => {
return "Q. 다음 중 귀댁에서 보유하고 계신 가전/전자 제품은 무엇입니까? 모두 선택해주세요.<br/>" +
"* 구독/렌탈/선물/경품/아파트 내 빌트인 제품을 모두 포함하여 선택해주세요.<br/>" +
"Q. 보유하고 계신 (제품)을 언제 구입하셨습니까?<br/>";
/*
"* 새 상품을 선물 받은 경우, 선물 받은 연도로 입력해주세요.<br/>" +
"** 중고 제품인 경우, 중고 제품을 구입한 연도로 입력해주세요.<br/>" +
"*** 신규 입주 시 빌트인되어 있던 제품은 입주한 연도를 입력해주세요.";
*/
}, []);
useEffect(() => {
registerExcelExporter(async () => {
const excelHeader = buildHeader_ProductKeyMap(PRODUCT_KEY_MAP, "sample");
await exportDashboardExcel({
fileName: `${SITE_TITLE}(구매율).xlsx`,
sheetName: "구매율",
sections: [
{ title: "구매율", header: excelHeader, rows: purchaseRowsWithGap, rowGap: 1 },
],
});
});
return () => registerExcelExporter(null);
}, [registerExcelExporter, purchaseRowsWithGap]);
return (
<div>
<TopChartTable
title={"전체 구매율"}
categories={[...PRODUCTS]}
rows={purchaseTableRows}
chart={
<VerticalBar
data={purchaseChartData}
paletteVarNames={["--chart-3", "--chart-2", "--chart-1"]}
grid={GRID_X_NOT_PADDING}
xAxis={{ type: "category", data: [...PRODUCTS], ...AXIS_HIDE_STYLE }}
yAxis={{ type: "value", min:0, max:100, ...AXIS_HIDE_STYLE }}
showFocusLabel={"last"}
height={260}
/>
}
signStyle={signStyleGapRow}
/>
<BottomDescription description={description} />
</div>
);
};
export default PurchasePage;