update 30/12
This commit is contained in:
33
src/lib/resolveArticlePage.ts
Normal file
33
src/lib/resolveArticlePage.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
// src/lib/articlePage.ts
|
||||
import { categories } from "../data/categories";
|
||||
|
||||
export type ArticleResult =
|
||||
| { type: "article_home"; data: any }
|
||||
| { type: "article_category"; data: any }
|
||||
| { type: "article_detail"; data: { slug: string } };
|
||||
|
||||
export function resolveArticlePage(slug: string): ArticleResult | null {
|
||||
const url = "/" + slug;
|
||||
|
||||
// HOME
|
||||
if (url === "/tin-tuc") {
|
||||
return { type: "article_home", data: null };
|
||||
}
|
||||
|
||||
// CATEGORY
|
||||
const cats = categories.article.all_category.article;
|
||||
for (const parent of cats) {
|
||||
if (parent.url === url) {
|
||||
return { type: "article_category", data: parent };
|
||||
}
|
||||
|
||||
for (const child of parent.children ?? []) {
|
||||
if (child.url === url) {
|
||||
return { type: "article_category", data: child };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// DETAIL
|
||||
return { type: "article_detail", data: { slug } };
|
||||
}
|
||||
32
src/lib/resolveProductPage.ts
Normal file
32
src/lib/resolveProductPage.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
// hoanghapc/src/lib/productPage.ts
|
||||
import { categories } from "../data/categories";
|
||||
import { productList } from "../data/product-list";
|
||||
|
||||
export type ProductResult =
|
||||
| { type: "product_category"; data: any }
|
||||
| { type: "product_detail"; data: any };
|
||||
|
||||
export function resolveProductPage(slug: string): ProductResult | null {
|
||||
const url = "/" + slug;
|
||||
|
||||
// CATEGORY
|
||||
for (const parent of categories.product.all_category) {
|
||||
if (parent.url === url) {
|
||||
return { type: "product_category", data: parent };
|
||||
}
|
||||
|
||||
for (const child of parent.children ?? []) {
|
||||
if (child.url === url) {
|
||||
return { type: "product_category", data: child };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// DETAIL
|
||||
const product = productList.find(p => p.productUrl === url);
|
||||
if (product) {
|
||||
return { type: "product_detail", data: product };
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -1,77 +1,17 @@
|
||||
// src/lib/slugMap.ts
|
||||
import { categories } from "@/data/categories";
|
||||
|
||||
export type PageType =
|
||||
| "article_home"
|
||||
| "article_category"
|
||||
| "article_detail"
|
||||
| "product_category"
|
||||
| "product_detail";
|
||||
import { resolveArticlePage } from "./resolveArticlePage";
|
||||
import { resolveProductPage } from "./resolveProductPage";
|
||||
|
||||
export type SlugResult =
|
||||
| { type: "article_home" }
|
||||
| { type: "article_category"; slug: string }
|
||||
| { type: "article_detail"; slug: string }
|
||||
| { type: "product_category"; slug: string }
|
||||
| { type: "product_detail"; slug: string };
|
||||
| ReturnType<typeof resolveArticlePage>
|
||||
| ReturnType<typeof resolveProductPage>;
|
||||
|
||||
export function findBySlug(rawSlug: string): SlugResult {
|
||||
const slug = normalizeSlug(rawSlug);
|
||||
const url = "/" + slug;
|
||||
export function findBySlug(slug?: string): SlugResult | null {
|
||||
if (!slug) return null;
|
||||
|
||||
/* 1. ARTICLE HOME */
|
||||
if (url === "/tin-tuc") {
|
||||
return { type: "article_home" };
|
||||
}
|
||||
// PRODUCT
|
||||
const product = resolveProductPage(slug);
|
||||
if (product) return product;
|
||||
|
||||
/* 2. ARTICLE CATEGORY */
|
||||
const articleCats = categories.article.all_category.article;
|
||||
for (const parent of articleCats) {
|
||||
if (parent.url === url) {
|
||||
return { type: "article_category", slug };
|
||||
}
|
||||
|
||||
for (const child of parent.children ?? []) {
|
||||
if (child.url === url) {
|
||||
return { type: "article_category", slug };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 3. PRODUCT CATEGORY */
|
||||
const productCats = categories.product.all_category;
|
||||
for (const cat of productCats) {
|
||||
if (cat.url === url) {
|
||||
return { type: "product_category", slug };
|
||||
}
|
||||
|
||||
for (const child of cat.children ?? []) {
|
||||
if (child.url === url) {
|
||||
return { type: "product_category", slug };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 4. PRODUCT DETAIL */
|
||||
if (isProductDetailSlug(slug)) {
|
||||
return { type: "product_detail", slug };
|
||||
}
|
||||
|
||||
/* 5. ARTICLE DETAIL (fallback) */
|
||||
return { type: "article_detail", slug };
|
||||
}
|
||||
|
||||
/* ===============================
|
||||
* HELPERS
|
||||
* =============================== */
|
||||
function normalizeSlug(slug: string) {
|
||||
return slug.replace(/^\/+/, "").trim();
|
||||
}
|
||||
|
||||
function isProductDetailSlug(slug: string) {
|
||||
return (
|
||||
slug.startsWith("hhpc-") ||
|
||||
slug.startsWith("pc-") ||
|
||||
slug.startsWith("man-hinh-")
|
||||
);
|
||||
}
|
||||
// ARTICLE
|
||||
return resolveArticlePage(slug);
|
||||
}
|
||||
Reference in New Issue
Block a user