🚸 (editor) Improve typebot updatedAt detection
Make sure the database is the single source of truth
This commit is contained in:
@ -169,7 +169,11 @@ export const TypebotProvider = ({
|
|||||||
if (dequal(omit(typebot, 'updatedAt'), omit(typebotToSave, 'updatedAt')))
|
if (dequal(omit(typebot, 'updatedAt'), omit(typebotToSave, 'updatedAt')))
|
||||||
return
|
return
|
||||||
setIsSavingLoading(true)
|
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)
|
setIsSavingLoading(false)
|
||||||
if (error) {
|
if (error) {
|
||||||
showToast({ title: error.name, description: error.message })
|
showToast({ title: error.name, description: error.message })
|
||||||
|
@ -2,7 +2,7 @@ import { Typebot } from 'models'
|
|||||||
import { sendRequest } from 'utils'
|
import { sendRequest } from 'utils'
|
||||||
|
|
||||||
export const updateTypebotQuery = async (id: string, typebot: Typebot) =>
|
export const updateTypebotQuery = async (id: string, typebot: Typebot) =>
|
||||||
sendRequest({
|
sendRequest<{ typebot: Typebot }>({
|
||||||
url: `/api/typebots/${id}`,
|
url: `/api/typebots/${id}`,
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
body: typebot,
|
body: typebot,
|
||||||
|
@ -5,7 +5,7 @@ export const patchTypebotQuery = async (
|
|||||||
id: string,
|
id: string,
|
||||||
typebot: Partial<Typebot>
|
typebot: Partial<Typebot>
|
||||||
) =>
|
) =>
|
||||||
sendRequest({
|
sendRequest<{ typebot: Typebot }>({
|
||||||
url: `/api/typebots/${id}`,
|
url: `/api/typebots/${id}`,
|
||||||
method: 'PATCH',
|
method: 'PATCH',
|
||||||
body: typebot,
|
body: typebot,
|
||||||
|
@ -94,11 +94,12 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
resultsTablePreferences: data.resultsTablePreferences ?? undefined,
|
resultsTablePreferences: data.resultsTablePreferences ?? undefined,
|
||||||
} satisfies Prisma.TypebotUpdateInput
|
} satisfies Prisma.TypebotUpdateInput
|
||||||
|
|
||||||
const { count } = await prisma.typebot.updateMany({
|
const updatedTypebot = await prisma.typebot.update({
|
||||||
where: { id: typebotId },
|
where: { id: typebotId },
|
||||||
data: updates,
|
data: updates,
|
||||||
})
|
})
|
||||||
return res.send({ count })
|
|
||||||
|
return res.send({ typebot: updatedTypebot })
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.method === 'PATCH') {
|
if (req.method === 'PATCH') {
|
||||||
@ -109,11 +110,11 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
})
|
})
|
||||||
if (!typebot) return res.status(404).send({ message: 'Typebot not found' })
|
if (!typebot) return res.status(404).send({ message: 'Typebot not found' })
|
||||||
const data = typeof req.body === 'string' ? JSON.parse(req.body) : req.body
|
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 },
|
where: { id: typebotId },
|
||||||
data,
|
data,
|
||||||
})
|
})
|
||||||
return res.send({ typebots })
|
return res.send({ typebot: updatedTypebot })
|
||||||
}
|
}
|
||||||
return methodNotAllowed(res)
|
return methodNotAllowed(res)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user