2
0

refactor: ️ Save both bots when updating name or publicId

This commit is contained in:
Baptiste Arnaud
2022-02-15 06:55:17 +01:00
parent 130f85e3c9
commit 2eee226a88
3 changed files with 50 additions and 12 deletions

View File

@ -7,11 +7,11 @@ import { EditableUrl } from './EditableUrl'
import { integrationsList } from './integrations/EmbedButton'
export const ShareContent = () => {
const { typebot, updateTypebot } = useTypebot()
const { typebot, updateOnBothTypebots } = useTypebot()
const handlePublicIdChange = (publicId: string) => {
if (publicId === typebot?.publicId) return
updateTypebot({ publicId })
updateOnBothTypebots({ publicId })
}
const publicId = typebot

View File

@ -13,7 +13,7 @@ export const headerHeight = 56
export const TypebotHeader = () => {
const router = useRouter()
const { typebot, updateTypebot, save, undo, redo, canUndo, canRedo } =
const { typebot, updateOnBothTypebots, save, undo, redo, canUndo, canRedo } =
useTypebot()
const { setRightPanel } = useEditor()
@ -25,7 +25,7 @@ export const TypebotHeader = () => {
})
}
const handleNameSubmit = (name: string) => updateTypebot({ name })
const handleNameSubmit = (name: string) => updateOnBothTypebots({ name })
const handlePreviewClick = async () => {
save().then()

View File

@ -55,6 +55,10 @@ const typebotContext = createContext<
canRedo: boolean
canUndo: boolean
updateTypebot: (updates: UpdateTypebotPayload) => void
updateOnBothTypebots: (updates: {
publicId?: string
name?: string
}) => void
publishTypebot: () => void
} & BlocksActions &
StepsActions &
@ -109,6 +113,20 @@ export const TypebotContext = ({
window.removeEventListener('beforeunload', preventUserFromRefreshing)
}
const savePublishedTypebot = async (newPublishedTypebot: PublicTypebot) => {
setIsPublishing(true)
const { error } = await updatePublishedTypebot(
newPublishedTypebot.id,
newPublishedTypebot
)
setIsPublishing(false)
if (error) return toast({ title: error.name, description: error.message })
mutate({
typebot: currentTypebotRef.current as Typebot,
publishedTypebot: newPublishedTypebot,
})
}
const hasUnsavedChanges = useMemo(
() =>
isDefined(typebot) &&
@ -186,6 +204,13 @@ export const TypebotContext = ({
const updateLocalTypebot = (updates: UpdateTypebotPayload) =>
localTypebot && setLocalTypebot({ ...localTypebot, ...updates })
const updateLocalPublishedTypebot = (updates: UpdateTypebotPayload) =>
publishedTypebot &&
setLocalPublishedTypebot({
...localPublishedTypebot,
...(updates as PublicTypebot),
})
const publishTypebot = async () => {
if (!localTypebot) return
const newLocalTypebot = { ...localTypebot }
@ -198,23 +223,35 @@ export const TypebotContext = ({
newLocalTypebot.publicId = newPublicId
}
if (hasUnsavedChanges || !localPublishedTypebot) await saveTypebot()
setIsPublishing(true)
if (localPublishedTypebot) {
const { error } = await updatePublishedTypebot(
localPublishedTypebot.id,
omit(parseTypebotToPublicTypebot(newLocalTypebot), 'id')
)
setIsPublishing(false)
if (error) return toast({ title: error.name, description: error.message })
await savePublishedTypebot({
...parseTypebotToPublicTypebot(newLocalTypebot),
id: localPublishedTypebot.id,
})
} else {
setIsPublishing(true)
const { data, error } = await createPublishedTypebot(
omit(parseTypebotToPublicTypebot(newLocalTypebot), 'id')
)
setLocalPublishedTypebot(data)
setIsPublishing(false)
if (error) return toast({ title: error.name, description: error.message })
mutate({ typebot: localTypebot })
}
mutate({ typebot: localTypebot })
}
const updateOnBothTypebots = async (updates: {
publicId?: string
name?: string
}) => {
updateLocalTypebot(updates)
await saveTypebot()
if (!localPublishedTypebot) return
updateLocalPublishedTypebot(updates)
await savePublishedTypebot({
...localPublishedTypebot,
...(updates as PublicTypebot),
})
}
return (
@ -233,6 +270,7 @@ export const TypebotContext = ({
isPublishing,
isPublished,
updateTypebot: updateLocalTypebot,
updateOnBothTypebots,
...blocksActions(localTypebot as Typebot, setLocalTypebot),
...stepsAction(localTypebot as Typebot, setLocalTypebot),
...variablesAction(localTypebot as Typebot, setLocalTypebot),