Support for parent pages on folders

This commit is contained in:
DrMint 2024-03-16 07:57:42 +01:00
parent 8557df4753
commit 5fe9844341
5 changed files with 22 additions and 9 deletions

View File

@ -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<string, string>;
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 },

View File

@ -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<EndpointFolder["files"][number]>(({ relationTo, value }) => {
files?.flatMap<EndpointFolder["files"][number]>(({ relationTo, value }) => {
if (!isPayloadType(value)) {
return [];
}
@ -43,6 +45,7 @@ export const getBySlugEndpoint = createGetByEndpoint(
return [{ relationTo, value: convertPageToPreview(value) }];
}
}) ?? [],
parentPages: handleParentPages({ folders: parentFolders }),
};
},
3

View File

@ -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 = {

View File

@ -290,6 +290,7 @@ export interface Folder {
id: string;
slug: string;
icon?: string | null;
parentFolders?: (string | Folder)[] | null;
translations: {
language: string | Language;
name: string;

View File

@ -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",
});
});
}