refactor: ♿️ Save both bots when updating name or publicId
This commit is contained in:
@ -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
|
||||
|
@ -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()
|
||||
|
@ -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,24 +223,36 @@ 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 })
|
||||
}
|
||||
}
|
||||
|
||||
const updateOnBothTypebots = async (updates: {
|
||||
publicId?: string
|
||||
name?: string
|
||||
}) => {
|
||||
updateLocalTypebot(updates)
|
||||
await saveTypebot()
|
||||
if (!localPublishedTypebot) return
|
||||
updateLocalPublishedTypebot(updates)
|
||||
await savePublishedTypebot({
|
||||
...localPublishedTypebot,
|
||||
...(updates as PublicTypebot),
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<typebotContext.Provider
|
||||
@ -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),
|
||||
|
Reference in New Issue
Block a user