From b072163945e4d773531fc314cc8157f01c428098 Mon Sep 17 00:00:00 2001 From: Giacomo Baggio Date: Tue, 31 Aug 2021 10:49:07 +0200 Subject: [PATCH 1/3] Fix path validation --- frontend/pages/projects/index.tsx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/frontend/pages/projects/index.tsx b/frontend/pages/projects/index.tsx index 7ccac0d..984c638 100644 --- a/frontend/pages/projects/index.tsx +++ b/frontend/pages/projects/index.tsx @@ -577,11 +577,11 @@ export function getProjectCreatorPlugin( defaultValue: "/projects/", description: "The path to the page ( e.g. /projects/something )", validate(path: string) { - if (!path) { - return "Required."; + if (!path.startsWith("/projects/")) { + return "Path must start with /projects/"; } - if (!path.startsWith("/projects")) { - return "Path must start with /projects"; + if(!path.charAt(10)){ + return "Path must contain something after /projects/" } }, }, From 156325ffe7960edb9758979f831f7f97a7ac8de3 Mon Sep 17 00:00:00 2001 From: Giacomo Baggio Date: Tue, 31 Aug 2021 10:55:12 +0200 Subject: [PATCH 2/3] Revert "Merge branch 'projects' into projects-creator-plugin" This reverts commit 805d8181154461f2529ca7b2aa86f9005982783d, reversing changes made to b072163945e4d773531fc314cc8157f01c428098. --- frontend/graphql/createProject.graphql | 7 ++ frontend/graphql/generated.ts | 25 +++++ frontend/plugins/usePagePlugin.ts | 149 +++++++++++++++++++++++++ 3 files changed, 181 insertions(+) create mode 100644 frontend/graphql/createProject.graphql diff --git a/frontend/graphql/createProject.graphql b/frontend/graphql/createProject.graphql new file mode 100644 index 0000000..ce694db --- /dev/null +++ b/frontend/graphql/createProject.graphql @@ -0,0 +1,7 @@ +mutation CreateProject($input: createProjectInput) { + createProject(input: $input) { + project { + id + } + } +} diff --git a/frontend/graphql/generated.ts b/frontend/graphql/generated.ts index 9ec0faa..e3a2269 100644 --- a/frontend/graphql/generated.ts +++ b/frontend/graphql/generated.ts @@ -1853,6 +1853,22 @@ export type CreatePageMutation = ( )> } ); +export type CreateProjectMutationVariables = Exact<{ + input?: Maybe; +}>; + + +export type CreateProjectMutation = ( + { __typename?: 'Mutation' } + & { createProject?: Maybe<( + { __typename?: 'createProjectPayload' } + & { project?: Maybe<( + { __typename?: 'Project' } + & Pick + )> } + )> } +); + export type GetGlobalQueryVariables = Exact<{ [key: string]: never; }>; @@ -2053,6 +2069,15 @@ export const CreatePage = ` } } `; +export const CreateProject = ` + mutation CreateProject($input: createProjectInput) { + createProject(input: $input) { + project { + id + } + } +} + `; export const GetGlobal = ` query getGlobal { global { diff --git a/frontend/plugins/usePagePlugin.ts b/frontend/plugins/usePagePlugin.ts index a1b7fe1..1c5066a 100644 --- a/frontend/plugins/usePagePlugin.ts +++ b/frontend/plugins/usePagePlugin.ts @@ -3,6 +3,8 @@ import { SectionBlockData } from "@features/page"; import { CreatePage, CreatePageInput, + CreateProject, + CreateProjectInput, SaveChanges, UpdateMenuInput, UpdatePageInput, @@ -17,6 +19,7 @@ import { usePlugin, } from "tinacms"; import { assertNever } from "@utils"; +import { PreviewImageProps } from "pages/projects"; export interface PageData { id: string; @@ -116,7 +119,12 @@ export function usePagePlugin(data: Data): [Data, Form] { locales: router.locales || [], }); + const projectCreatorPlugin = getProjectCreatorPlugin({ + locales: router.locales || [], + }); + usePlugin(pageCreatorPlugin); + usePlugin(projectCreatorPlugin); return [formData, form]; } @@ -279,6 +287,132 @@ export function getPageCreatorPlugin( }; } +interface ProjectCreatorPluginOptions { + locales: string[]; +} + +export function getProjectCreatorPlugin( + options: ProjectCreatorPluginOptions +): ContentCreatorPlugin { + return { + __type: "content-creator", + name: "Add new project", + fields: [ + { + label: "Company name", + name: "companyName", + component: "text", + validate(name: string) { + if (!name) return "Required."; + }, + }, + { + label: "Path", + name: "path", + component: "text", + defaultValue: "/projects/", + description: "The path to the page ( e.g. /projects/something )", + validate(path: string) { + if (!path) { + return "Required."; + } + if (!path.startsWith("/projects")) { + return "Path must start with /projects"; + } + }, + }, + { + label: "Project type", + name: "projectType", + component: "text", + validate(type: string) { + if (!type) return "Required."; + }, + }, + { + label: "Description", + name: "description", + component: "textarea", + validate(description: string) { + if (!description) return "Required."; + }, + }, + { + label: "Locale", + name: "locale", + component: "select", + defaultValue: "en", + description: "Select a locale for this page", + // @ts-ignore + options: options.locales, + }, + { + label: "Link path", + name: "linkPath", + component: "text", + validate(linkPath: string) { + if (!linkPath) return "Required."; + if (!linkPath.startsWith("/")) { + return "Path should start with /"; + } + }, + }, + { + label: "Link label", + name: "linkLabel", + component: "text", + }, + { + label: "Image", + name: "image", + component: "image", + parse: (media) => media, + // @ts-ignore + uploadDir: () => "/", + previewSrc: (imageSrc: PreviewImageProps) => { + if (imageSrc.previewSrc === "") { + return "/images/default-image.png"; + } + return imageSrc.previewSrc; + }, + /*uploadDir={() => "/"} + previewSrc={(imageSrc) => { + if (imageSrc === "") { + return "/images/default-image.png"; + } + + return imageSrc; + }} + parse={(media) => { + return media as any; + }}*/ + }, + ], + onSubmit: async (values, cms) => { + const input = getProjectCreateInput(values); + + try { + const response = await cms.api.strapi.fetchGraphql(CreateProject, { + input, + }); + + if (response.data) { + // @ts-ignore + cms.alerts.success("Changes saved!"); + window.location.href = `/${values.locale}${values.path}`; + } else { + // @ts-ignore + cms.alerts.error("Error while saving changes"); + } + } catch (error) { + console.log(error); + // @ts-ignore + cms.alerts.error("Error while saving changes"); + } + }, + }; +} + export function getPageCreateInput( input: PageDataCreateInput ): CreatePageInput { @@ -291,6 +425,21 @@ export function getPageCreateInput( }; } +export function getProjectCreateInput( + input: ProjectDataCreateInput +): CreateProjectInput { + return { + data: { + companyName: input.companyName, + projectType: input.projectType, + linkLabel: input.linkLabel || "Discover more", + linkPath: input.linkPath, + description: input.description, + path: input.path, + }, + }; +} + function getMenuInput(data: MenuData): UpdateMenuInput { return { where: { id: data.id }, From 8e0f6b21c338c2678e833bf2559251c0b1af8be5 Mon Sep 17 00:00:00 2001 From: Giacomo Baggio Date: Tue, 31 Aug 2021 11:04:10 +0200 Subject: [PATCH 3/3] Update index.tsx --- frontend/pages/projects/index.tsx | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/frontend/pages/projects/index.tsx b/frontend/pages/projects/index.tsx index 984c638..c68e957 100644 --- a/frontend/pages/projects/index.tsx +++ b/frontend/pages/projects/index.tsx @@ -580,8 +580,8 @@ export function getProjectCreatorPlugin( if (!path.startsWith("/projects/")) { return "Path must start with /projects/"; } - if(!path.charAt(10)){ - return "Path must contain something after /projects/" + if (!path.charAt(10)) { + return "Path must contain something after /projects/"; } }, }, @@ -639,17 +639,6 @@ export function getProjectCreatorPlugin( } return imageSrc.previewSrc; }, - /*uploadDir={() => "/"} - previewSrc={(imageSrc) => { - if (imageSrc === "") { - return "/images/default-image.png"; - } - - return imageSrc; - }} - parse={(media) => { - return media as any; - }}*/ }, ], onSubmit: async (values, cms) => {