fix: do not update step on initial render

This commit is contained in:
Ali BARIN
2022-01-28 16:17:23 +01:00
committed by Ömer Faruk Aydın
parent bb3183959e
commit 3925de13a7
3 changed files with 67 additions and 14 deletions

View File

@@ -50,10 +50,9 @@ export default function Editor(props: EditorProps): React.ReactElement {
const { flow } = props; const { flow } = props;
const onStepChange = React.useCallback((step: any) => { const onStepChange = React.useCallback((step: any) => {
const mutationInput = { const mutationInput: Record<string, unknown> = {
id: step.id, id: step.id,
key: step.key, key: step.key,
appKey: step.appKey,
previousStepId: step.previousStepId, previousStepId: step.previousStepId,
connection: { connection: {
id: step.connection?.id id: step.connection?.id
@@ -63,6 +62,10 @@ export default function Editor(props: EditorProps): React.ReactElement {
}, },
}; };
if (step.appKey) {
mutationInput.appKey = step.appKey;
}
updateStep({ variables: { input: mutationInput } }); updateStep({ variables: { input: mutationInput } });
}, [updateStep, flow.id]); }, [updateStep, flow.id]);

View File

@@ -37,6 +37,7 @@ const getOption = (options: Record<string, unknown>[], appKey: string) => option
export default function FlowStep(props: FlowStepProps): React.ReactElement | null { export default function FlowStep(props: FlowStepProps): React.ReactElement | null {
const { collapsed, index, onChange } = props; const { collapsed, index, onChange } = props;
const [step, setStep] = React.useState<Step>(props.step); const [step, setStep] = React.useState<Step>(props.step);
const initialRender = React.useRef<boolean>(true);
const formatMessage = useFormatMessage(); const formatMessage = useFormatMessage();
const [currentSubstep, setCurrentSubstep] = React.useState<number | null>(0); const [currentSubstep, setCurrentSubstep] = React.useState<number | null>(0);
const { data } = useQuery(GET_APPS) const { data } = useQuery(GET_APPS)
@@ -44,11 +45,24 @@ export default function FlowStep(props: FlowStepProps): React.ReactElement | nul
// emit the step change to the parent component // emit the step change to the parent component
React.useEffect(() => { React.useEffect(() => {
onChange?.(step); if (!initialRender.current) {
onChange?.(step);
} else {
initialRender.current = false;
}
}, [step, onChange]); }, [step, onChange]);
const appAndEventOptions = React.useMemo(() => apps?.map(optionGenerator), [apps]); const appAndEventOptions = React.useMemo(() => apps?.map(optionGenerator), [apps]);
const onAppAndEventChange = React.useCallback((event: React.SyntheticEvent, selectedOption: unknown) => {
if (typeof selectedOption === 'object') {
const typedSelectedOption = selectedOption as { value: string; };
const option: { value: string } = typedSelectedOption;
const appKey = option.value as string;
setStep((step) => ({ ...step, appKey }));
}
}, []);
if (!apps) return null; if (!apps) return null;
const app = apps.find((currentApp: App) => currentApp.key === step.appKey); const app = apps.find((currentApp: App) => currentApp.key === step.appKey);
@@ -58,16 +72,6 @@ export default function FlowStep(props: FlowStepProps): React.ReactElement | nul
const toggleSubstep = (substepIndex: number) => setCurrentSubstep((value) => value !== substepIndex ? substepIndex : null); const toggleSubstep = (substepIndex: number) => setCurrentSubstep((value) => value !== substepIndex ? substepIndex : null);
const onAppAndEventChange = (event: React.SyntheticEvent, selectedOption: unknown) => {
if (typeof selectedOption === 'object') {
const typedSelectedOption = selectedOption as { value: string; };
const option: { value: string } = typedSelectedOption;
const appKey = option.value as string;
const updatedStep = { ...step, appKey };
setStep(updatedStep);
}
}
return ( return (
<Wrapper elevation={collapsed ? 1 : 4} onClick={onOpen}> <Wrapper elevation={collapsed ? 1 : 4} onClick={onOpen}>
<Header collapsed={collapsed}> <Header collapsed={collapsed}>

View File

@@ -3,25 +3,71 @@ import { gql } from '@apollo/client';
export const GET_APPS = gql` export const GET_APPS = gql`
query GetApps($name: String) { query GetApps($name: String) {
getApps(name: $name) { getApps(name: $name) {
key
name name
key
iconUrl iconUrl
docUrl docUrl
primaryColor primaryColor
connectionCount
fields { fields {
key key
label label
type type
required required
readOnly readOnly
value
placeholder placeholder
description description
docUrl docUrl
clickToCopy clickToCopy
} }
authenticationSteps {
step
type
name
arguments {
name
value
type
properties {
name
value
}
}
}
reconnectionSteps {
step
type
name
arguments {
name
value
type
properties {
name
value
}
}
}
connections { connections {
id id
} }
triggers {
name
key
description
subSteps {
name
}
}
actions {
name
key
description
subSteps {
name
}
}
} }
} }
`; `;