Skip to content

Commit

Permalink
fix(editor): 🐛 Delete edge when adding last step
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Feb 15, 2022
1 parent 93e8f90 commit 67ccf07
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
1 change: 0 additions & 1 deletion apps/builder/components/shared/Graph/Edges/Edge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ export const Edge = ({ edge }: { edge: EdgeProps }) => {
sourceTop,
])

if (sourceTop === 0) return <></>
return (
<path
data-testid="edge"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
27 changes: 26 additions & 1 deletion apps/builder/contexts/TypebotContext/actions/edges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions apps/builder/contexts/TypebotContext/actions/steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: (
Expand Down Expand Up @@ -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)
}

Expand Down

4 comments on commit 67ccf07

@vercel
Copy link

@vercel vercel bot commented on 67ccf07 Feb 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 67ccf07 Feb 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

viewer-v2 – ./apps/viewer

viewer-v2-typebot-io.vercel.app
viewer-v2-git-main-typebot-io.vercel.app
typebot-viewer.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 67ccf07 Feb 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./apps/docs

docs-git-main-typebot-io.vercel.app
docs-typebot-io.vercel.app
docs.typebot.io

@vercel
Copy link

@vercel vercel bot commented on 67ccf07 Feb 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder-v2 – ./apps/builder

app.typebot.io
builder-v2-git-main-typebot-io.vercel.app
builder-v2-typebot-io.vercel.app

Please sign in to comment.