260129_01_ENH: 카테고리 파트 layout 및 route 구조 변경
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
"use client";
|
||||
|
||||
import {ReactNode, useState} from "react";
|
||||
import {ReactNode, useEffect, useMemo, useState} from "react";
|
||||
import { usePathname, useRouter } from "next/navigation";
|
||||
import {cn} from "@/lib/utils";
|
||||
import {SelectedMap} from "@/types/common";
|
||||
import {topMenus, totalSubMenus, categorySubMenus} from "@/config/menu.data";
|
||||
import {categoryTarget, totalFilters} from "@/config/filter.data";
|
||||
import {categoryTarget, TARGET_SLUG, totalFilters} from "@/config/filter.data";
|
||||
import {CategoryTargetData, CategoryTargetType} from "@/config/filter.types";
|
||||
import Header from "./Header";
|
||||
import SubMenu from "./SubMenu";
|
||||
@@ -13,6 +13,9 @@ import TotalFilter from "./TotalFilter";
|
||||
import CategoryFilter, {CategoryFilterValue} from "./CategoryFilter";
|
||||
import ExcelDownloadButton from "./ExcelDownloadButton";
|
||||
import {LayoutFilterProvider} from "./LayoutFilterContext";
|
||||
import {PRODUCTS_FILTER_ITEM} from "@/const/lgConst";
|
||||
import {ProductsFilterItem} from "@/types/lg";
|
||||
import {useLoading} from "@/hooks/useLoading";
|
||||
|
||||
type MainLayoutProps = {
|
||||
children: ReactNode;
|
||||
@@ -28,43 +31,79 @@ const buildDefaultTotalValue = (): SelectedMap => {
|
||||
return init;
|
||||
};
|
||||
|
||||
export const buildDefaultCategoryValue = (): CategoryFilterValue => {
|
||||
export const buildDefaultCategoryValue = (categoryFilterItems: ProductsFilterItem): CategoryFilterValue => {
|
||||
const initProduct = categoryFilterItems.items[0];
|
||||
const initType = {label:"보유", value:CategoryTargetType.PENETRATION}
|
||||
const firstTarget = categoryTarget.find((x) => x.type === initType.value)?.children?.[0]?.target;
|
||||
const initTarget = firstTarget ?? CategoryTargetData.PROFILE;
|
||||
|
||||
return {
|
||||
product: "냉장고",
|
||||
product: initProduct,
|
||||
type: initType,
|
||||
target: firstTarget ?? CategoryTargetData.PROFILE,
|
||||
target: initTarget,
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
const MainLayout = ({ children }: MainLayoutProps) => {
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
const { withLoadingAsync } = useLoading();
|
||||
|
||||
const isTotal = pathname.startsWith("/total");
|
||||
const activeTop = topMenus.find((m) => m.match?.(pathname)) ?? topMenus[0];
|
||||
const subMenus = isTotal ? totalSubMenus : categorySubMenus;
|
||||
const activeSub = subMenus.find((m) => m.path.startsWith(pathname)) ?? subMenus[0];
|
||||
const activeSub = subMenus.find((m) => pathname.startsWith(m.path)) ?? subMenus[0];
|
||||
|
||||
const [isTotalFilterOpen, setIsTotalFilterOpen] = useState(true);
|
||||
const [isCategoryFilterOpen, setIsCategoryFilterOpen] = useState(true);
|
||||
const isFilterOpen = isTotal ? isTotalFilterOpen : isCategoryFilterOpen;
|
||||
|
||||
const categoryFilterItems = PRODUCTS_FILTER_ITEM.find((m) => pathname.match?.(m.path)) ?? PRODUCTS_FILTER_ITEM[0];
|
||||
|
||||
const [totalValue, setTotalValue] = useState<SelectedMap>(() => buildDefaultTotalValue());
|
||||
const [categoryValue, setCategoryValue] = useState<CategoryFilterValue>(() => buildDefaultCategoryValue());
|
||||
const [categoryValue, setCategoryValue] = useState<CategoryFilterValue>(() => buildDefaultCategoryValue(categoryFilterItems));
|
||||
|
||||
const movePage = (path: string) => {
|
||||
router.push(path);
|
||||
};
|
||||
|
||||
const moveCategoryPage = (path: string) => {
|
||||
const slug = TARGET_SLUG[categoryValue.target] ?? "profile";
|
||||
const nextPath = `${path}/${slug}`;
|
||||
|
||||
if (pathname !== nextPath) {
|
||||
router.push(nextPath);
|
||||
}
|
||||
}
|
||||
|
||||
const toggleFilter = () => {
|
||||
if (isTotal) setIsTotalFilterOpen((v) => !v);
|
||||
else setIsCategoryFilterOpen((v) => !v);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
/*
|
||||
withLoadingAsync(async () => {
|
||||
setCategoryValue(prev => ({...prev, product: categoryFilterItems.items[0]}))
|
||||
});
|
||||
*/
|
||||
setCategoryValue(prev => ({...prev, product: categoryFilterItems.items[0]}))
|
||||
}, [categoryFilterItems]);
|
||||
|
||||
useEffect(() => {
|
||||
if (isTotal) return;
|
||||
|
||||
const base = activeSub?.path; // "/category/ha-kitchen"
|
||||
if (!base) return;
|
||||
|
||||
const slug = TARGET_SLUG[categoryValue.target] ?? "profile";
|
||||
const nextPath = `${base}/${slug}`;
|
||||
|
||||
if (pathname !== nextPath) {
|
||||
router.replace(nextPath);
|
||||
}
|
||||
}, [isTotal, categoryValue.target, activeSub?.path, pathname, router]);
|
||||
|
||||
return (
|
||||
<div className="min-h-screen flex flex-col bg-transparent">
|
||||
<LayoutFilterProvider
|
||||
@@ -82,7 +121,7 @@ const MainLayout = ({ children }: MainLayoutProps) => {
|
||||
<SubMenu
|
||||
subMenu={subMenus}
|
||||
activeSubId={activeSub.id}
|
||||
onNavigate={movePage}
|
||||
onNavigate={isTotal? movePage : moveCategoryPage}
|
||||
/>
|
||||
<div className="flex-1 min-h-0 flex flex-col pr-4">
|
||||
<div className="bg-card lg-tab-content flex-1 min-h-0">
|
||||
@@ -106,7 +145,7 @@ const MainLayout = ({ children }: MainLayoutProps) => {
|
||||
{isTotal ? (
|
||||
<TotalFilter filters={totalFilters} value={totalValue} onChange={setTotalValue} />
|
||||
) : (
|
||||
<CategoryFilter value={categoryValue} onChange={setCategoryValue} />
|
||||
<CategoryFilter filterItems={categoryFilterItems} value={categoryValue} onChange={setCategoryValue} />
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user