Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreCrb committed Feb 2, 2021
1 parent 6e90a44 commit 2dbe21e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 31 deletions.
53 changes: 32 additions & 21 deletions src/components/ModalComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ interface Props {
loading: boolean
setModalLoading: (value: boolean) => void
showUserProjectList: () => void
accessToken: string
}

interface UpdateProject {
Expand All @@ -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', {
Expand Down Expand Up @@ -111,24 +113,26 @@ const ModalComponent = (props: Props) => {
}
}

const publishPublicProject = async (e: UpdateProject) => {
const publishPublicProject = async (e: UpdateProject, token: string) => {
props.setModalLoading(true)

e.public = !e.public
const data = {
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,
Expand Down Expand Up @@ -311,23 +315,30 @@ const ModalComponent = (props: Props) => {
<AccordionIcon />
</AccordionHeader>
<AccordionPanel pb={4}>
<Switch
color="teal"
id="projectPublic"
size="md"
defaultIsChecked={e.public}
onChange={() => publishPublicProject(e)}
/>
{e.public && (
<Button
variantColor="teal"
size="sm"
ml={5}
onClick={() => saveScreenshot(e)}
>
Take a screenshot
</Button>
)}
<Flex align="center" justify="center">
<Text mr={3}>
{e.public ? 'Public' : 'Private'}
</Text>
<Switch
color="teal"
id="projectPublic"
size="md"
defaultIsChecked={e.public}
onChange={() =>
publishPublicProject(e, props.accessToken)
}
/>
{e.public && (
<Button
variantColor="teal"
size="sm"
ml={5}
onClick={() => saveScreenshot(e)}
>
Take a screenshot
</Button>
)}
</Flex>
</AccordionPanel>
</AccordionItem>
</Accordion>
Expand Down
38 changes: 28 additions & 10 deletions src/pages/api/project/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,38 @@ 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,
},
data: {
markup: projectData.markup,
public: projectData.public,
updatedAt: ts.toISOString(),
accessToken: projectData.accessToken,
},
})
res.status(201)
res.json({
success: 'Update project to database successfully !',

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(),
},
})
res.status(201)
res.json({
success: 'Update project to database successfully !',
})
} else {
res.status(500)
res.json({ error: 'Sorry this is not your project' })
}
} catch (e) {
res.status(500)
res.json({ error: 'Sorry unable to update project to database' })
Expand Down
3 changes: 3 additions & 0 deletions src/pages/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down Expand Up @@ -241,6 +242,7 @@ const EditorPage = (props: {
loading={modalLoading}
setModalLoading={setModalLoading}
showUserProjectList={showUserProjectList}
accessToken={session?.accessToken as string}
/>

<Box
Expand Down Expand Up @@ -299,6 +301,7 @@ const EditorPage = (props: {
loading={modalLoading}
setModalLoading={setModalLoading}
showUserProjectList={showUserProjectList}
accessToken={session?.accessToken as string}
/>
<Box
maxH="calc(100vh - 3rem)"
Expand Down

0 comments on commit 2dbe21e

Please sign in to comment.