2
0

(setVariable) Add "Moment of the day" variable value

Closes #564
This commit is contained in:
Baptiste Arnaud
2023-06-15 14:45:42 +02:00
parent fbe63aa3f3
commit d8c1a36bc0
6 changed files with 41 additions and 7 deletions

View File

@@ -15,10 +15,18 @@ export const executeSetVariable = async (
return {
outgoingEdgeId: block.outgoingEdgeId,
}
if (block.options.isExecutedOnClient && block.options.expressionToEvaluate) {
const expressionToEvaluate = getExpressionToEvaluate(state.result.id)(
block.options
)
const isCustomValue = !block.options.type || block.options.type === 'Custom'
if (
expressionToEvaluate &&
((isCustomValue && block.options.isExecutedOnClient) ||
block.options.type === 'Moment of the day')
) {
const scriptToExecute = parseScriptToExecuteClientSideAction(
state.typebot.variables,
block.options.expressionToEvaluate
expressionToEvaluate
)
return {
outgoingEdgeId: block.outgoingEdgeId,
@@ -31,9 +39,6 @@ export const executeSetVariable = async (
],
}
}
const expressionToEvaluate = getExpressionToEvaluate(state.result.id)(
block.options
)
const evaluatedExpression = expressionToEvaluate
? evaluateSetVariableExpression(variables)(expressionToEvaluate)
: undefined
@@ -92,6 +97,13 @@ const getExpressionToEvaluate =
case 'Empty': {
return null
}
case 'Moment of the day': {
return `const now = new Date()
if(now.getHours() < 12) return 'morning'
if(now.getHours() >= 12 && now.getHours() < 18) return 'afternoon'
if(now.getHours() >= 18) return 'evening'
if(now.getHours() >= 22 || now.getHours() < 6) return 'night'`
}
case 'Custom':
case undefined: {
return options.expressionToEvaluate ?? null