UNPKG

@websolutespa/payload-plugin-bowl

Version:

Bowl PayloadCms plugin of the BOM Repository

53 lines (52 loc) 1.91 kB
import { collectCategoryRedirects } from './redirect.service'; /** * afterCategoryChangeHook. */ export const afterCategoryChangeHook = async ({ doc, req, previousDoc, operation })=>{ if (operation === 'update') { if (previousDoc.slug && doc.slug) { const redirects = await collectCategoryRedirects(req, previousDoc, doc); console.log('redirects', redirects); } } return doc; }; /** * afterCategoryDeleteHook. */ export const afterCategoryDeleteHook = async ({ req, id, doc })=>{ if (doc.slug) { console.log('RedirectService.deleteCategory !!!'); // !!! todo find and redirect to parentCategory connected to a page // const parentCategory = } }; export const ROOT_ITEM = '_root'; export function getNewCategoriesFromChanges(categories, changes) { const newCategories = categories.map((x)=>({ ...x })); const root = changes[ROOT_ITEM]; let order = 0; const parseChildren = (parent)=>{ const parentIndex = parent.index; parent.children?.forEach((childIndex)=>{ order += 10; const category = newCategories.find((x)=>x.id === childIndex); const child = changes[childIndex]; // console.log('RouteService.getNewCategoriesFromChanges', 'parentIndex', parentIndex, 'childIndex', childIndex, child); if (category) { category.category = parentIndex === ROOT_ITEM ? undefined : parentIndex; category.order = order; } if (child) { parseChildren(child); } }); }; if (root) { parseChildren(root); } newCategories.sort((a, b)=>(a.order || 0) - (b.order || 0)); // console.log('RouteService.getNewCategoriesFromChanges', newCategories); return newCategories; } //# sourceMappingURL=category.service.js.map