From 5fe98443414e983a178b21156258b3c0728b7254 Mon Sep 17 00:00:00 2001 From: DrMint <29893320+DrMint@users.noreply.github.com> Date: Sat, 16 Mar 2024 07:57:42 +0100 Subject: [PATCH] Support for parent pages on folders --- src/collections/Folders/Folders.ts | 15 +++++++++++++-- .../Folders/endpoints/getBySlugEndpoint.ts | 11 +++++++---- src/sdk.ts | 2 +- src/types/collections.ts | 1 + src/utils/endpoints.ts | 2 -- 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/collections/Folders/Folders.ts b/src/collections/Folders/Folders.ts index 18d1d4f..0484b89 100644 --- a/src/collections/Folders/Folders.ts +++ b/src/collections/Folders/Folders.ts @@ -1,4 +1,5 @@ import { CollectionGroups, Collections } from "../../constants"; +import { backPropagationField } from "../../fields/backPropagationField/backPropagationField"; import { iconField } from "../../fields/iconField/iconField"; import { rowField } from "../../fields/rowField/rowField"; import { slugField } from "../../fields/slugField/slugField"; @@ -19,6 +20,7 @@ const fields = { sectionsTranslationsName: "name", files: "files", icon: "icon", + parentFolders: "parentFolders", } as const satisfies Record; export const Folders = buildCollectionConfig({ @@ -27,14 +29,23 @@ export const Folders = buildCollectionConfig({ admin: { useAsTitle: fields.slug, group: CollectionGroups.Collections, - defaultColumns: [fields.slug, fields.translations, fields.sections, fields.files, fields.icon], + defaultColumns: [fields.slug, fields.translations, fields.parentFolders, fields.sections, fields.files, fields.icon], description: "Folders provide a way to structure our content. A folder can contain subfolders and/or files.", preview: ({ slug }) => `https://v3.accords-library.com/en/folders/${slug}`, }, endpoints: [getRootFoldersEndpoint, getBySlugEndpoint], fields: [ - rowField([slugField({ name: fields.slug }), iconField({ name: fields.icon })]), + rowField([ + slugField({ name: fields.slug }), + iconField({ name: fields.icon }), + backPropagationField({ + name: fields.parentFolders, + relationTo: Collections.Folders, + hasMany: true, + where: ({ id }) => ({ "sections.subfolders": { equals: id } }), + }), + ]), translatedFields({ name: fields.translations, admin: { useAsTitle: fields.translationsName }, diff --git a/src/collections/Folders/endpoints/getBySlugEndpoint.ts b/src/collections/Folders/endpoints/getBySlugEndpoint.ts index fc8f509..3ae7f8a 100644 --- a/src/collections/Folders/endpoints/getBySlugEndpoint.ts +++ b/src/collections/Folders/endpoints/getBySlugEndpoint.ts @@ -3,6 +3,7 @@ import { createGetByEndpoint } from "../../../endpoints/createGetByEndpoint"; import { EndpointFolder, EndpointFolderPreview } from "../../../sdk"; import { Folder, Language } from "../../../types/collections"; import { isDefined, isPayloadType } from "../../../utils/asserts"; +import { handleParentPages } from "../../../utils/endpoints"; import { convertCollectibleToPreview } from "../../Collectibles/endpoints/getBySlugEndpoint"; import { convertPageToPreview } from "../../Pages/endpoints/getBySlugEndpoint"; @@ -10,20 +11,21 @@ export const getBySlugEndpoint = createGetByEndpoint( Collections.Folders, "slug", (folder: Folder): EndpointFolder => { + const { sections, files, parentFolders } = folder; return { ...convertFolderToPreview(folder), sections: - folder.sections?.length === 1 + sections?.length === 1 ? { type: "single", subfolders: - folder.sections[0]?.subfolders?.filter(isPayloadType).map(convertFolderToPreview) ?? + sections[0]?.subfolders?.filter(isPayloadType).map(convertFolderToPreview) ?? [], } : { type: "multiple", sections: - folder.sections?.filter(isValidSection).map(({ translations, subfolders }) => ({ + sections?.filter(isValidSection).map(({ translations, subfolders }) => ({ translations: translations.map(({ language, name }) => ({ language: getLanguageId(language), name, @@ -32,7 +34,7 @@ export const getBySlugEndpoint = createGetByEndpoint( })) ?? [], }, files: - folder.files?.flatMap(({ relationTo, value }) => { + files?.flatMap(({ relationTo, value }) => { if (!isPayloadType(value)) { return []; } @@ -43,6 +45,7 @@ export const getBySlugEndpoint = createGetByEndpoint( return [{ relationTo, value: convertPageToPreview(value) }]; } }) ?? [], + parentPages: handleParentPages({ folders: parentFolders }), }; }, 3 diff --git a/src/sdk.ts b/src/sdk.ts index 7a34bbe..daf7b69 100644 --- a/src/sdk.ts +++ b/src/sdk.ts @@ -156,6 +156,7 @@ export type EndpointFolder = EndpointFolderPreview & { value: EndpointPagePreview; } )[]; + parentPages: ParentPage[]; }; export type EndpointHomeFolder = EndpointFolderPreview & { @@ -233,7 +234,6 @@ export type ParentPage = { slug: string; collection: Collections; translations: { language: string; name: string }[]; - tag: string; }; export type EndpointCollectiblePreview = { diff --git a/src/types/collections.ts b/src/types/collections.ts index 3c3b607..cbe4c28 100644 --- a/src/types/collections.ts +++ b/src/types/collections.ts @@ -290,6 +290,7 @@ export interface Folder { id: string; slug: string; icon?: string | null; + parentFolders?: (string | Folder)[] | null; translations: { language: string | Language; name: string; diff --git a/src/utils/endpoints.ts b/src/utils/endpoints.ts index 96cb836..7fa19a4 100644 --- a/src/utils/endpoints.ts +++ b/src/utils/endpoints.ts @@ -61,7 +61,6 @@ export const handleParentPages = ({ language: isPayloadType(language) ? language.id : language, name: title, // TODO: Use the entire pretitle + title + subtitle })), - tag: "collectible", }); }); } @@ -76,7 +75,6 @@ export const handleParentPages = ({ language: isPayloadType(language) ? language.id : language, name, })) ?? [], - tag: "folders", }); }); }