2
0

🚸 (condition) Enable multiple condition items in one block

Closes #162
This commit is contained in:
Baptiste Arnaud
2022-11-16 14:56:09 +01:00
parent 96eb77d94b
commit 6725c17a02
24 changed files with 327 additions and 216 deletions

View File

@ -1,9 +1,9 @@
import {
ItemIndices,
Item,
InputBlockType,
BlockWithItems,
ButtonItem,
defaultConditionContent,
ItemType,
} from 'models'
import { SetTypebot } from '../TypebotProvider'
import produce from 'immer'
@ -12,10 +12,7 @@ import { byId, blockHasItems } from 'utils'
import cuid from 'cuid'
export type ItemsActions = {
createItem: (
item: ButtonItem | Omit<ButtonItem, 'id'>,
indices: ItemIndices
) => void
createItem: (item: Item | Omit<Item, 'id'>, indices: ItemIndices) => void
updateItem: (indices: ItemIndices, updates: Partial<Omit<Item, 'id'>>) => void
detachItemFromBlock: (indices: ItemIndices) => void
deleteItem: (indices: ItemIndices) => void
@ -23,18 +20,23 @@ export type ItemsActions = {
const itemsAction = (setTypebot: SetTypebot): ItemsActions => ({
createItem: (
item: ButtonItem | Omit<ButtonItem, 'id'>,
item: Item | Omit<Item, 'id'>,
{ groupIndex, blockIndex, itemIndex }: ItemIndices
) =>
setTypebot((typebot) =>
produce(typebot, (typebot) => {
const block = typebot.groups[groupIndex].blocks[blockIndex]
if (block.type !== InputBlockType.CHOICE) return
const block = typebot.groups[groupIndex].blocks[
blockIndex
] as BlockWithItems
const newItem = {
...item,
blockId: block.id,
id: 'id' in item ? item.id : cuid(),
}
content:
item.type === ItemType.CONDITION
? defaultConditionContent
: undefined,
...item,
} as Item
if (item.outgoingEdgeId) {
const edgeIndex = typebot.edges.findIndex(byId(item.outgoingEdgeId))
edgeIndex !== -1