2
0

🐛 (typebotLink) Make sure to update all linked bot variable values

This commit is contained in:
Baptiste Arnaud
2022-12-12 10:29:52 +01:00
parent 578e41c09f
commit 656d1e3cad
3 changed files with 25 additions and 7 deletions

View File

@ -11,7 +11,10 @@ test('folders navigation should work', async ({ page }) => {
await createFolderButton.click()
await page.click('text="New folder"')
await page.fill('input[value="New folder"]', 'My folder #1')
await page.press('input[value="My folder #1"]', 'Enter'),
await Promise.all([
page.waitForResponse((resp) => resp.request().method() === 'PATCH'),
page.press('input[value="My folder #1"]', 'Enter'),
])
await page.click('li:has-text("My folder #1")')
await expect(page.locator('h1:has-text("My folder #1")')).toBeVisible()
await createFolderButton.click()

View File

@ -54,7 +54,9 @@ export const AnswersProvider = ({
setResultValues((resultValues) => {
const updatedVariables = [
...resultValues.variables.filter((v) =>
serializedNewVariables.every((variable) => variable.id !== v.id)
serializedNewVariables.every(
(variable) => variable.id !== v.id || variable.name !== v.name
)
),
...serializedNewVariables,
].filter((variable) => isDefined(variable.value)) as VariableWithValue[]

View File

@ -3,13 +3,14 @@ import { safeStringify } from '@/features/variables'
import { sendEventToParent } from '@/utils/chat'
import { Log } from 'db'
import { Edge, PublicTypebot, Typebot } from 'models'
import React, {
import {
createContext,
ReactNode,
useContext,
useEffect,
useState,
} from 'react'
import { isDefined } from 'utils'
export type LinkedTypebot = Pick<
PublicTypebot | Typebot,
@ -78,16 +79,28 @@ export const TypebotProvider = ({
sendEventToParent({
newVariableValue: {
name:
typebot.variables.find((variable) => variable.id === variableId)
localTypebot.variables.find((variable) => variable.id === variableId)
?.name ?? '',
value: formattedValue ?? '',
},
})
const variable = localTypebot.variables.find((v) => v.id === variableId)
const otherVariablesWithSameName = localTypebot.variables.filter(
(v) => v.name === variable?.name && v.id !== variableId
)
const variablesToUpdate = [variable, ...otherVariablesWithSameName].filter(
isDefined
)
setLocalTypebot((typebot) => ({
...typebot,
variables: typebot.variables.map((v) =>
v.id === variableId ? { ...v, value: formattedValue } : v
variables: typebot.variables.map((variable) =>
variablesToUpdate.some(
(variableToUpdate) => variableToUpdate.id === variable.id
)
? { ...variable, value: formattedValue }
: variable
),
}))
}