83 lines
2.0 KiB
TypeScript
83 lines
2.0 KiB
TypeScript
import { clsx, type ClassValue } from "clsx";
|
|
import { twMerge } from "tailwind-merge";
|
|
|
|
export interface IPost {
|
|
images: string[];
|
|
title: string;
|
|
price: number;
|
|
category: string;
|
|
condition: string;
|
|
brand?: string;
|
|
description: string;
|
|
tags: string[];
|
|
sku: string;
|
|
location?: string;
|
|
status?: boolean;
|
|
id: string;
|
|
publist_id?: string;
|
|
}
|
|
|
|
export interface ISyncItem {
|
|
title: string;
|
|
price: number;
|
|
el: HTMLElement;
|
|
}
|
|
|
|
export function cn(...inputs: ClassValue[]) {
|
|
return twMerge(clsx(inputs));
|
|
}
|
|
|
|
export function mapToIPost(raw: any): IPost {
|
|
let images: string[] = [];
|
|
|
|
if (Array.isArray(raw?.listImages)) {
|
|
images = raw.listImages
|
|
.map((img: any) =>
|
|
typeof img === "string"
|
|
? img
|
|
: `${(import.meta.env.VITE_API_URL as string).replaceAll("api", "")}${
|
|
img.url
|
|
}`
|
|
)
|
|
.slice(0, 10); // chỉ lấy tối đa 10 ảnh
|
|
}
|
|
|
|
// xử lý package_contain
|
|
let packageContainText = "";
|
|
if (Array.isArray(raw?.package_contain)) {
|
|
packageContainText = raw.package_contain
|
|
.map((item: string) => `- ${item}`)
|
|
.join("\n");
|
|
} else if (typeof raw?.package_contain === "string") {
|
|
packageContainText = raw.package_contain;
|
|
}
|
|
|
|
// build description rõ ràng, không có thụt đầu dòng
|
|
const descriptionLines = [
|
|
raw.description || "",
|
|
raw.code || "",
|
|
raw.name || "",
|
|
(typeof raw?.category === "string" ? raw.category : raw?.category?.name) ||
|
|
"",
|
|
packageContainText,
|
|
]
|
|
.filter(Boolean) // loại bỏ cái rỗng
|
|
.join("\n\n"); // cách 1 dòng giữa các block
|
|
|
|
return {
|
|
images,
|
|
title: `${raw.name} - ${raw.code}`,
|
|
price: raw.price,
|
|
category: "electronics & computers",
|
|
condition: "new",
|
|
brand: typeof raw.manufactor === "string" ? raw.manufactor : raw?.name,
|
|
description: descriptionLines,
|
|
tags: raw.tags || [],
|
|
sku: raw.code,
|
|
location: raw.location || "Sydney, Australia",
|
|
status: false,
|
|
id: raw.id,
|
|
publist_id: raw.listingId,
|
|
};
|
|
}
|