From 23ec63c770a2df96fa18b712c3ef048944ac010c Mon Sep 17 00:00:00 2001 From: Pierre Date: Tue, 2 Feb 2021 16:59:00 +0100 Subject: [PATCH] fix --- src/components/ModalComponent.tsx | 53 +++++++++++++++++++------------ src/pages/api/project/update.ts | 31 ++++++++++++++---- src/pages/editor.tsx | 3 ++ 3 files changed, 60 insertions(+), 27 deletions(-) diff --git a/src/components/ModalComponent.tsx b/src/components/ModalComponent.tsx index e1fe160312..03e09af355 100644 --- a/src/components/ModalComponent.tsx +++ b/src/components/ModalComponent.tsx @@ -44,6 +44,7 @@ interface Props { loading: boolean setModalLoading: (value: boolean) => void showUserProjectList: () => void + accessToken: string } interface UpdateProject { @@ -57,12 +58,13 @@ const ModalComponent = (props: Props) => { const toast = useToast() const [loadingAdd, setLoadingAdd] = useState(false) - const updateProject = async (e: UpdateProject) => { + const updateProject = async (e: UpdateProject, token: string) => { props.setModalLoading(true) let bodyData = { project: { id: e.id, public: e.public, + accessToken: token, }, } const response = await fetch('/api/project/update', { @@ -111,7 +113,7 @@ const ModalComponent = (props: Props) => { } } - const publishPublicProject = async (e: UpdateProject) => { + const publishPublicProject = async (e: UpdateProject, token: string) => { props.setModalLoading(true) e.public = !e.public @@ -119,16 +121,18 @@ const ModalComponent = (props: Props) => { id: e.id, public: e.public, projectName: e.projectName, + accessToken: token, } - const projectUpdated = await updateProject(data) + const projectUpdated = await updateProject(data, token) props.setModalLoading(false) if (projectUpdated) { toast({ title: 'The project visibility has been updated', - description: 'The project has been updated successfully', + description: + 'The project must now be validated by an admin to publish it in the gallery', status: 'success', duration: 9000, isClosable: true, @@ -311,23 +315,30 @@ const ModalComponent = (props: Props) => { - publishPublicProject(e)} - /> - {e.public && ( - - )} + + + {e.public ? 'Public' : 'Private'} + + + publishPublicProject(e, props.accessToken) + } + /> + {e.public && ( + + )} + diff --git a/src/pages/api/project/update.ts b/src/pages/api/project/update.ts index d438f8757c..90935d5f14 100644 --- a/src/pages/api/project/update.ts +++ b/src/pages/api/project/update.ts @@ -8,16 +8,35 @@ export default async function UpdateProject( let ts = new Date() try { const { project: projectData } = req.body - await prisma.project.update({ + + const projects = await prisma.session.findUnique({ where: { - id: projectData.id, + accessToken: projectData.accessToken, }, - data: { - markup: projectData.markup, - public: projectData.public, - updatedAt: ts.toISOString(), + }) + + const userProject = await prisma.project.findUnique({ + where: { + id: projectData.id, }, }) + + if (userProject?.userId === projects?.userId) { + await prisma.project.update({ + where: { + id: projectData.id, + }, + data: { + markup: projectData.markup, + public: projectData.public, + updatedAt: ts.toISOString(), + }, + }) + } else { + res.status(500) + res.json({ error: 'Sorry this is not your project' }) + } + res.status(201) res.json({ success: 'Update project to database successfully !', diff --git a/src/pages/editor.tsx b/src/pages/editor.tsx index 43ff231516..1418307315 100644 --- a/src/pages/editor.tsx +++ b/src/pages/editor.tsx @@ -58,6 +58,7 @@ const EditorPage = (props: { id: props.id, projectName: props.projectName, validated: props.validated, + accessToken: session?.accessToken as string, }, } const response = await fetch('/api/project/update', { @@ -241,6 +242,7 @@ const EditorPage = (props: { loading={modalLoading} setModalLoading={setModalLoading} showUserProjectList={showUserProjectList} + accessToken={session?.accessToken as string} />