2
0

🐛 Fix inconsistent updatedAt when timezone is different fro…

This commit is contained in:
Baptiste Arnaud
2024-05-04 15:18:40 +02:00
parent 86441a5337
commit ad4d1a1000
4 changed files with 40 additions and 11 deletions

View File

@ -253,7 +253,7 @@ const RightElements = ({
}: StackProps & { isResultsDisplayed: boolean }) => {
const router = useRouter()
const { t } = useTranslate()
const { typebot, currentUserMode, save } = useTypebot()
const { typebot, currentUserMode, save, isSavingLoading } = useTypebot()
const {
setRightPanel,
rightPanel,
@ -264,7 +264,7 @@ const RightElements = ({
const handlePreviewClick = async () => {
setStartPreviewAtGroup(undefined)
setStartPreviewAtEvent(undefined)
save().then()
await save()
setRightPanel(RightPanel.PREVIEW)
}
@ -282,7 +282,7 @@ const RightElements = ({
<Button
colorScheme="gray"
onClick={handlePreviewClick}
isLoading={isNotDefined(typebot)}
isLoading={isNotDefined(typebot) || isSavingLoading}
leftIcon={<PlayIcon />}
size="sm"
iconSpacing={{ base: 0, xl: 2 }}

View File

@ -4,6 +4,7 @@ import { isDefined } from '@typebot.io/lib'
export interface Actions<T extends { updatedAt: Date }> {
set: (newPresent: T | ((current: T) => T) | undefined) => void
setUpdateDate: (updateDate: Date) => void
undo: () => void
redo: () => void
flush: () => void
@ -107,6 +108,16 @@ export const useUndo = <T extends { updatedAt: Date }>(
[history, params?.isReadOnly]
)
const setUpdateDate = useCallback(
(updatedAt: Date) => {
set((current) => ({
...current,
updatedAt,
}))
},
[set]
)
const flush = useCallback(() => {
if (params?.isReadOnly) return
setHistory({
@ -116,5 +127,8 @@ export const useUndo = <T extends { updatedAt: Date }>(
})
}, [params?.isReadOnly])
return [history.present, { set, undo, redo, flush, canUndo, canRedo }]
return [
history.present,
{ set, undo, redo, flush, setUpdateDate, canUndo, canRedo },
]
}

View File

@ -174,7 +174,15 @@ export const TypebotProvider = ({
const [
localTypebot,
{ redo, undo, flush, canRedo, canUndo, set: setLocalTypebot },
{
redo,
undo,
flush,
canRedo,
canUndo,
set: setLocalTypebot,
setUpdateDate,
},
] = useUndo<TypebotV6>(undefined, {
isReadOnly,
onUndo: (t) => {
@ -216,24 +224,33 @@ export const TypebotProvider = ({
const typebotToSave = {
...localTypebot,
...updates,
updatedAt: new Date(),
}
if (dequal(omit(typebot, 'updatedAt'), omit(typebotToSave, 'updatedAt')))
return
const newParsedTypebot = typebotV6Schema.parse({ ...typebotToSave })
setLocalTypebot(newParsedTypebot)
try {
await updateTypebot({
const {
typebot: { updatedAt },
} = await updateTypebot({
typebotId: newParsedTypebot.id,
typebot: newParsedTypebot,
})
setUpdateDate(updatedAt)
} catch {
setLocalTypebot({
...localTypebot,
})
}
},
[isReadOnly, localTypebot, setLocalTypebot, typebot, updateTypebot]
[
isReadOnly,
localTypebot,
setLocalTypebot,
setUpdateDate,
typebot,
updateTypebot,
]
)
useAutoSave(

View File

@ -120,8 +120,7 @@ export const updateTypebot = authenticatedProcedure
if (
typebot.updatedAt &&
new Date(existingTypebot?.updatedAt).getTime() >
typebot.updatedAt.getTime()
existingTypebot.updatedAt.getTime() > typebot.updatedAt.getTime()
)
throw new TRPCError({
code: 'CONFLICT',
@ -197,7 +196,6 @@ export const updateTypebot = authenticatedProcedure
}),
isClosed: typebot.isClosed,
whatsAppCredentialsId: typebot.whatsAppCredentialsId ?? undefined,
updatedAt: typebot.updatedAt,
},
})