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,
])
if (sourceTop === 0) return <></>
return (
<path
data-testid="edge"

View File

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

View File

@ -3,7 +3,7 @@ import { WritableDraft } from 'immer/dist/types/types-external'
import { generate } from 'short-uuid'
import { SetTypebot } from '../TypebotContext'
import { produce } from 'immer'
import { byId, isDefined } from 'utils'
import { byId, isDefined, stepHasItems } from 'utils'
export type EdgesActions = {
createEdge: (edge: Omit<Edge, 'id'>) => void
@ -89,9 +89,34 @@ export const deleteEdgeDraft = (
edgeId: string
) => {
const edgeIndex = typebot.edges.findIndex(byId(edgeId))
deleteOutgoingEdgeIdProps(typebot, edgeIndex)
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 = (
typebot: WritableDraft<Typebot>,
deletedNodeId: string

View File

@ -10,7 +10,7 @@ import { removeEmptyBlocks } from './blocks'
import { WritableDraft } from 'immer/dist/types/types-external'
import { SetTypebot } from '../TypebotContext'
import produce from 'immer'
import { cleanUpEdgeDraft } from './edges'
import { cleanUpEdgeDraft, deleteEdgeDraft } from './edges'
export type StepsActions = {
createStep: (
@ -76,11 +76,18 @@ const createStepDraft = (
typebot: WritableDraft<Typebot>,
step: DraggableStep | DraggableStepType,
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'
? createNewStep(typebot, step, blockId, indices)
: moveStepToBlock(typebot, step, blockId, indices)
? createNewStep(typebot, step, blockId, { blockIndex, stepIndex })
: moveStepToBlock(typebot, step, blockId, { blockIndex, stepIndex })
removeEmptyBlocks(typebot)
}