2
0

🐛 (editor) inconsistency in route change auto save

This commit is contained in:
Baptiste Arnaud
2022-10-12 07:42:32 +02:00
parent d1cc9180c8
commit b46d35214d
4 changed files with 23 additions and 16 deletions

View File

@ -159,7 +159,7 @@ export const TypebotContext = ({
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [typebot])
const saveTypebot = async (options?: { disableMutation: boolean }) => {
const saveTypebot = async () => {
if (!currentTypebotRef.current || !typebot) return
const typebotToSave = { ...currentTypebotRef.current }
if (dequal(omit(typebot, 'updatedAt'), omit(typebotToSave, 'updatedAt')))
@ -171,12 +171,11 @@ export const TypebotContext = ({
showToast({ title: error.name, description: error.message })
return
}
if (!options?.disableMutation)
mutate({
typebot: typebotToSave,
publishedTypebot,
webhooks: webhooks ?? [],
})
mutate({
typebot: typebotToSave,
publishedTypebot,
webhooks: webhooks ?? [],
})
window.removeEventListener('beforeunload', preventUserFromRefreshing)
}
@ -208,10 +207,9 @@ export const TypebotContext = ({
)
useEffect(() => {
const save = () => saveTypebot({ disableMutation: true })
Router.events.on('routeChangeStart', save)
Router.events.on('routeChangeStart', saveTypebot)
return () => {
Router.events.off('routeChangeStart', save)
Router.events.off('routeChangeStart', saveTypebot)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [typebot, publishedTypebot, webhooks])

View File

@ -16,7 +16,10 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const workspaceId = req.query.workspaceId as string | undefined
if (!workspaceId) return badRequest(res, 'workspaceId is required')
const workspace = await prisma.workspace.findFirst({
where: { id: workspaceId, members: { some: { userId: user.id } } },
where:
user.email === process.env.ADMIN_EMAIL
? undefined
: { id: workspaceId, members: { some: { userId: user.id } } },
select: { plan: true },
})
if (!workspace) return forbidden(res)

View File

@ -97,10 +97,16 @@ export const getAllResults = async (workspaceId: string, typebotId: string) => {
let lastResultId: string | undefined = undefined
do {
const query = stringify({ limit: 200, lastResultId, workspaceId })
const { data } = await sendRequest<{ results: ResultWithAnswers[] }>({
url: `/api/typebots/${typebotId}/results?${query}`,
method: 'GET',
})
const { data, error } = await sendRequest<{ results: ResultWithAnswers[] }>(
{
url: `/api/typebots/${typebotId}/results?${query}`,
method: 'GET',
}
)
if (error) {
console.error(error)
break
}
results.push(...(data?.results ?? []))
lastResultId = results[results.length - 1]?.id as string | undefined
if (data?.results.length === 0) hasMore = false

View File

@ -17,7 +17,7 @@ export const badRequest = (res: NextApiResponse, customMessage?: any) =>
res.status(400).json({ message: customMessage ?? 'Bad Request' })
export const forbidden = (res: NextApiResponse, customMessage?: string) =>
res.status(403).json({ message: customMessage ?? 'Bad Request' })
res.status(403).json({ message: customMessage ?? 'Forbidden' })
export const initMiddleware =
(