feat(Editor): use REST API endpoint to create step
This commit is contained in:
@@ -4,7 +4,7 @@ import Box from '@mui/material/Box';
|
|||||||
import IconButton from '@mui/material/IconButton';
|
import IconButton from '@mui/material/IconButton';
|
||||||
import AddIcon from '@mui/icons-material/Add';
|
import AddIcon from '@mui/icons-material/Add';
|
||||||
|
|
||||||
import { CREATE_STEP } from 'graphql/mutations/create-step';
|
import useCreateStep from 'hooks/useCreateStep';
|
||||||
import { UPDATE_STEP } from 'graphql/mutations/update-step';
|
import { UPDATE_STEP } from 'graphql/mutations/update-step';
|
||||||
import FlowStep from 'components/FlowStep';
|
import FlowStep from 'components/FlowStep';
|
||||||
import { FlowPropType } from 'propTypes/propTypes';
|
import { FlowPropType } from 'propTypes/propTypes';
|
||||||
@@ -12,9 +12,9 @@ import { useQueryClient } from '@tanstack/react-query';
|
|||||||
|
|
||||||
function Editor(props) {
|
function Editor(props) {
|
||||||
const [updateStep] = useMutation(UPDATE_STEP);
|
const [updateStep] = useMutation(UPDATE_STEP);
|
||||||
const [createStep, { loading: creationInProgress }] =
|
|
||||||
useMutation(CREATE_STEP);
|
|
||||||
const { flow } = props;
|
const { flow } = props;
|
||||||
|
const { mutateAsync: createStep, isPending: isCreateStepPending } =
|
||||||
|
useCreateStep(flow?.id);
|
||||||
const [triggerStep] = flow.steps;
|
const [triggerStep] = flow.steps;
|
||||||
const [currentStepId, setCurrentStepId] = React.useState(triggerStep.id);
|
const [currentStepId, setCurrentStepId] = React.useState(triggerStep.id);
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
@@ -48,24 +48,13 @@ function Editor(props) {
|
|||||||
|
|
||||||
const addStep = React.useCallback(
|
const addStep = React.useCallback(
|
||||||
async (previousStepId) => {
|
async (previousStepId) => {
|
||||||
const mutationInput = {
|
const { data: createdStep } = await createStep({
|
||||||
previousStep: {
|
previousStepId,
|
||||||
id: previousStepId,
|
|
||||||
},
|
|
||||||
flow: {
|
|
||||||
id: flow.id,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const createdStep = await createStep({
|
|
||||||
variables: { input: mutationInput },
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const createdStepId = createdStep.data.createStep.id;
|
setCurrentStepId(createdStep.id);
|
||||||
setCurrentStepId(createdStepId);
|
|
||||||
await queryClient.invalidateQueries({ queryKey: ['flows', flow.id] });
|
|
||||||
},
|
},
|
||||||
[createStep, flow.id, queryClient],
|
[createStep],
|
||||||
);
|
);
|
||||||
|
|
||||||
const openNextStep = React.useCallback((nextStep) => {
|
const openNextStep = React.useCallback((nextStep) => {
|
||||||
@@ -101,7 +90,7 @@ function Editor(props) {
|
|||||||
<IconButton
|
<IconButton
|
||||||
onClick={() => addStep(step.id)}
|
onClick={() => addStep(step.id)}
|
||||||
color="primary"
|
color="primary"
|
||||||
disabled={creationInProgress || flow.active}
|
disabled={isCreateStepPending || flow.active}
|
||||||
>
|
>
|
||||||
<AddIcon />
|
<AddIcon />
|
||||||
</IconButton>
|
</IconButton>
|
||||||
|
@@ -4,8 +4,9 @@ import { useQueryClient } from '@tanstack/react-query';
|
|||||||
import { FlowPropType } from 'propTypes/propTypes';
|
import { FlowPropType } from 'propTypes/propTypes';
|
||||||
import ReactFlow, { useNodesState, useEdgesState } from 'reactflow';
|
import ReactFlow, { useNodesState, useEdgesState } from 'reactflow';
|
||||||
import 'reactflow/dist/style.css';
|
import 'reactflow/dist/style.css';
|
||||||
|
|
||||||
import { UPDATE_STEP } from 'graphql/mutations/update-step';
|
import { UPDATE_STEP } from 'graphql/mutations/update-step';
|
||||||
import { CREATE_STEP } from 'graphql/mutations/create-step';
|
import useCreateStep from 'hooks/useCreateStep';
|
||||||
|
|
||||||
import { useAutoLayout } from './useAutoLayout';
|
import { useAutoLayout } from './useAutoLayout';
|
||||||
import { useScrollBoundaries } from './useScrollBoundaries';
|
import { useScrollBoundaries } from './useScrollBoundaries';
|
||||||
@@ -36,8 +37,9 @@ const edgeTypes = {
|
|||||||
const EditorNew = ({ flow }) => {
|
const EditorNew = ({ flow }) => {
|
||||||
const [updateStep] = useMutation(UPDATE_STEP);
|
const [updateStep] = useMutation(UPDATE_STEP);
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
const [createStep, { loading: stepCreationInProgress }] =
|
|
||||||
useMutation(CREATE_STEP);
|
const { mutateAsync: createStep, isPending: isCreateStepPending } =
|
||||||
|
useCreateStep(flow?.id);
|
||||||
|
|
||||||
const [nodes, setNodes, onNodesChange] = useNodesState(
|
const [nodes, setNodes, onNodesChange] = useNodesState(
|
||||||
generateInitialNodes(flow),
|
generateInitialNodes(flow),
|
||||||
@@ -111,26 +113,13 @@ const EditorNew = ({ flow }) => {
|
|||||||
|
|
||||||
const onAddStep = useCallback(
|
const onAddStep = useCallback(
|
||||||
async (previousStepId) => {
|
async (previousStepId) => {
|
||||||
const mutationInput = {
|
const { data: createdStep } = await createStep({ previousStepId });
|
||||||
previousStep: {
|
|
||||||
id: previousStepId,
|
|
||||||
},
|
|
||||||
flow: {
|
|
||||||
id: flow.id,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const {
|
console.log('CHECK THIS OUT!', createdStep);
|
||||||
data: { createStep: createdStep },
|
|
||||||
} = await createStep({
|
|
||||||
variables: { input: mutationInput },
|
|
||||||
});
|
|
||||||
|
|
||||||
const createdStepId = createdStep.id;
|
createdStepIdRef.current = createdStep.id;
|
||||||
await queryClient.invalidateQueries({ queryKey: ['flows', flow.id] });
|
|
||||||
createdStepIdRef.current = createdStepId;
|
|
||||||
},
|
},
|
||||||
[flow.id, createStep, queryClient],
|
[createStep],
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -260,7 +249,7 @@ const EditorNew = ({ flow }) => {
|
|||||||
>
|
>
|
||||||
<EdgesContext.Provider
|
<EdgesContext.Provider
|
||||||
value={{
|
value={{
|
||||||
stepCreationInProgress,
|
stepCreationInProgress: isCreateStepPending,
|
||||||
onAddStep,
|
onAddStep,
|
||||||
flowActive: flow.active,
|
flowActive: flow.active,
|
||||||
}}
|
}}
|
||||||
|
24
packages/web/src/hooks/useCreateStep.js
Normal file
24
packages/web/src/hooks/useCreateStep.js
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
||||||
|
|
||||||
|
import api from 'helpers/api';
|
||||||
|
|
||||||
|
export default function useCreateStep(flowId) {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
const query = useMutation({
|
||||||
|
mutationFn: async ({ previousStepId }) => {
|
||||||
|
const { data } = await api.post(`/v1/flows/${flowId}/steps`, {
|
||||||
|
previousStepId,
|
||||||
|
});
|
||||||
|
|
||||||
|
return data;
|
||||||
|
},
|
||||||
|
|
||||||
|
onSuccess: () => {
|
||||||
|
queryClient.invalidateQueries({
|
||||||
|
queryKey: ['flows', flowId],
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return query;
|
||||||
|
}
|
Reference in New Issue
Block a user