fix(viewer): 🐛 Better result initialization
This commit is contained in:
@ -35,23 +35,15 @@ export const TypebotPage = ({
|
|||||||
predefinedVariables[key] = value
|
predefinedVariables[key] = value
|
||||||
})
|
})
|
||||||
setPredefinedVariables(predefinedVariables)
|
setPredefinedVariables(predefinedVariables)
|
||||||
|
initializeResult().then()
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
// Workaround for react-frame-component bug (https://github.com/ryanseddon/react-frame-component/pull/207)
|
const initializeResult = async () => {
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
||||||
useEffect(() => {
|
|
||||||
if (showTypebot) return
|
|
||||||
setShowTypebot(true)
|
|
||||||
})
|
|
||||||
|
|
||||||
const initializeResult = async (variables: VariableWithValue[]) => {
|
|
||||||
const resultIdFromSession = getExistingResultFromSession()
|
const resultIdFromSession = getExistingResultFromSession()
|
||||||
if (resultIdFromSession) setResultId(resultIdFromSession)
|
if (resultIdFromSession) setResultId(resultIdFromSession)
|
||||||
else {
|
else {
|
||||||
const { error, data: result } = await createResult(
|
const { error, data: result } = await createResult(typebot.typebotId)
|
||||||
typebot.typebotId,
|
|
||||||
variables
|
|
||||||
)
|
|
||||||
if (error) setError(error)
|
if (error) setError(error)
|
||||||
if (result) {
|
if (result) {
|
||||||
setResultId(result.id)
|
setResultId(result.id)
|
||||||
@ -59,6 +51,15 @@ export const TypebotPage = ({
|
|||||||
setResultInSession(result.id)
|
setResultInSession(result.id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
setShowTypebot(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleVariablesPrefilled = async (
|
||||||
|
prefilledVariables: VariableWithValue[]
|
||||||
|
) => {
|
||||||
|
if (!resultId) return setError(new Error('Result was not created'))
|
||||||
|
const { error } = await updateResult(resultId, { prefilledVariables })
|
||||||
|
if (error) setError(error)
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleNewAnswer = async (answer: Answer) => {
|
const handleNewAnswer = async (answer: Answer) => {
|
||||||
@ -97,7 +98,7 @@ export const TypebotPage = ({
|
|||||||
predefinedVariables={predefinedVariables}
|
predefinedVariables={predefinedVariables}
|
||||||
onNewAnswer={handleNewAnswer}
|
onNewAnswer={handleNewAnswer}
|
||||||
onCompleted={handleCompleted}
|
onCompleted={handleCompleted}
|
||||||
onVariablesPrefilled={initializeResult}
|
onVariablesPrefilled={handleVariablesPrefilled}
|
||||||
onNewLog={handleNewLog}
|
onNewLog={handleNewLog}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import prisma from 'libs/prisma'
|
import prisma from 'libs/prisma'
|
||||||
import { ResultWithAnswers, VariableWithValue } from 'models'
|
import { ResultWithAnswers } from 'models'
|
||||||
import { NextApiRequest, NextApiResponse } from 'next'
|
import { NextApiRequest, NextApiResponse } from 'next'
|
||||||
import { authenticateUser } from 'services/api/utils'
|
import { authenticateUser } from 'services/api/utils'
|
||||||
import { methodNotAllowed } from 'utils'
|
import { methodNotAllowed } from 'utils'
|
||||||
@ -24,13 +24,8 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
}
|
}
|
||||||
if (req.method === 'POST') {
|
if (req.method === 'POST') {
|
||||||
const typebotId = req.query.typebotId as string
|
const typebotId = req.query.typebotId as string
|
||||||
const resultData = (
|
|
||||||
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
|
|
||||||
) as {
|
|
||||||
prefilledVariables: VariableWithValue[]
|
|
||||||
}
|
|
||||||
const result = await prisma.result.create({
|
const result = await prisma.result.create({
|
||||||
data: { ...resultData, typebotId, isCompleted: false },
|
data: { typebotId, isCompleted: false },
|
||||||
})
|
})
|
||||||
return res.send(result)
|
return res.send(result)
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,10 @@
|
|||||||
import { Log, Result } from 'db'
|
import { Log, Result } from 'db'
|
||||||
import { VariableWithValue } from 'models'
|
|
||||||
import { sendRequest } from 'utils'
|
import { sendRequest } from 'utils'
|
||||||
|
|
||||||
export const createResult = async (
|
export const createResult = async (typebotId: string) => {
|
||||||
typebotId: string,
|
|
||||||
prefilledVariables: VariableWithValue[]
|
|
||||||
) => {
|
|
||||||
return sendRequest<Result>({
|
return sendRequest<Result>({
|
||||||
url: `/api/typebots/${typebotId}/results`,
|
url: `/api/typebots/${typebotId}/results`,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: { prefilledVariables },
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,26 +52,22 @@ export const ConversationContainer = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (predefinedVariables) {
|
|
||||||
const prefilledVariables = injectPredefinedVariables(predefinedVariables)
|
const prefilledVariables = injectPredefinedVariables(predefinedVariables)
|
||||||
if (onVariablesPrefilled) {
|
if (onVariablesPrefilled) onVariablesPrefilled(prefilledVariables)
|
||||||
onVariablesPrefilled(prefilledVariables)
|
|
||||||
setPrefilledVariables(prefilledVariables)
|
setPrefilledVariables(prefilledVariables)
|
||||||
}
|
|
||||||
}
|
|
||||||
displayNextBlock(typebot.blocks[0].steps[0].outgoingEdgeId)
|
displayNextBlock(typebot.blocks[0].steps[0].outgoingEdgeId)
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
const injectPredefinedVariables = (predefinedVariables: {
|
const injectPredefinedVariables = (predefinedVariables?: {
|
||||||
[key: string]: string | undefined
|
[key: string]: string | undefined
|
||||||
}) => {
|
}) => {
|
||||||
const prefilledVariables: VariableWithValue[] = []
|
const prefilledVariables: VariableWithValue[] = []
|
||||||
Object.keys(predefinedVariables).forEach((key) => {
|
Object.keys(predefinedVariables ?? {}).forEach((key) => {
|
||||||
const matchingVariable = typebot.variables.find(
|
const matchingVariable = typebot.variables.find(
|
||||||
(v) => v.name.toLowerCase() === key.toLowerCase()
|
(v) => v.name.toLowerCase() === key.toLowerCase()
|
||||||
)
|
)
|
||||||
if (isNotDefined(matchingVariable)) return
|
if (!predefinedVariables || isNotDefined(matchingVariable)) return
|
||||||
const value = predefinedVariables[key]
|
const value = predefinedVariables[key]
|
||||||
if (!value) return
|
if (!value) return
|
||||||
updateVariableValue(matchingVariable?.id, value)
|
updateVariableValue(matchingVariable?.id, value)
|
||||||
|
Reference in New Issue
Block a user