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'
|
import { integrationsList } from './integrations/EmbedButton'
|
||||||
|
|
||||||
export const ShareContent = () => {
|
export const ShareContent = () => {
|
||||||
const { typebot, updateTypebot } = useTypebot()
|
const { typebot, updateOnBothTypebots } = useTypebot()
|
||||||
|
|
||||||
const handlePublicIdChange = (publicId: string) => {
|
const handlePublicIdChange = (publicId: string) => {
|
||||||
if (publicId === typebot?.publicId) return
|
if (publicId === typebot?.publicId) return
|
||||||
updateTypebot({ publicId })
|
updateOnBothTypebots({ publicId })
|
||||||
}
|
}
|
||||||
|
|
||||||
const publicId = typebot
|
const publicId = typebot
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ export const headerHeight = 56
|
|||||||
|
|
||||||
export const TypebotHeader = () => {
|
export const TypebotHeader = () => {
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const { typebot, updateTypebot, save, undo, redo, canUndo, canRedo } =
|
const { typebot, updateOnBothTypebots, save, undo, redo, canUndo, canRedo } =
|
||||||
useTypebot()
|
useTypebot()
|
||||||
const { setRightPanel } = useEditor()
|
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 () => {
|
const handlePreviewClick = async () => {
|
||||||
save().then()
|
save().then()
|
||||||
|
|||||||
@@ -55,6 +55,10 @@ const typebotContext = createContext<
|
|||||||
canRedo: boolean
|
canRedo: boolean
|
||||||
canUndo: boolean
|
canUndo: boolean
|
||||||
updateTypebot: (updates: UpdateTypebotPayload) => void
|
updateTypebot: (updates: UpdateTypebotPayload) => void
|
||||||
|
updateOnBothTypebots: (updates: {
|
||||||
|
publicId?: string
|
||||||
|
name?: string
|
||||||
|
}) => void
|
||||||
publishTypebot: () => void
|
publishTypebot: () => void
|
||||||
} & BlocksActions &
|
} & BlocksActions &
|
||||||
StepsActions &
|
StepsActions &
|
||||||
@@ -109,6 +113,20 @@ export const TypebotContext = ({
|
|||||||
window.removeEventListener('beforeunload', preventUserFromRefreshing)
|
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(
|
const hasUnsavedChanges = useMemo(
|
||||||
() =>
|
() =>
|
||||||
isDefined(typebot) &&
|
isDefined(typebot) &&
|
||||||
@@ -186,6 +204,13 @@ export const TypebotContext = ({
|
|||||||
const updateLocalTypebot = (updates: UpdateTypebotPayload) =>
|
const updateLocalTypebot = (updates: UpdateTypebotPayload) =>
|
||||||
localTypebot && setLocalTypebot({ ...localTypebot, ...updates })
|
localTypebot && setLocalTypebot({ ...localTypebot, ...updates })
|
||||||
|
|
||||||
|
const updateLocalPublishedTypebot = (updates: UpdateTypebotPayload) =>
|
||||||
|
publishedTypebot &&
|
||||||
|
setLocalPublishedTypebot({
|
||||||
|
...localPublishedTypebot,
|
||||||
|
...(updates as PublicTypebot),
|
||||||
|
})
|
||||||
|
|
||||||
const publishTypebot = async () => {
|
const publishTypebot = async () => {
|
||||||
if (!localTypebot) return
|
if (!localTypebot) return
|
||||||
const newLocalTypebot = { ...localTypebot }
|
const newLocalTypebot = { ...localTypebot }
|
||||||
@@ -198,23 +223,35 @@ export const TypebotContext = ({
|
|||||||
newLocalTypebot.publicId = newPublicId
|
newLocalTypebot.publicId = newPublicId
|
||||||
}
|
}
|
||||||
if (hasUnsavedChanges || !localPublishedTypebot) await saveTypebot()
|
if (hasUnsavedChanges || !localPublishedTypebot) await saveTypebot()
|
||||||
setIsPublishing(true)
|
|
||||||
if (localPublishedTypebot) {
|
if (localPublishedTypebot) {
|
||||||
const { error } = await updatePublishedTypebot(
|
await savePublishedTypebot({
|
||||||
localPublishedTypebot.id,
|
...parseTypebotToPublicTypebot(newLocalTypebot),
|
||||||
omit(parseTypebotToPublicTypebot(newLocalTypebot), 'id')
|
id: localPublishedTypebot.id,
|
||||||
)
|
})
|
||||||
setIsPublishing(false)
|
|
||||||
if (error) return toast({ title: error.name, description: error.message })
|
|
||||||
} else {
|
} else {
|
||||||
|
setIsPublishing(true)
|
||||||
const { data, error } = await createPublishedTypebot(
|
const { data, error } = await createPublishedTypebot(
|
||||||
omit(parseTypebotToPublicTypebot(newLocalTypebot), 'id')
|
omit(parseTypebotToPublicTypebot(newLocalTypebot), 'id')
|
||||||
)
|
)
|
||||||
setLocalPublishedTypebot(data)
|
setLocalPublishedTypebot(data)
|
||||||
setIsPublishing(false)
|
setIsPublishing(false)
|
||||||
if (error) return toast({ title: error.name, description: error.message })
|
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 (
|
return (
|
||||||
@@ -233,6 +270,7 @@ export const TypebotContext = ({
|
|||||||
isPublishing,
|
isPublishing,
|
||||||
isPublished,
|
isPublished,
|
||||||
updateTypebot: updateLocalTypebot,
|
updateTypebot: updateLocalTypebot,
|
||||||
|
updateOnBothTypebots,
|
||||||
...blocksActions(localTypebot as Typebot, setLocalTypebot),
|
...blocksActions(localTypebot as Typebot, setLocalTypebot),
|
||||||
...stepsAction(localTypebot as Typebot, setLocalTypebot),
|
...stepsAction(localTypebot as Typebot, setLocalTypebot),
|
||||||
...variablesAction(localTypebot as Typebot, setLocalTypebot),
|
...variablesAction(localTypebot as Typebot, setLocalTypebot),
|
||||||
|
|||||||
Reference in New Issue
Block a user