⚡ (setVariable) Add Pop and Shift items
This commit is contained in:
@ -62,7 +62,19 @@ const Expression = ({
|
||||
)
|
||||
}
|
||||
case 'Empty':
|
||||
return <Text as="span">Reset {variableName}</Text>
|
||||
return <Text as="span">Reset {variableName} </Text>
|
||||
case 'Shift':
|
||||
case 'Pop': {
|
||||
const itemVariableName = variables.find(
|
||||
byId(options.saveItemInVariableId)
|
||||
)?.name
|
||||
return (
|
||||
<Text as="span">
|
||||
{options.type} {variableName}
|
||||
{itemVariableName ? ` into ${itemVariableName}` : ''}
|
||||
</Text>
|
||||
)
|
||||
}
|
||||
case 'Random ID':
|
||||
case 'Today':
|
||||
case 'Now':
|
||||
|
@ -132,6 +132,14 @@ const SetVariableValue = ({
|
||||
isExecutedOnClient,
|
||||
})
|
||||
|
||||
const updateListVariableId = (variable?: Pick<Variable, 'id'>) => {
|
||||
if (!options || (options.type !== 'Pop' && options.type !== 'Shift')) return
|
||||
onOptionsChange({
|
||||
...options,
|
||||
saveItemInVariableId: variable?.id,
|
||||
})
|
||||
}
|
||||
|
||||
const updateItemVariableId = (variable?: Pick<Variable, 'id'>) => {
|
||||
if (!options || options.type !== 'Map item with same index') return
|
||||
onOptionsChange({
|
||||
@ -222,6 +230,17 @@ const SetVariableValue = ({
|
||||
</Stack>
|
||||
</>
|
||||
)
|
||||
case 'Pop':
|
||||
case 'Shift':
|
||||
return (
|
||||
<VariableSearchInput
|
||||
initialVariableId={options.saveItemInVariableId}
|
||||
onSelectVariable={updateListVariableId}
|
||||
placeholder={
|
||||
options.type === 'Shift' ? 'Shifted item' : 'Popped item'
|
||||
}
|
||||
/>
|
||||
)
|
||||
case 'Map item with same index': {
|
||||
return (
|
||||
<Stack p="2" rounded="md" borderWidth={1}>
|
||||
|
@ -4,6 +4,7 @@ import {
|
||||
SetVariableBlock,
|
||||
SetVariableHistoryItem,
|
||||
Variable,
|
||||
VariableWithUnknowValue,
|
||||
} from '@typebot.io/schemas'
|
||||
import { byId, isEmpty } from '@typebot.io/lib'
|
||||
import { ExecuteLogicResponse } from '../../../types'
|
||||
@ -80,6 +81,7 @@ export const executeSetVariable = async (
|
||||
const { newSetVariableHistory, updatedState } = updateVariablesInSession({
|
||||
state,
|
||||
newVariables: [
|
||||
...parseColateralVariableChangeIfAny({ state, options: block.options }),
|
||||
{
|
||||
...newVariable,
|
||||
isSessionVariable: sessionOnlySetVariableOptions.includes(
|
||||
@ -183,6 +185,12 @@ const getExpressionToEvaluate =
|
||||
return `const itemIndex = ${options.mapListItemParams?.baseListVariableId}.indexOf(${options.mapListItemParams?.baseItemVariableId})
|
||||
return ${options.mapListItemParams?.targetListVariableId}.at(itemIndex)`
|
||||
}
|
||||
case 'Pop': {
|
||||
return `${options.variableId} && Array.isArray(${options.variableId}) ? ${options.variableId}.slice(0, -1) : []`
|
||||
}
|
||||
case 'Shift': {
|
||||
return `${options.variableId} && Array.isArray(${options.variableId}) ? ${options.variableId}.slice(1) : []`
|
||||
}
|
||||
case 'Append value(s)': {
|
||||
const item = parseVariables(state.typebotsQueue[0].typebot.variables)(
|
||||
options.item
|
||||
@ -329,3 +337,30 @@ const parseResultTranscriptProps = async (
|
||||
.map((edge) => edge.edgeId),
|
||||
}
|
||||
}
|
||||
|
||||
const parseColateralVariableChangeIfAny = ({
|
||||
state,
|
||||
options,
|
||||
}: {
|
||||
state: SessionState
|
||||
options: SetVariableBlock['options']
|
||||
}): VariableWithUnknowValue[] => {
|
||||
if (!options || (options.type !== 'Pop' && options.type !== 'Shift'))
|
||||
return []
|
||||
const listVariableValue = state.typebotsQueue[0].typebot.variables.find(
|
||||
(v) => v.id === options.variableId
|
||||
)?.value
|
||||
const variable = state.typebotsQueue[0].typebot.variables.find(
|
||||
(v) => v.id === options.saveItemInVariableId
|
||||
)
|
||||
if (!variable || !listVariableValue) return []
|
||||
return [
|
||||
{
|
||||
...variable,
|
||||
value:
|
||||
options.type === 'Pop'
|
||||
? listVariableValue.at(-1)
|
||||
: listVariableValue.at(0),
|
||||
},
|
||||
]
|
||||
}
|
||||
|
@ -15,6 +15,8 @@ export const valueTypes = [
|
||||
'Random ID',
|
||||
'Moment of the day',
|
||||
'Map item with same index',
|
||||
'Pop',
|
||||
'Shift',
|
||||
'Phone number',
|
||||
'Contact name',
|
||||
] as const
|
||||
|
@ -25,6 +25,11 @@ const basicSetVariableOptionsSchema = baseOptions.extend({
|
||||
]),
|
||||
})
|
||||
|
||||
const popOrShiftSetVariableOptionsSchema = baseOptions.extend({
|
||||
type: z.enum(['Pop', 'Shift']),
|
||||
saveItemInVariableId: z.string().optional(),
|
||||
})
|
||||
|
||||
const dateSetVariableOptionsSchema = baseOptions.extend({
|
||||
type: z.enum(['Now', 'Yesterday', 'Tomorrow']),
|
||||
timeZone: z.string().optional(),
|
||||
@ -65,6 +70,7 @@ export const setVariableOptionsSchema = z.discriminatedUnion('type', [
|
||||
customSetVariableOptionsSchema,
|
||||
mapListItemsOptionsSchema,
|
||||
appendItemToListOptionsSchema,
|
||||
popOrShiftSetVariableOptionsSchema,
|
||||
])
|
||||
|
||||
export const setVariableBlockSchema = blockBaseSchema.merge(
|
||||
|
Reference in New Issue
Block a user