2
0

🐛 (typebotLink) Populate variable value when linked typebot is injected

This commit is contained in:
Baptiste Arnaud
2022-12-13 09:48:48 +01:00
parent fd6b94bb1b
commit 92dc797b6c

View File

@@ -2,7 +2,7 @@ import { TypebotViewerProps } from '@/components/TypebotViewer'
import { safeStringify } from '@/features/variables' import { safeStringify } from '@/features/variables'
import { sendEventToParent } from '@/utils/chat' import { sendEventToParent } from '@/utils/chat'
import { Log } from 'db' import { Log } from 'db'
import { Edge, PublicTypebot, Typebot } from 'models' import { Edge, PublicTypebot, Typebot, Variable } from 'models'
import { import {
createContext, createContext,
ReactNode, ReactNode,
@@ -113,11 +113,15 @@ export const TypebotProvider = ({
} }
const injectLinkedTypebot = (typebot: Typebot | PublicTypebot) => { const injectLinkedTypebot = (typebot: Typebot | PublicTypebot) => {
const newVariables = fillVariablesWithExistingValues(
typebot.variables,
localTypebot.variables
)
const typebotToInject = { const typebotToInject = {
id: 'typebotId' in typebot ? typebot.typebotId : typebot.id, id: 'typebotId' in typebot ? typebot.typebotId : typebot.id,
groups: typebot.groups, groups: typebot.groups,
edges: typebot.edges, edges: typebot.edges,
variables: typebot.variables, variables: newVariables,
} }
setLinkedTypebots((typebots) => [...typebots, typebotToInject]) setLinkedTypebots((typebots) => [...typebots, typebotToInject])
const updatedTypebot = { const updatedTypebot = {
@@ -130,6 +134,21 @@ export const TypebotProvider = ({
return typebotToInject return typebotToInject
} }
const fillVariablesWithExistingValues = (
variables: Variable[],
variablesWithValues: Variable[]
): Variable[] =>
variables.map((variable) => {
const matchedVariable = variablesWithValues.find(
(variableWithValue) => variableWithValue.name === variable.name
)
return {
...variable,
value: matchedVariable?.value ?? variable.value,
}
})
const pushEdgeIdInLinkedTypebotQueue = (bot: { const pushEdgeIdInLinkedTypebotQueue = (bot: {
typebotId: string typebotId: string
edgeId: string edgeId: string