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

View File

@ -38,7 +38,7 @@ export const edgesAction = (setTypebot: SetTypebot): EdgesActions => ({
).items.findIndex(byId(edge.from.itemId))
: null
isDefined(itemIndex)
isDefined(itemIndex) && itemIndex !== -1
? addEdgeIdToItem(typebot, newEdge.id, {
blockIndex,
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,
DraggableStepType,
StepIndices,
IntegrationStepType,
} from 'models'
import { parseNewStep } from 'services/typebots/typebots'
import { removeEmptyBlocks } from './blocks'
@ -13,7 +12,8 @@ import { SetTypebot } from '../TypebotContext'
import produce from 'immer'
import { cleanUpEdgeDraft, deleteEdgeDraft } from './edges'
import cuid from 'cuid'
import { byId } from 'utils'
import { byId, isWebhookStep, stepHasItems } from 'utils'
import { duplicateItemDraft } from './items'
export type StepsActions = {
createStep: (
@ -54,19 +54,8 @@ const stepsAction = (setTypebot: SetTypebot): StepsActions => ({
duplicateStep: ({ blockIndex, stepIndex }: StepIndices) =>
setTypebot((typebot) =>
produce(typebot, (typebot) => {
const step = typebot.blocks[blockIndex].steps[stepIndex]
const id = cuid()
const newStep: Step =
step.type === IntegrationStepType.WEBHOOK
? {
...step,
id,
webhookId: cuid(),
}
: {
...step,
id,
}
const step = { ...typebot.blocks[blockIndex].steps[stepIndex] }
const newStep = duplicateStepDraft(step.blockId)(step)
typebot.blocks[blockIndex].steps.splice(stepIndex + 1, 0, newStep)
})
),
@ -125,6 +114,13 @@ const moveStepToBlock = (
{ blockIndex, stepIndex }: StepIndices
) => {
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 (typebot.blocks[blockIndex].steps.length > stepIndex ?? 0) {
deleteEdgeDraft(typebot, step.outgoingEdgeId)
@ -139,4 +135,22 @@ const moveStepToBlock = (
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 }