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