From ae0ccedd438b983f3ff0830ed3c32b0a1c4fec61 Mon Sep 17 00:00:00 2001 From: DrMint <29893320+DrMint@users.noreply.github.com> Date: Sat, 9 Mar 2024 23:43:12 +0100 Subject: [PATCH] Changed tag type and added webhooks --- src/collections/Currencies/Currencies.ts | 4 +++ .../Folders/endpoints/getBySlugEndpoint.ts | 2 +- src/collections/Languages/Languages.ts | 4 +++ src/sdk.ts | 9 +++---- src/utils/endpoints.ts | 27 ++++++++++++++----- 5 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/collections/Currencies/Currencies.ts b/src/collections/Currencies/Currencies.ts index 87cae7b..41b3692 100644 --- a/src/collections/Currencies/Currencies.ts +++ b/src/collections/Currencies/Currencies.ts @@ -1,6 +1,7 @@ import { text } from "payload/dist/fields/validations"; import { mustBeAdmin } from "../../accesses/collections/mustBeAdmin"; import { CollectionGroups, Collections } from "../../constants"; +import { afterOperationWebhook } from "../../hooks/afterOperationWebhook"; import { buildCollectionConfig } from "../../utils/collectionConfig"; import { getAllEndpoint } from "./endpoints/getAllEndpoint"; import { importFromStrapi } from "./endpoints/importFromStrapi"; @@ -24,6 +25,9 @@ export const Currencies = buildCollectionConfig({ group: CollectionGroups.Meta, }, access: { create: mustBeAdmin, update: mustBeAdmin }, + hooks: { + afterOperation: [afterOperationWebhook], + }, endpoints: [importFromStrapi, getAllEndpoint], timestamps: false, fields: [ diff --git a/src/collections/Folders/endpoints/getBySlugEndpoint.ts b/src/collections/Folders/endpoints/getBySlugEndpoint.ts index 936cf61..a8f0067 100644 --- a/src/collections/Folders/endpoints/getBySlugEndpoint.ts +++ b/src/collections/Folders/endpoints/getBySlugEndpoint.ts @@ -44,7 +44,7 @@ export const getBySlugEndpoint = createGetByEndpoint( } }) ?? [], }; - } + }, 3 ); export const convertFolderToPreview = ({ diff --git a/src/collections/Languages/Languages.ts b/src/collections/Languages/Languages.ts index d8e3b59..9d94b37 100644 --- a/src/collections/Languages/Languages.ts +++ b/src/collections/Languages/Languages.ts @@ -2,6 +2,7 @@ import { text } from "payload/dist/fields/validations"; import { mustBeAdmin } from "../../accesses/collections/mustBeAdmin"; import { publicAccess } from "../../accesses/publicAccess"; import { CollectionGroups, Collections } from "../../constants"; +import { afterOperationWebhook } from "../../hooks/afterOperationWebhook"; import { buildCollectionConfig } from "../../utils/collectionConfig"; import { getAllEndpoint } from "./endpoints/getAllEndpoint"; import { importFromStrapi } from "./endpoints/importFromStrapi"; @@ -26,6 +27,9 @@ export const Languages = buildCollectionConfig({ pagination: { defaultLimit: 100 }, }, access: { create: mustBeAdmin, update: mustBeAdmin, read: publicAccess }, + hooks: { + afterOperation: [afterOperationWebhook], + }, timestamps: false, endpoints: [importFromStrapi, getAllEndpoint], fields: [ diff --git a/src/sdk.ts b/src/sdk.ts index 237018a..f72b509 100644 --- a/src/sdk.ts +++ b/src/sdk.ts @@ -225,12 +225,11 @@ export type EndpointTag = { language: string; name: string; }[]; - group: string; }; export type EndpointTagsGroup = { slug: string; - icon?: string; + icon: string; translations: { language: string; name: string; @@ -243,7 +242,7 @@ export type EndpointPagePreview = { type: PageType; thumbnail?: PayloadImage; authors: EndpointRecorder[]; - tagGroups: TagGroup[]; + tagGroups: EndpointTagsGroup[]; translations: { language: string; pretitle?: string; @@ -284,7 +283,7 @@ export type EndpointCollectiblePreview = { subtitle?: string; description?: RichTextContent; }[]; - tagGroups: TagGroup[]; + tagGroups: EndpointTagsGroup[]; status: "draft" | "published"; releaseDate?: string; languages: string[]; @@ -350,8 +349,6 @@ export type EndpointCollectible = EndpointCollectiblePreview & { parentPages: ParentPage[]; }; -export type TagGroup = { slug: string; icon: string; values: string[] }; - export type TableOfContentEntry = { prefix: string; title: string; diff --git a/src/utils/endpoints.ts b/src/utils/endpoints.ts index ae6408d..96cb836 100644 --- a/src/utils/endpoints.ts +++ b/src/utils/endpoints.ts @@ -1,25 +1,40 @@ import { Collections } from "../constants"; -import { EndpointRecorder, ParentPage, TagGroup } from "../sdk"; +import { EndpointRecorder, EndpointTag, EndpointTagsGroup, ParentPage } from "../sdk"; import { Collectible, Folder, Recorder, Tag } from "../types/collections"; import { isPayloadArrayType, isPayloadType, isValidPayloadImage } from "./asserts"; -export const convertTagsToGroups = (tags: (string | Tag)[] | null | undefined): TagGroup[] => { +export const convertTagsToGroups = ( + tags: (string | Tag)[] | null | undefined +): EndpointTagsGroup[] => { if (!isPayloadArrayType(tags)) { return []; } - const groups: TagGroup[] = []; + const groups: EndpointTagsGroup[] = []; - tags.forEach(({ group, slug }) => { + tags.forEach(({ translations, slug, group }) => { if (isPayloadType(group)) { const existingGroup = groups.find((existingGroup) => existingGroup.slug === group.slug); + + const endpointTag: EndpointTag = { + slug, + translations: translations.map(({ language, name }) => ({ + language: isPayloadType(language) ? language.id : language, + name, + })), + }; + if (existingGroup) { - existingGroup.values.push(slug); + existingGroup.tags.push(endpointTag); } else { groups.push({ slug: group.slug, icon: group.icon ?? "material-symbols:category-outline", - values: [slug], + tags: [endpointTag], + translations: group.translations.map(({ language, name }) => ({ + language: isPayloadType(language) ? language.id : language, + name, + })), }); } }