Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
1 line
4.2 KiB
Plaintext
1 line
4.2 KiB
Plaintext
{"version":3,"sources":["../../../src/folders/hooks/reparentChildFolder.ts"],"sourcesContent":["import type { CollectionAfterChangeHook, Payload } from '../../index.js'\n\nimport { extractID } from '../../utilities/extractID.js'\n\ntype Args = {\n folderCollectionSlug: string\n folderFieldName: string\n folderID: number | string\n parentIDToFind: number | string\n payload: Payload\n}\n\n/**\n * Determines if a child folder belongs to a parent folder by\n * recursively checking upwards through the folder hierarchy.\n */\nasync function isChildOfFolder({\n folderCollectionSlug,\n folderFieldName,\n folderID,\n parentIDToFind,\n payload,\n}: Args): Promise<boolean> {\n const parentFolder = await payload.findByID({\n id: folderID,\n collection: folderCollectionSlug,\n })\n\n const parentFolderID = parentFolder[folderFieldName]\n ? extractID(parentFolder[folderFieldName])\n : undefined\n\n if (!parentFolderID) {\n // made it to the root\n return false\n }\n\n if (parentFolderID === parentIDToFind) {\n // found match, would be cyclic\n return true\n }\n\n return isChildOfFolder({\n folderCollectionSlug,\n folderFieldName,\n folderID: parentFolderID,\n parentIDToFind,\n payload,\n })\n}\n\n/**\n * If a parent is moved into a child folder, we need to re-parent the child\n * \n * @example\n * \n * ```ts\n → F1\n → F2\n → F2A\n → F3\n\n Moving F1 → F2A becomes:\n\n → F2A\n → F1\n → F2\n → F3\n ```\n */\nexport const reparentChildFolder = ({\n folderFieldName,\n}: {\n folderFieldName: string\n}): CollectionAfterChangeHook => {\n return async ({ doc, previousDoc, req }) => {\n if (\n previousDoc[folderFieldName] !== doc[folderFieldName] &&\n doc[folderFieldName] &&\n req.payload.config.folders\n ) {\n const newParentFolderID = extractID(doc[folderFieldName])\n const isMovingToChild = newParentFolderID\n ? await isChildOfFolder({\n folderCollectionSlug: req.payload.config.folders.slug,\n folderFieldName,\n folderID: newParentFolderID,\n parentIDToFind: doc.id,\n payload: req.payload,\n })\n : false\n\n if (isMovingToChild) {\n // if the folder was moved into a child folder, the child folder needs\n // to be re-parented with the parent of the folder that was moved\n await req.payload.update({\n id: newParentFolderID,\n collection: req.payload.config.folders.slug,\n data: {\n [folderFieldName]: previousDoc[folderFieldName]\n ? extractID(previousDoc[folderFieldName])\n : null,\n },\n req,\n })\n }\n }\n }\n}\n"],"names":["extractID","isChildOfFolder","folderCollectionSlug","folderFieldName","folderID","parentIDToFind","payload","parentFolder","findByID","id","collection","parentFolderID","undefined","reparentChildFolder","doc","previousDoc","req","config","folders","newParentFolderID","isMovingToChild","slug","update","data"],"mappings":"AAEA,SAASA,SAAS,QAAQ,+BAA8B;AAUxD;;;CAGC,GACD,eAAeC,gBAAgB,EAC7BC,oBAAoB,EACpBC,eAAe,EACfC,QAAQ,EACRC,cAAc,EACdC,OAAO,EACF;IACL,MAAMC,eAAe,MAAMD,QAAQE,QAAQ,CAAC;QAC1CC,IAAIL;QACJM,YAAYR;IACd;IAEA,MAAMS,iBAAiBJ,YAAY,CAACJ,gBAAgB,GAChDH,UAAUO,YAAY,CAACJ,gBAAgB,IACvCS;IAEJ,IAAI,CAACD,gBAAgB;QACnB,sBAAsB;QACtB,OAAO;IACT;IAEA,IAAIA,mBAAmBN,gBAAgB;QACrC,+BAA+B;QAC/B,OAAO;IACT;IAEA,OAAOJ,gBAAgB;QACrBC;QACAC;QACAC,UAAUO;QACVN;QACAC;IACF;AACF;AAEA;;;;;;;;;;;;;;;;;;CAkBC,GACD,OAAO,MAAMO,sBAAsB,CAAC,EAClCV,eAAe,EAGhB;IACC,OAAO,OAAO,EAAEW,GAAG,EAAEC,WAAW,EAAEC,GAAG,EAAE;QACrC,IACED,WAAW,CAACZ,gBAAgB,KAAKW,GAAG,CAAC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