2
0

fix(graph): 🐛 Step node drag

This commit is contained in:
Baptiste Arnaud
2022-01-07 07:03:08 +01:00
parent f712c7ad98
commit 6fe27bd8ea
4 changed files with 47 additions and 7 deletions

View File

@ -70,6 +70,7 @@ export const StepsList = ({
if (!draggedStep && !draggedStepType) return if (!draggedStep && !draggedStepType) return
setExpandedPlaceholderIndex(stepIndex + 1) setExpandedPlaceholderIndex(stepIndex + 1)
} }
return ( return (
<Stack <Stack
spacing={1} spacing={1}

View File

@ -1,4 +1,5 @@
import { Coordinates } from 'contexts/GraphContext' import { Coordinates } from 'contexts/GraphContext'
import { WritableDraft } from 'immer/dist/internal'
import { Block, Step, StepType, Typebot } from 'models' import { Block, Step, StepType, Typebot } from 'models'
import { parseNewBlock } from 'services/typebots' import { parseNewBlock } from 'services/typebots'
import { Updater } from 'use-immer' import { Updater } from 'use-immer'
@ -13,6 +14,7 @@ export type BlocksActions = {
export const blocksActions = (setTypebot: Updater<Typebot>): BlocksActions => ({ export const blocksActions = (setTypebot: Updater<Typebot>): BlocksActions => ({
createBlock: ({ x, y, step }: Coordinates & { step: StepType | Step }) => { createBlock: ({ x, y, step }: Coordinates & { step: StepType | Step }) => {
setTypebot((typebot) => { setTypebot((typebot) => {
removeEmptyBlocks(typebot)
const newBlock = parseNewBlock({ const newBlock = parseNewBlock({
totalBlocks: typebot.blocks.allIds.length, totalBlocks: typebot.blocks.allIds.length,
initialCoordinates: { x, y }, initialCoordinates: { x, y },
@ -31,10 +33,29 @@ export const blocksActions = (setTypebot: Updater<Typebot>): BlocksActions => ({
}), }),
deleteBlock: (blockId: string) => deleteBlock: (blockId: string) =>
setTypebot((typebot) => { setTypebot((typebot) => {
const block = typebot.blocks.byId[blockId] deleteStepsInsideBlock(typebot, blockId)
block.stepIds.forEach((stepId) => deleteStepDraft(typebot, stepId)) deleteBlockDraft(typebot)(blockId)
delete typebot.blocks.byId[blockId]
const index = typebot.blocks.allIds.indexOf(blockId)
if (index !== -1) typebot.blocks.allIds.splice(index, 1)
}), }),
}) })
export const removeEmptyBlocks = (typebot: WritableDraft<Typebot>) => {
const emptyBlockIds = typebot.blocks.allIds.filter(
(blockId) => typebot.blocks.byId[blockId].stepIds.length === 0
)
emptyBlockIds.forEach(deleteBlockDraft(typebot))
}
const deleteStepsInsideBlock = (
typebot: WritableDraft<Typebot>,
blockId: string
) => {
const block = typebot.blocks.byId[blockId]
block.stepIds.forEach((stepId) => deleteStepDraft(typebot, stepId))
}
export const deleteBlockDraft =
(typebot: WritableDraft<Typebot>) => (blockId: string) => {
delete typebot.blocks.byId[blockId]
const index = typebot.blocks.allIds.indexOf(blockId)
if (index !== -1) typebot.blocks.allIds.splice(index, 1)
}

View File

@ -1,6 +1,7 @@
import { Step, StepType, Typebot } from 'models' import { Step, StepType, Typebot } from 'models'
import { parseNewStep } from 'services/typebots' import { parseNewStep } from 'services/typebots'
import { Updater } from 'use-immer' import { Updater } from 'use-immer'
import { removeEmptyBlocks } from './blocks'
import { WritableDraft } from 'immer/dist/types/types-external' import { WritableDraft } from 'immer/dist/types/types-external'
export type StepsActions = { export type StepsActions = {
@ -15,6 +16,7 @@ export type StepsActions = {
export const stepsAction = (setTypebot: Updater<Typebot>): StepsActions => ({ export const stepsAction = (setTypebot: Updater<Typebot>): StepsActions => ({
createStep: (blockId: string, step: StepType | Step, index?: number) => { createStep: (blockId: string, step: StepType | Step, index?: number) => {
setTypebot((typebot) => { setTypebot((typebot) => {
removeEmptyBlocks(typebot)
createStepDraft(typebot, step, blockId, index) createStepDraft(typebot, step, blockId, index)
}) })
}, },
@ -24,11 +26,25 @@ export const stepsAction = (setTypebot: Updater<Typebot>): StepsActions => ({
}), }),
deleteStep: (stepId: string) => { deleteStep: (stepId: string) => {
setTypebot((typebot) => { setTypebot((typebot) => {
removeStepIdFromBlock(typebot, stepId)
deleteStepDraft(typebot, stepId) deleteStepDraft(typebot, stepId)
}) })
}, },
}) })
const removeStepIdFromBlock = (
typebot: WritableDraft<Typebot>,
stepId: string
) => {
const containerBlockId = typebot.blocks.allIds.find((blockId) =>
typebot.blocks.byId[blockId].stepIds.includes(stepId)
) as string
typebot.blocks.byId[containerBlockId].stepIds.splice(
typebot.blocks.byId[containerBlockId].stepIds.indexOf(stepId),
1
)
}
export const deleteStepDraft = ( export const deleteStepDraft = (
typebot: WritableDraft<Typebot>, typebot: WritableDraft<Typebot>,
stepId: string stepId: string
@ -44,7 +60,10 @@ export const createStepDraft = (
blockId: string, blockId: string,
index?: number index?: number
) => { ) => {
const newStep = typeof step === 'string' ? parseNewStep(step, blockId) : step const newStep =
typeof step === 'string'
? parseNewStep(step, blockId)
: { ...step, blockId }
typebot.steps.byId[newStep.id] = newStep typebot.steps.byId[newStep.id] = newStep
typebot.steps.allIds.push(newStep.id) typebot.steps.allIds.push(newStep.id)
typebot.blocks.byId[blockId].stepIds.splice(index ?? 0, 0, newStep.id) typebot.blocks.byId[blockId].stepIds.splice(index ?? 0, 0, newStep.id)

View File

@ -43,7 +43,6 @@ export const parseSubmissionsColumns = (
Header: JSX.Element Header: JSX.Element
accessor: string accessor: string
}[] => { }[] => {
console.log(typebot)
if (!typebot) return [] if (!typebot) return []
return [ return [
{ {