mirror of
https://git.meshkee.com/BaloutPastry/dashboards.git
synced 2026-08-11 22:30:59 +04:30
195 lines
4.1 KiB
TypeScript
195 lines
4.1 KiB
TypeScript
export type Category = {
|
|
id: string
|
|
nameFa: string
|
|
nameEn: string
|
|
children: Category[]
|
|
}
|
|
|
|
export const initialCategories: Category[] = [
|
|
{
|
|
id: '1',
|
|
nameFa: 'شیرینی',
|
|
nameEn: 'Pastries',
|
|
children: [
|
|
{
|
|
id: '1-1',
|
|
nameFa: 'شیرینی خشک',
|
|
nameEn: 'Dry Cookies',
|
|
children: [
|
|
{
|
|
id: '1-1-1',
|
|
nameFa: 'شیرینی پسته',
|
|
nameEn: 'Pistachio Cookie',
|
|
children: [],
|
|
},
|
|
{
|
|
id: '1-1-2',
|
|
nameFa: 'شیرینی گردو',
|
|
nameEn: 'Walnut Cookie',
|
|
children: [],
|
|
},
|
|
],
|
|
},
|
|
{
|
|
id: '1-2',
|
|
nameFa: 'شیرینی تر',
|
|
nameEn: 'Soft Pastries',
|
|
children: [],
|
|
},
|
|
],
|
|
},
|
|
{
|
|
id: '2',
|
|
nameFa: 'کیک',
|
|
nameEn: 'Cakes',
|
|
children: [
|
|
{
|
|
id: '2-1',
|
|
nameFa: 'کیک شکلاتی',
|
|
nameEn: 'Chocolate Cake',
|
|
children: [],
|
|
},
|
|
{
|
|
id: '2-2',
|
|
nameFa: 'چیزکیک',
|
|
nameEn: 'Cheesecake',
|
|
children: [],
|
|
},
|
|
],
|
|
},
|
|
{
|
|
id: '3',
|
|
nameFa: 'نان',
|
|
nameEn: 'Bread',
|
|
children: [
|
|
{
|
|
id: '3-1',
|
|
nameFa: 'کروسان',
|
|
nameEn: 'Croissant',
|
|
children: [],
|
|
},
|
|
],
|
|
},
|
|
{
|
|
id: '4',
|
|
nameFa: 'باقلوا',
|
|
nameEn: 'Baklava',
|
|
children: [],
|
|
},
|
|
]
|
|
|
|
export function createCategory(nameFa: string, nameEn: string): Category {
|
|
return {
|
|
id: `${Date.now()}-${Math.random().toString(36).slice(2, 7)}`,
|
|
nameFa,
|
|
nameEn,
|
|
children: [],
|
|
}
|
|
}
|
|
|
|
export function addChildCategory(
|
|
categories: Category[],
|
|
parentId: string,
|
|
child: Category,
|
|
): Category[] {
|
|
return categories.map((category) => {
|
|
if (category.id === parentId) {
|
|
return { ...category, children: [...category.children, child] }
|
|
}
|
|
if (category.children.length > 0) {
|
|
return {
|
|
...category,
|
|
children: addChildCategory(category.children, parentId, child),
|
|
}
|
|
}
|
|
return category
|
|
})
|
|
}
|
|
|
|
export function removeCategory(
|
|
categories: Category[],
|
|
targetId: string,
|
|
): Category[] {
|
|
return categories
|
|
.filter((category) => category.id !== targetId)
|
|
.map((category) => ({
|
|
...category,
|
|
children: removeCategory(category.children, targetId),
|
|
}))
|
|
}
|
|
|
|
export type FlatCategoryOption = {
|
|
id: string
|
|
nameFa: string
|
|
nameEn: string
|
|
labelFa: string
|
|
labelEn: string
|
|
depth: number
|
|
}
|
|
|
|
export function flattenCategories(
|
|
categories: Category[],
|
|
prefix = '',
|
|
depth = 0,
|
|
): FlatCategoryOption[] {
|
|
return categories.flatMap((category) => {
|
|
const labelFa = prefix ? `${prefix} / ${category.nameFa}` : category.nameFa
|
|
const labelEn = prefix ? `${prefix} / ${category.nameEn}` : category.nameEn
|
|
return [
|
|
{
|
|
id: category.id,
|
|
nameFa: category.nameFa,
|
|
nameEn: category.nameEn,
|
|
labelFa,
|
|
labelEn,
|
|
depth,
|
|
},
|
|
...flattenCategories(category.children, labelFa, depth + 1),
|
|
]
|
|
})
|
|
}
|
|
|
|
export function findCategory(
|
|
categories: Category[],
|
|
id: string,
|
|
): Category | null {
|
|
for (const category of categories) {
|
|
if (category.id === id) return category
|
|
const found = findCategory(category.children, id)
|
|
if (found) return found
|
|
}
|
|
return null
|
|
}
|
|
|
|
export function findParentId(
|
|
categories: Category[],
|
|
targetId: string,
|
|
parentId: string | null = null,
|
|
): string | null | undefined {
|
|
for (const category of categories) {
|
|
if (category.id === targetId) return parentId
|
|
const found = findParentId(category.children, targetId, category.id)
|
|
if (found !== undefined) return found
|
|
}
|
|
return undefined
|
|
}
|
|
|
|
export function updateCategory(
|
|
categories: Category[],
|
|
targetId: string,
|
|
updates: { nameFa: string; nameEn: string },
|
|
): Category[] {
|
|
return categories.map((category) => {
|
|
if (category.id === targetId) {
|
|
return { ...category, ...updates }
|
|
}
|
|
if (category.children.length > 0) {
|
|
return {
|
|
...category,
|
|
children: updateCategory(category.children, targetId, updates),
|
|
}
|
|
}
|
|
return category
|
|
})
|
|
}
|