Created
October 16, 2024 16:20
-
-
Save oncode/bdfb8a7b982fe55cf8e26c8c9c9d3571 to your computer and use it in GitHub Desktop.
Lifecycles file for strapi entity introducing path, parent and children field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const updateChildPaths = async (parent: any) => { | |
const children = await strapi.entityService.findMany(context, { | |
filters: { parent: parent.id }, | |
}); | |
for (const child of children) { | |
let newPath = `${parent.path}/${child.slug}`; | |
if (parent.slug === homeSlug) { | |
newPath = `/${child.slug}`; | |
} | |
const updatedChild = await strapi.entityService.update(context, child.id, { | |
data: { path: newPath }, | |
}); | |
await updateChildPaths(updatedChild); // Recursively update child paths | |
} | |
}; | |
const context = 'api::page.page'; | |
const homeSlug = '_home_'; | |
export default { | |
async beforeCreate(event: any) { | |
const { data } = event.params; | |
if (data.slug) { | |
if (data.parent.connect.length > 0) { // Parent was added | |
const parent = await strapi.entityService.findOne(context, data.parent.connect[0].id); | |
if (parent.slug === homeSlug) { | |
data.path = `/${data.slug}`; | |
} else { | |
data.path = `${parent?.path}/${data.slug}`; | |
} | |
} else { // No parent was added | |
if (data.slug === homeSlug) { | |
data.path = `/`; | |
} else { | |
data.path = `/${data.slug}`; | |
} | |
} | |
} | |
}, | |
async beforeUpdate(event: any) { | |
const { data, where } = event.params; | |
if (data.slug) { | |
if (data.parent.connect.length > 0) { // Parent was added | |
const parent = await strapi.entityService.findOne(context, data.parent.connect[0].id); | |
if (parent?.slug === homeSlug) { | |
data.path = `/${data.slug}`; | |
} else { | |
data.path = `${parent?.path}/${data.slug}`; | |
} | |
} else if (data.parent.disconnect.length > 0) { // Parent was removed | |
if (data.slug === homeSlug) { | |
data.path = `/`; | |
} else { | |
data.path = `/${data.slug}`; | |
} | |
} else { // Parent hasn't changed | |
const existingData = await strapi.entityService.findOne(context, where.id, { | |
populate: ["parent"] | |
}); | |
if (existingData?.parent?.id) { | |
if (existingData.parent.slug === homeSlug) { | |
data.path = `/${data.slug}`; | |
} else { | |
data.path = `${existingData.parent.path}/${data.slug}`; | |
} | |
} else { | |
if (data.slug === homeSlug) { | |
data.path = `/`; | |
} else { | |
data.path = `/${data.slug}`; | |
} | |
} | |
if (data.children?.disconnect && data.children?.disconnect.length > 0) { | |
const disconnectedPages = await strapi.entityService.findMany(context, { | |
filters: { | |
id: { | |
$in: data.children.disconnect.map(child => child.id), | |
}, | |
} | |
}); | |
// update disconnected children | |
await Promise.all(disconnectedPages.map(async (page) => { | |
let newPath; | |
if (page.slug === homeSlug) { | |
newPath = `/`; | |
} else { | |
newPath = `/${page.slug}`; | |
} | |
const updatedChild = await strapi.entityService.update(context, page.id, { | |
data: { path: newPath }, | |
}); | |
await updateChildPaths(updatedChild); // Recursively update child paths | |
})) | |
} | |
} | |
} | |
}, | |
async afterUpdate(event: any) { | |
const { result } = event; // 'result' contains the updated entity | |
await updateChildPaths(result); // Update child paths recursively | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment