260225_01_ENH: 실제 Data 통계 API와 연결 및 일부 페이지 축소
This commit is contained in:
@@ -1,70 +1,132 @@
|
||||
"use client";
|
||||
|
||||
import {useEffect, useMemo} from "react";
|
||||
import {LAST_YEAR, PRODUCT_KEY_MAP, PRODUCT_KEYS, PRODUCTS, SITE_TITLE, YEARS} from "@/const/lgConst";
|
||||
import {
|
||||
CHART_COLOR_BY_YEAR, CHART_FOCUS_COLOR_BY_YEAR,
|
||||
PRODUCT_KEY_MAP,
|
||||
PRODUCT_KEYS,
|
||||
SITE_TITLE,
|
||||
YEARS
|
||||
} from "@/const/lgConst";
|
||||
import {
|
||||
BottomDescription,
|
||||
GroupedOverlayBar,
|
||||
type GroupedOverlayBarData,
|
||||
signStyleGapRow,
|
||||
TopChartTable,
|
||||
type TopChartTableRow,
|
||||
} from "@/components/lge-dashboard";
|
||||
import { apiOwnShareOfTotalPurchase, apiTrendBuy } from "@/config/sample.data";
|
||||
import { useLayoutFilter } from "@/components/layout/LayoutFilterContext";
|
||||
import { formatRowLabel } from "@/lib/lg-dashboard";
|
||||
import {
|
||||
calcGapProductRow, calcTotalBaseRow,
|
||||
formatRowLabel,
|
||||
normalizeFilterCondition,
|
||||
withGapRow, withTotalBaseRow
|
||||
} from "@/lib/lg-dashboard";
|
||||
import {buildHeader_ProductKeyMap, exportDashboardExcel} from "@/lib/lg-export-excel";
|
||||
import {useDebouncedValue} from "@/hooks/useDebouncedValue";
|
||||
import {SelectedMap} from "@/types/common";
|
||||
import {ProductKey, StatisticsRequest} from "@/types/lg";
|
||||
import {useProductStatisticsQuery} from "@/hooks/useLgStatisticsQuery";
|
||||
import {toNumOr0} from "@/lib/utils";
|
||||
|
||||
const LGPurchasePage = () => {
|
||||
const { filterValue, registerExcelExporter } = useLayoutFilter();
|
||||
console.log(filterValue);
|
||||
const debouncedFilterValue = useDebouncedValue<SelectedMap>(filterValue as SelectedMap, 500);
|
||||
|
||||
//TODO: LG가 0인 제품은 제외시키기
|
||||
const req = useMemo<StatisticsRequest>(() => {
|
||||
return {
|
||||
variableIdMap: {purchase: "KQA3_2", purchase_lg: "KQX3_3_LG"},
|
||||
bannerIdList: ["product"],
|
||||
filterCondition: normalizeFilterCondition(debouncedFilterValue),
|
||||
option: {useCache: false, lastYear: false, decimalPoint: 0},
|
||||
};
|
||||
}, [debouncedFilterValue]);
|
||||
|
||||
const {data, isError, error} = useProductStatisticsQuery(req);
|
||||
|
||||
if(isError){
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
const purchaseRows =
|
||||
data?.statisticsDataMap?.purchase
|
||||
.filter((r) => r.rowLabel == `해당년도`)
|
||||
.map((row) => {
|
||||
return {
|
||||
...row,
|
||||
rowLabel: row.year,
|
||||
}
|
||||
}) ?? [];
|
||||
|
||||
const purchaseLGRows =
|
||||
data?.statisticsDataMap?.purchase_lg
|
||||
.filter((row) => row.rowLabel === `구입`)
|
||||
.map((row) => {
|
||||
return {
|
||||
...row,
|
||||
rowLabel: row.year,
|
||||
}
|
||||
}) ?? [];
|
||||
|
||||
const purchaseLGRowsWithGap = useMemo(() => {
|
||||
const withGap = withGapRow(purchaseLGRows, calcGapProductRow);
|
||||
return withTotalBaseRow("구입율",purchaseRows, withGap, calcTotalBaseRow);
|
||||
}, [purchaseRows, purchaseLGRows]);
|
||||
|
||||
const { includeProductKeys, productKeyMap, includeProducts } = useMemo(() => {
|
||||
const included = new Set<ProductKey>();
|
||||
|
||||
for (const row of purchaseLGRows) {
|
||||
for (const k of PRODUCT_KEYS) {
|
||||
if (!included.has(k) && toNumOr0((row as any)[k]) !== 0) included.add(k);
|
||||
}
|
||||
}
|
||||
|
||||
const keys = PRODUCT_KEYS.filter((k) => included.has(k));
|
||||
const map = Object.fromEntries(keys.map((k) => [k, PRODUCT_KEY_MAP[k]])) as Record<string, string>;
|
||||
const products = keys.map((k) => PRODUCT_KEY_MAP[k]);
|
||||
|
||||
return { includeProductKeys: keys, productKeyMap: map, includeProducts: products };
|
||||
}, [purchaseLGRows]);
|
||||
|
||||
const purchaseTableRows = useMemo<TopChartTableRow[]>(() => {
|
||||
return apiOwnShareOfTotalPurchase.map((row) => ({
|
||||
return purchaseLGRowsWithGap.map((row) => ({
|
||||
label: formatRowLabel(row.rowLabel),
|
||||
total: row.total,
|
||||
values: PRODUCT_KEYS.map((k) => (row as any)[k] ?? null),
|
||||
values: includeProductKeys.map((k) => (row as any)[k] ?? null),
|
||||
}));
|
||||
}, []);
|
||||
}, [purchaseLGRowsWithGap, includeProductKeys]);
|
||||
|
||||
const purchaseChartData = useMemo<GroupedOverlayBarData>(() => {
|
||||
const total: Array<Array<number>> = YEARS.map(() => Array(PRODUCT_KEYS.length).fill(0));
|
||||
const focus: Array<Array<number>> = YEARS.map(() => Array(PRODUCT_KEYS.length).fill(0));
|
||||
const total: Array<Array<number>> = YEARS.map(() => Array(includeProductKeys.length).fill(0));
|
||||
const focus: Array<Array<number>> = YEARS.map(() => Array(includeProductKeys.length).fill(0));
|
||||
|
||||
const yearIndexMap = new Map<string, number>(YEARS.map((y, i) => [y, i]));
|
||||
|
||||
const totalRows = apiTrendBuy.filter(
|
||||
(r) => YEARS.includes(r.year as any) && r.rowLabel.endsWith("년")
|
||||
);
|
||||
const lgRows = apiOwnShareOfTotalPurchase.filter(
|
||||
(r) => YEARS.includes(r.year as any) && r.rowLabel.endsWith("년")
|
||||
);
|
||||
|
||||
lgRows.forEach((r, ri) => {
|
||||
purchaseLGRows.forEach((r, ri) => {
|
||||
const yi = yearIndexMap.get(r.year);
|
||||
if (yi == null) return;
|
||||
|
||||
for (let ci = 0; ci < PRODUCT_KEYS.length; ci++) {
|
||||
const k = PRODUCT_KEYS[ci];
|
||||
const t = (totalRows[ri] as any)[k];
|
||||
for (let ci = 0; ci < includeProductKeys.length; ci++) {
|
||||
const k = includeProductKeys[ci];
|
||||
const t = (purchaseRows[ri] as any)[k];
|
||||
const v = (r as any)[k];
|
||||
|
||||
// total은 t 기준으로 체크해야 안전
|
||||
total[yi][ci] = Number(t);
|
||||
focus[yi][ci] = Number(v);
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
categories: PRODUCTS,
|
||||
categories: includeProducts,
|
||||
years: YEARS,
|
||||
totalName: "전체",
|
||||
focusName: "LG",
|
||||
total,
|
||||
focus,
|
||||
};
|
||||
}, []);
|
||||
}, [purchaseRows, purchaseLGRows, includeProducts, includeProductKeys]);
|
||||
|
||||
const description = useMemo(() => {
|
||||
return "Q. 다음 중 귀댁에서 보유하고 계신 가전/전자 제품은 무엇입니까? 모두 선택해주세요.<br/>" +
|
||||
@@ -78,35 +140,38 @@ const LGPurchasePage = () => {
|
||||
|
||||
useEffect(() => {
|
||||
registerExcelExporter(async () => {
|
||||
const excelHeader = buildHeader_ProductKeyMap(PRODUCT_KEY_MAP);
|
||||
const excelHeader = buildHeader_ProductKeyMap(productKeyMap);
|
||||
|
||||
await exportDashboardExcel({
|
||||
fileName: `${SITE_TITLE}(LG제품 구매율).xlsx`,
|
||||
sheetName: "LG제품 구매율",
|
||||
sections: [
|
||||
{ title: "LG제품 구매율", header: excelHeader, rows: apiOwnShareOfTotalPurchase, rowGap: 1 },
|
||||
{ title: "LG제품 구매율", header: excelHeader, rows: purchaseLGRowsWithGap, rowGap: 1 },
|
||||
],
|
||||
});
|
||||
});
|
||||
return () => registerExcelExporter(null);
|
||||
}, [registerExcelExporter]);
|
||||
}, [registerExcelExporter, purchaseLGRowsWithGap]);
|
||||
|
||||
return (
|
||||
<div>
|
||||
<TopChartTable
|
||||
title={"LG제품 구매율"}
|
||||
categories={[...PRODUCTS]}
|
||||
categories={[...includeProducts]}
|
||||
rows={purchaseTableRows}
|
||||
chart={
|
||||
<GroupedOverlayBar
|
||||
data={purchaseChartData}
|
||||
yearTotalColor={{ "24": "--chart-3-t", "25": "--chart-2-t", "26": "--chart-1-t" }}
|
||||
yearFocusColor={{ "24": "--chart-3", "25": "--chart-2", "26": "--chart-1" }}
|
||||
yearTotalColor={CHART_COLOR_BY_YEAR}
|
||||
yearFocusColor={CHART_FOCUS_COLOR_BY_YEAR}
|
||||
showFocusLabel={"last"}
|
||||
height={260}
|
||||
/>
|
||||
}
|
||||
signStyle={signStyleGapRow}
|
||||
/>
|
||||
|
||||
<BottomDescription description={description}/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user