Added wordings

This commit is contained in:
DrMint 2024-03-02 20:38:38 +01:00
parent a5b636a858
commit d00bf52e40
4 changed files with 141 additions and 0 deletions

View File

@ -0,0 +1,81 @@
import { CollectionConfig } from "payload/types";
import { mustBeAdmin } from "../../accesses/collections/mustBeAdmin";
import { QuickFilters } from "../../components/QuickFilters";
import { CollectionGroups, Collections, LanguageCodes } from "../../constants";
import { rowField } from "../../fields/rowField/rowField";
import { translatedFields } from "../../fields/translatedFields/translatedFields";
import { afterOperationWebhook } from "../../hooks/afterOperationWebhook";
import { beforeDuplicateAddCopyTo } from "../../hooks/beforeDuplicateAddCopyTo";
import { buildCollectionConfig } from "../../utils/collectionConfig";
import { getAllEndpoint } from "./endpoints/getAllEndpoint";
const fields = {
name: "name",
translations: "translations",
translationsName: "name",
} as const satisfies Record<string, string>;
export const Wordings: CollectionConfig = buildCollectionConfig({
slug: Collections.Wordings,
labels: {
singular: "Wording",
plural: "Wordings",
},
defaultSort: fields.name,
admin: {
useAsTitle: fields.name,
defaultColumns: [fields.name, fields.translations],
group: CollectionGroups.Meta,
hooks: {
beforeDuplicate: beforeDuplicateAddCopyTo(fields.name),
},
components: {
BeforeListTable: [
() =>
QuickFilters({
slug: Collections.Wordings,
filterGroups: [
Object.entries(LanguageCodes).map(([key, value]) => ({
label: `${value}`,
filter: { where: { "translations.language": { not_equals: key } } },
})),
],
}),
],
},
},
access: {
create: mustBeAdmin,
delete: mustBeAdmin,
},
hooks: {
afterOperation: [afterOperationWebhook],
},
endpoints: [getAllEndpoint],
fields: [
{
name: fields.name,
type: "text",
required: true,
unique: true,
},
translatedFields({
name: fields.translations,
minRows: 1,
required: true,
interfaceName: "CategoryTranslations",
admin: {
useAsTitle: fields.translationsName,
},
fields: [
rowField([
{
name: fields.translationsName,
type: "textarea",
required: true,
},
]),
],
}),
],
});

View File

@ -0,0 +1,40 @@
import payload from "payload";
import { Collections } from "../../../constants";
import { EndpointWording } from "../../../sdk";
import { CollectionEndpoint } from "../../../types/payload";
import { isPayloadType } from "../../../utils/asserts";
export const getAllEndpoint: CollectionEndpoint = {
method: "get",
path: "/all",
handler: async (req, res) => {
if (!req.user) {
return res.status(403).send({
errors: [
{
message: "You are not allowed to perform this action.",
},
],
});
}
const wordings = (
await payload.find({
collection: Collections.Wordings,
sort: "id",
pagination: false,
})
).docs;
const result: EndpointWording[] = wordings.map(({ translations, name }) => ({
name,
translations:
translations?.map(({ language, name }) => ({
language: isPayloadType(language) ? language.id : language,
name,
})) ?? [],
}));
res.status(200).json(result);
},
};

View File

@ -0,0 +1,18 @@
import { AfterOperationHook } from "payload/dist/collections/config/types";
export const afterOperationWebhook: AfterOperationHook = async ({
result,
collection,
operation,
}) => {
if (["create", "delete", "deleteByID", "update", "updateByID"].includes(operation)) {
const url = `${process.env.WEB_HOOK_URI}/collection-operation`;
await fetch(url, {
headers: {
Authorization: `Bearer ${process.env.WEB_HOOK_TOKEN}`,
Collection: collection.slug,
},
});
}
return result;
};

View File

@ -26,6 +26,7 @@ import { VideosChannels } from "./collections/VideosChannels/VideosChannels";
import { Weapons } from "./collections/Weapons/Weapons";
import { WeaponsGroups } from "./collections/WeaponsGroups/WeaponsGroups";
import { WeaponsThumbnails } from "./collections/WeaponsThumbnails/WeaponsThumbnails";
import { Wordings } from "./collections/Wordings/Wordings";
import { Icon } from "./components/Icon";
import { Logo } from "./components/Logo";
import { Collections } from "./constants";
@ -70,6 +71,7 @@ export default buildConfig({
Tags,
TagsGroups,
Images,
Wordings
],
db: mongooseAdapter({
url: process.env.MONGODB_URI ?? "mongodb://mongo:27017/payload",