Skip to content

Commit

Permalink
🚸 (editor) Improve typebot updatedAt detection
Browse files Browse the repository at this point in the history
Make sure the database is the single source of truth
  • Loading branch information
baptisteArno committed Feb 16, 2023
1 parent 618eb8a commit 4a0dd0b
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,11 @@ export const TypebotProvider = ({
if (dequal(omit(typebot, 'updatedAt'), omit(typebotToSave, 'updatedAt')))
return
setIsSavingLoading(true)
const { error } = await updateTypebotQuery(typebotToSave.id, typebotToSave)
const { data, error } = await updateTypebotQuery(
typebotToSave.id,
typebotToSave
)
if (data?.typebot) setLocalTypebot({ ...data.typebot })
setIsSavingLoading(false)
if (error) {
showToast({ title: error.name, description: error.message })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Typebot } from 'models'
import { sendRequest } from 'utils'

export const updateTypebotQuery = async (id: string, typebot: Typebot) =>
sendRequest({
sendRequest<{ typebot: Typebot }>({
url: `/api/typebots/${id}`,
method: 'PUT',
body: typebot,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const patchTypebotQuery = async (
id: string,
typebot: Partial<Typebot>
) =>
sendRequest({
sendRequest<{ typebot: Typebot }>({
url: `/api/typebots/${id}`,
method: 'PATCH',
body: typebot,
Expand Down
9 changes: 5 additions & 4 deletions apps/builder/src/pages/api/typebots/[typebotId].ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,12 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
resultsTablePreferences: data.resultsTablePreferences ?? undefined,
} satisfies Prisma.TypebotUpdateInput

const { count } = await prisma.typebot.updateMany({
const updatedTypebot = await prisma.typebot.update({
where: { id: typebotId },
data: updates,
})
return res.send({ count })

return res.send({ typebot: updatedTypebot })
}

if (req.method === 'PATCH') {
Expand All @@ -109,11 +110,11 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
})
if (!typebot) return res.status(404).send({ message: 'Typebot not found' })
const data = typeof req.body === 'string' ? JSON.parse(req.body) : req.body
const typebots = await prisma.typebot.updateMany({
const updatedTypebot = await prisma.typebot.update({
where: { id: typebotId },
data,
})
return res.send({ typebots })
return res.send({ typebot: updatedTypebot })
}
return methodNotAllowed(res)
}
Expand Down

2 comments on commit 4a0dd0b

@vercel
Copy link

@vercel vercel bot commented on 4a0dd0b Feb 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deployment failed with the following error:

Was unable to schedule a deployment.

@vercel
Copy link

@vercel vercel bot commented on 4a0dd0b Feb 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deployment failed with the following error:

Creating the Deployment Timed Out.

Please sign in to comment.