2
0

fix(editor): 🐛 Delete edge when adding last step

This commit is contained in:
Baptiste Arnaud
2022-02-15 15:45:52 +01:00
parent 93e8f90ac3
commit 67ccf07a61
4 changed files with 39 additions and 7 deletions

View File

@@ -75,7 +75,6 @@ export const Edge = ({ edge }: { edge: EdgeProps }) => {
sourceTop, sourceTop,
]) ])
if (sourceTop === 0) return <></>
return ( return (
<path <path
data-testid="edge" data-testid="edge"

View File

@@ -196,7 +196,8 @@ export const StepNode = ({
top="19px" top="19px"
stepId={localStep.id} stepId={localStep.id}
/> />
{isConnectable && hasDefaultConnector(localStep) && ( {(localStep.outgoingEdgeId ||
(isConnectable && hasDefaultConnector(localStep))) && (
<SourceEndpoint <SourceEndpoint
source={{ source={{
blockId: localStep.blockId, blockId: localStep.blockId,

View File

@@ -3,7 +3,7 @@ import { WritableDraft } from 'immer/dist/types/types-external'
import { generate } from 'short-uuid' import { generate } from 'short-uuid'
import { SetTypebot } from '../TypebotContext' import { SetTypebot } from '../TypebotContext'
import { produce } from 'immer' import { produce } from 'immer'
import { byId, isDefined } from 'utils' import { byId, isDefined, stepHasItems } from 'utils'
export type EdgesActions = { export type EdgesActions = {
createEdge: (edge: Omit<Edge, 'id'>) => void createEdge: (edge: Omit<Edge, 'id'>) => void
@@ -89,9 +89,34 @@ export const deleteEdgeDraft = (
edgeId: string edgeId: string
) => { ) => {
const edgeIndex = typebot.edges.findIndex(byId(edgeId)) const edgeIndex = typebot.edges.findIndex(byId(edgeId))
deleteOutgoingEdgeIdProps(typebot, edgeIndex)
typebot.edges.splice(edgeIndex, 1) typebot.edges.splice(edgeIndex, 1)
} }
const deleteOutgoingEdgeIdProps = (
typebot: WritableDraft<Typebot>,
edgeIndex: number
) => {
const edge = typebot.edges[edgeIndex]
const fromBlockIndex = typebot.blocks.findIndex(byId(edge.from.blockId))
const fromStepIndex = typebot.blocks[fromBlockIndex].steps.findIndex(
byId(edge.from.stepId)
)
const step = typebot.blocks[fromBlockIndex].steps[fromStepIndex]
const fromItemIndex =
edge.from.itemId && stepHasItems(step)
? step.items.findIndex(byId(edge.from.itemId))
: -1
if (fromStepIndex !== -1)
typebot.blocks[fromBlockIndex].steps[fromStepIndex].outgoingEdgeId =
undefined
if (fromItemIndex !== -1) {
;(
typebot.blocks[fromBlockIndex].steps[fromStepIndex] as StepWithItems
).items[fromItemIndex].outgoingEdgeId = undefined
}
}
export const cleanUpEdgeDraft = ( export const cleanUpEdgeDraft = (
typebot: WritableDraft<Typebot>, typebot: WritableDraft<Typebot>,
deletedNodeId: string deletedNodeId: string

View File

@@ -10,7 +10,7 @@ import { removeEmptyBlocks } from './blocks'
import { WritableDraft } from 'immer/dist/types/types-external' import { WritableDraft } from 'immer/dist/types/types-external'
import { SetTypebot } from '../TypebotContext' import { SetTypebot } from '../TypebotContext'
import produce from 'immer' import produce from 'immer'
import { cleanUpEdgeDraft } from './edges' import { cleanUpEdgeDraft, deleteEdgeDraft } from './edges'
export type StepsActions = { export type StepsActions = {
createStep: ( createStep: (
@@ -76,11 +76,18 @@ const createStepDraft = (
typebot: WritableDraft<Typebot>, typebot: WritableDraft<Typebot>,
step: DraggableStep | DraggableStepType, step: DraggableStep | DraggableStepType,
blockId: string, blockId: string,
indices: StepIndices { blockIndex, stepIndex }: StepIndices
) => { ) => {
const steps = typebot.blocks[blockIndex].steps
if (
stepIndex === steps.length &&
stepIndex > 0 &&
steps[stepIndex - 1].outgoingEdgeId
)
deleteEdgeDraft(typebot, steps[stepIndex - 1].outgoingEdgeId as string)
typeof step === 'string' typeof step === 'string'
? createNewStep(typebot, step, blockId, indices) ? createNewStep(typebot, step, blockId, { blockIndex, stepIndex })
: moveStepToBlock(typebot, step, blockId, indices) : moveStepToBlock(typebot, step, blockId, { blockIndex, stepIndex })
removeEmptyBlocks(typebot) removeEmptyBlocks(typebot)
} }