260225_01_ENH: 실제 Data 통계 API와 연결 및 일부 페이지 축소
This commit is contained in:
@@ -9,62 +9,141 @@ import {
|
||||
type TopChartTableRow,
|
||||
} from "@/components/lge-dashboard";
|
||||
import {
|
||||
LAST_YEAR,
|
||||
CHART_COLOR_BY_YEAR,
|
||||
CHART_FOCUS_COLOR_BY_YEAR,
|
||||
LIFE_STAGE_COLUMNS,
|
||||
LIFE_STAGE_GROUPS,
|
||||
PRODUCT_KEY_MAP,
|
||||
PRODUCT_KEYS,
|
||||
PRODUCTS, SITE_TITLE,
|
||||
SITE_TITLE,
|
||||
YEARS
|
||||
} from "@/const/lgConst";
|
||||
import {useEffect, useMemo} from "react";
|
||||
import {apiLifeStageByProduct, apiOwnShareOfTotalPenetration, apiTrendCategories} from "@/config/sample.data";
|
||||
import LifeStageDiv from "@/components/lge-dashboard/combo/LifeStageDiv";
|
||||
import { useLayoutFilter } from "@/components/layout/LayoutFilterContext";
|
||||
import { formatRowLabel } from "@/lib/lg-dashboard";
|
||||
import {useLayoutFilter} from "@/components/layout/LayoutFilterContext";
|
||||
import {
|
||||
calcGapProductRow, calcTotalBaseRow,
|
||||
formatRowLabel,
|
||||
normalizeFilterCondition,
|
||||
withGapRow, withTotalBaseRow
|
||||
} from "@/lib/lg-dashboard";
|
||||
import {
|
||||
buildHeader_LifeStageColumns,
|
||||
buildHeader_ProductKeyMap,
|
||||
exportDashboardExcel,
|
||||
Section
|
||||
} from "@/lib/lg-export-excel";
|
||||
import {norm} from "@/lib/utils";
|
||||
import {LifeStageGroup, LifeStageRow} from "@/types/lg";
|
||||
import {toNumOr0} from "@/lib/utils";
|
||||
import {LifeStageGroup, LifeStageRow, ProductKey, StatisticsRequest} from "@/types/lg";
|
||||
import {useDebouncedValue} from "@/hooks/useDebouncedValue";
|
||||
import {SelectedMap} from "@/types/common";
|
||||
import {useBannerStatisticsQuery, useProductStatisticsQuery} from "@/hooks/useLgStatisticsQuery";
|
||||
|
||||
const LGPenetrationPage = () => {
|
||||
const { filterValue, registerExcelExporter } = useLayoutFilter();
|
||||
console.log(filterValue);
|
||||
const {filterValue, registerExcelExporter} = useLayoutFilter();
|
||||
const debouncedFilterValue = useDebouncedValue<SelectedMap>(filterValue as SelectedMap, 500);
|
||||
|
||||
//TODO: LG가 0인 제품은 제외시키기
|
||||
const req = useMemo<StatisticsRequest>(() => {
|
||||
return {
|
||||
variableIdMap: {penetration: "KQA1", penetration_lg: "KQA3_3_LG"},
|
||||
bannerIdList: ["product"],
|
||||
filterCondition: normalizeFilterCondition(debouncedFilterValue),
|
||||
option: {useCache: true, lastYear: false, decimalPoint: 0},
|
||||
};
|
||||
}, [debouncedFilterValue]);
|
||||
|
||||
const req_lifestage = useMemo<StatisticsRequest>(() => {
|
||||
const filter = {...debouncedFilterValue, "":""}
|
||||
|
||||
return {
|
||||
variableIdMap: {lifestage_lg: "KQA3_3_LG"},
|
||||
bannerIdList: ["product|lifeStage"],
|
||||
filterCondition: normalizeFilterCondition(filter),
|
||||
option: {useCache: true, lastYear: true, decimalPoint: 0},
|
||||
};
|
||||
}, [debouncedFilterValue]);
|
||||
|
||||
const {data, isError, error} = useProductStatisticsQuery(req);
|
||||
const {
|
||||
data: data_lifestage,
|
||||
isError: isError_lifestage,
|
||||
error: error_lifestage
|
||||
} = useBannerStatisticsQuery(req_lifestage);
|
||||
|
||||
if(isError){
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
if(isError_lifestage){
|
||||
console.error(error_lifestage);
|
||||
}
|
||||
|
||||
const penetrationRows =
|
||||
data?.statisticsDataMap?.penetration
|
||||
.filter((r) => r.rowLabel == `보유`)
|
||||
.map((row) => {
|
||||
return {
|
||||
...row,
|
||||
rowLabel: row.year,
|
||||
}
|
||||
}) ?? [];
|
||||
|
||||
const penetrationLGRows =
|
||||
data?.statisticsDataMap?.penetration_lg
|
||||
.filter((row) => row.rowLabel === `보유`)
|
||||
.map((row) => {
|
||||
return {
|
||||
...row,
|
||||
rowLabel: row.year,
|
||||
}
|
||||
}) ?? [];
|
||||
|
||||
const lifeStageRows =
|
||||
data_lifestage?.statisticsDataMap?.lifestage_lg
|
||||
.filter((r) => r.rowLabel == `보유`) ?? [];
|
||||
|
||||
const { includeProductKeys, productKeyMap, includeProducts } = useMemo(() => {
|
||||
const included = new Set<ProductKey>();
|
||||
|
||||
for (const row of penetrationLGRows) {
|
||||
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 };
|
||||
}, [penetrationLGRows]);
|
||||
|
||||
const penetrationLGRowsWithGap = useMemo(() => {
|
||||
const withGap = withGapRow(penetrationLGRows, calcGapProductRow);
|
||||
return withTotalBaseRow("보급률",penetrationRows, withGap, calcTotalBaseRow);
|
||||
}, [penetrationRows, penetrationLGRows]);
|
||||
|
||||
const productTableRows = useMemo<TopChartTableRow[]>(() => {
|
||||
return apiOwnShareOfTotalPenetration.map((row) => ({
|
||||
return penetrationLGRowsWithGap.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),
|
||||
}));
|
||||
}, []);
|
||||
}, [penetrationLGRowsWithGap, includeProductKeys]);
|
||||
|
||||
const productChartData = 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 = apiTrendCategories.filter(
|
||||
(r) => YEARS.includes(r.year as any) && r.rowLabel.endsWith("년")
|
||||
);
|
||||
const lgRows = apiOwnShareOfTotalPenetration.filter(
|
||||
(r) => YEARS.includes(r.year as any) && r.rowLabel.endsWith("년")
|
||||
);
|
||||
|
||||
lgRows.forEach((r, ri) => {
|
||||
penetrationLGRows.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 = (penetrationRows[ri] as any)[k];
|
||||
const v = (r as any)[k];
|
||||
|
||||
total[yi][ci] = Number(t);
|
||||
@@ -73,47 +152,51 @@ const LGPenetrationPage = () => {
|
||||
});
|
||||
|
||||
return {
|
||||
categories: PRODUCTS,
|
||||
categories: includeProducts,
|
||||
years: YEARS,
|
||||
totalName: "전체",
|
||||
focusName: "LG",
|
||||
total,
|
||||
focus,
|
||||
};
|
||||
}, []);
|
||||
}, [penetrationRows, penetrationLGRows, includeProductKeys, includeProducts]);
|
||||
|
||||
const lifeStageRowsByGroup:LifeStageGroup[] = useMemo(() => {
|
||||
const pick = (label: string) => apiLifeStageByProduct.find((r) => norm(r.rowLabel) === norm(label));
|
||||
const lifeStageRowsByGroup: LifeStageGroup[] = useMemo(() => {
|
||||
if(lifeStageRows.length == 0){
|
||||
return [];
|
||||
}
|
||||
|
||||
const lifeStageData = lifeStageRows[0];
|
||||
|
||||
return LIFE_STAGE_GROUPS.map((g) => {
|
||||
const rows: LifeStageRow[] = g.items.map((label) => {
|
||||
const r = pick(label);
|
||||
const rows: LifeStageRow[] = g.items.map((key) => {
|
||||
const label = PRODUCT_KEY_MAP[key];
|
||||
|
||||
return {
|
||||
item: label,
|
||||
total: r?.total ?? "",
|
||||
single: r?.single ?? "",
|
||||
noKids: r?.noKids ?? "",
|
||||
preschoolKids: r?.preschoolKids ?? "",
|
||||
schoolKids: r?.schoolKids ?? "",
|
||||
adultKids: r?.adultKids ?? "",
|
||||
independentSenior: r?.independentSenior ?? "",
|
||||
total: lifeStageData[`${key}`] ?? "",
|
||||
"KLIFEstAGE#1": lifeStageData[`${key}|KLIFEstAGE#1`] ?? "",
|
||||
"KLIFEstAGE#2": lifeStageData[`${key}|KLIFEstAGE#2`] ?? "",
|
||||
"KLIFEstAGE#3": lifeStageData[`${key}|KLIFEstAGE#3`] ?? "",
|
||||
"KLIFEstAGE#4": lifeStageData[`${key}|KLIFEstAGE#4`] ?? "",
|
||||
"KLIFEstAGE#5": lifeStageData[`${key}|KLIFEstAGE#5`] ?? "",
|
||||
"KLIFEstAGE#6": lifeStageData[`${key}|KLIFEstAGE#6`] ?? "",
|
||||
};
|
||||
});
|
||||
|
||||
return { title: g.title, rows };
|
||||
return {title: g.title, rows};
|
||||
});
|
||||
}, []);
|
||||
}, [lifeStageRows]);
|
||||
|
||||
const description = useMemo(() => {
|
||||
return "Q. 다음 중 귀댁에서 보유하고 계신 가전/전자 제품은 무엇입니까? 모두 선택해주세요.<br/>" +
|
||||
"* 구독/렌탈/선물/경품/아파트 내 빌트인 제품을 모두 포함하여 선택해주세요.<br/>" +
|
||||
"Q. 보유하고 계신 (제품)의 브랜드는 어떻게 되십니까?";
|
||||
"* 구독/렌탈/선물/경품/아파트 내 빌트인 제품을 모두 포함하여 선택해주세요.<br/>" +
|
||||
"Q. 보유하고 계신 (제품)의 브랜드는 어떻게 되십니까?";
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
registerExcelExporter(async () => {
|
||||
const excelHeader = buildHeader_ProductKeyMap(PRODUCT_KEY_MAP);
|
||||
const excelHeader = buildHeader_ProductKeyMap(productKeyMap);
|
||||
const lifeStageHeader = buildHeader_LifeStageColumns(LIFE_STAGE_COLUMNS);
|
||||
|
||||
type AnyRow = Record<string, any>;
|
||||
@@ -122,7 +205,7 @@ const LGPenetrationPage = () => {
|
||||
{
|
||||
title: "LG제품 보급률",
|
||||
header: excelHeader,
|
||||
rows: apiOwnShareOfTotalPenetration as AnyRow[],
|
||||
rows: penetrationLGRowsWithGap as AnyRow[],
|
||||
rowGap: 1,
|
||||
},
|
||||
|
||||
@@ -141,28 +224,29 @@ const LGPenetrationPage = () => {
|
||||
});
|
||||
});
|
||||
return () => registerExcelExporter(null);
|
||||
}, [registerExcelExporter]);
|
||||
}, [registerExcelExporter, penetrationLGRowsWithGap, lifeStageRowsByGroup, productKeyMap]);
|
||||
|
||||
return (
|
||||
<div>
|
||||
<TopChartTable
|
||||
title={"LG제품 보급률"}
|
||||
categories={[...PRODUCTS]}
|
||||
categories={[...includeProducts]}
|
||||
rows={productTableRows}
|
||||
chart={
|
||||
<GroupedOverlayBar
|
||||
data={productChartData}
|
||||
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} />
|
||||
<BottomDescription description={description}/>
|
||||
|
||||
<LifeStageDiv lifeStageRowsByGroup={lifeStageRowsByGroup} />
|
||||
<LifeStageDiv lifeStageRowsByGroup={lifeStageRowsByGroup}/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user