2
0

fix(editor): 🐛 Duplicate steps with items

This commit is contained in:
Baptiste Arnaud
2022-03-25 14:15:27 +01:00
parent c507ef55ae
commit d06cbeac73
4 changed files with 41 additions and 25 deletions

View File

@ -6,13 +6,12 @@ import {
Block, Block,
DraggableStep, DraggableStep,
DraggableStepType, DraggableStepType,
IntegrationStepType,
StepIndices, StepIndices,
Typebot, Typebot,
} from 'models' } from 'models'
import { SetTypebot } from '../TypebotContext' import { SetTypebot } from '../TypebotContext'
import { cleanUpEdgeDraft } from './edges' import { cleanUpEdgeDraft } from './edges'
import { createStepDraft } from './steps' import { createStepDraft, duplicateStepDraft } from './steps'
export type BlocksActions = { export type BlocksActions = {
createBlock: ( createBlock: (
@ -66,11 +65,7 @@ const blocksActions = (setTypebot: SetTypebot): BlocksActions => ({
...block, ...block,
title: `${block.title} copy`, title: `${block.title} copy`,
id, id,
steps: block.steps.map((s) => steps: block.steps.map(duplicateStepDraft(id)),
s.type === IntegrationStepType.WEBHOOK
? { ...s, blockId: id, id: cuid(), webhookId: cuid() }
: { ...s, blockId: id, id: cuid() }
),
graphCoordinates: { graphCoordinates: {
x: block.graphCoordinates.x + 200, x: block.graphCoordinates.x + 200,
y: block.graphCoordinates.y + 100, y: block.graphCoordinates.y + 100,

View File

@ -38,7 +38,7 @@ export const edgesAction = (setTypebot: SetTypebot): EdgesActions => ({
).items.findIndex(byId(edge.from.itemId)) ).items.findIndex(byId(edge.from.itemId))
: null : null
isDefined(itemIndex) isDefined(itemIndex) && itemIndex !== -1
? addEdgeIdToItem(typebot, newEdge.id, { ? addEdgeIdToItem(typebot, newEdge.id, {
blockIndex, blockIndex,
stepIndex, stepIndex,

View File

@ -86,4 +86,11 @@ const itemsAction = (setTypebot: SetTypebot): ItemsActions => ({
), ),
}) })
export { itemsAction } const duplicateItemDraft = (stepId: string) => (item: Item) => ({
...item,
id: cuid(),
stepId,
outgoingEdgeId: undefined,
})
export { itemsAction, duplicateItemDraft }

View File

@ -4,7 +4,6 @@ import {
DraggableStep, DraggableStep,
DraggableStepType, DraggableStepType,
StepIndices, StepIndices,
IntegrationStepType,
} from 'models' } from 'models'
import { parseNewStep } from 'services/typebots/typebots' import { parseNewStep } from 'services/typebots/typebots'
import { removeEmptyBlocks } from './blocks' import { removeEmptyBlocks } from './blocks'
@ -13,7 +12,8 @@ import { SetTypebot } from '../TypebotContext'
import produce from 'immer' import produce from 'immer'
import { cleanUpEdgeDraft, deleteEdgeDraft } from './edges' import { cleanUpEdgeDraft, deleteEdgeDraft } from './edges'
import cuid from 'cuid' import cuid from 'cuid'
import { byId } from 'utils' import { byId, isWebhookStep, stepHasItems } from 'utils'
import { duplicateItemDraft } from './items'
export type StepsActions = { export type StepsActions = {
createStep: ( createStep: (
@ -54,19 +54,8 @@ const stepsAction = (setTypebot: SetTypebot): StepsActions => ({
duplicateStep: ({ blockIndex, stepIndex }: StepIndices) => duplicateStep: ({ blockIndex, stepIndex }: StepIndices) =>
setTypebot((typebot) => setTypebot((typebot) =>
produce(typebot, (typebot) => { produce(typebot, (typebot) => {
const step = typebot.blocks[blockIndex].steps[stepIndex] const step = { ...typebot.blocks[blockIndex].steps[stepIndex] }
const id = cuid() const newStep = duplicateStepDraft(step.blockId)(step)
const newStep: Step =
step.type === IntegrationStepType.WEBHOOK
? {
...step,
id,
webhookId: cuid(),
}
: {
...step,
id,
}
typebot.blocks[blockIndex].steps.splice(stepIndex + 1, 0, newStep) typebot.blocks[blockIndex].steps.splice(stepIndex + 1, 0, newStep)
}) })
), ),
@ -125,6 +114,13 @@ const moveStepToBlock = (
{ blockIndex, stepIndex }: StepIndices { blockIndex, stepIndex }: StepIndices
) => { ) => {
const newStep = { ...step, blockId } const newStep = { ...step, blockId }
const items = stepHasItems(step) ? step.items : []
items.forEach((item) => {
const edgeIndex = typebot.edges.findIndex(byId(item.outgoingEdgeId))
edgeIndex !== -1
? (typebot.edges[edgeIndex].from.blockId = blockId)
: (newStep.outgoingEdgeId = undefined)
})
if (step.outgoingEdgeId) { if (step.outgoingEdgeId) {
if (typebot.blocks[blockIndex].steps.length > stepIndex ?? 0) { if (typebot.blocks[blockIndex].steps.length > stepIndex ?? 0) {
deleteEdgeDraft(typebot, step.outgoingEdgeId) deleteEdgeDraft(typebot, step.outgoingEdgeId)
@ -139,4 +135,22 @@ const moveStepToBlock = (
typebot.blocks[blockIndex].steps.splice(stepIndex ?? 0, 0, newStep) typebot.blocks[blockIndex].steps.splice(stepIndex ?? 0, 0, newStep)
} }
export { stepsAction, createStepDraft } const duplicateStepDraft =
(blockId: string) =>
(step: Step): Step => {
const stepId = cuid()
return {
...step,
blockId,
id: stepId,
items: stepHasItems(step)
? step.items.map(duplicateItemDraft(stepId))
: undefined,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
webhookId: isWebhookStep(step) ? cuid() : undefined,
outgoingEdgeId: undefined,
}
}
export { stepsAction, createStepDraft, duplicateStepDraft }