feat: make flows enablable

This commit is contained in:
Ali BARIN
2022-03-22 18:16:36 +01:00
committed by Ömer Faruk Aydın
parent 932145e3a1
commit c9bf7c9e21
3 changed files with 40 additions and 8 deletions

View File

@@ -7,13 +7,13 @@ import { Box, TextField } from './style';
type EditableTypographyProps = TypographyProps & { type EditableTypographyProps = TypographyProps & {
children: string; children: string;
onNameSubmit?: (value: string) => void; onConfirm?: (value: string) => void;
}; };
const noop = () => null; const noop = () => null;
function EditableTypography(props: EditableTypographyProps) { function EditableTypography(props: EditableTypographyProps) {
const { children, onNameSubmit = noop, sx, ...typographyProps } = props; const { children, onConfirm = noop, sx, ...typographyProps } = props;
const [editing, setEditing] = React.useState(false); const [editing, setEditing] = React.useState(false);
const handleClick = React.useCallback(() => { const handleClick = React.useCallback(() => {
@@ -28,7 +28,7 @@ function EditableTypography(props: EditableTypographyProps) {
const target = event.target as HTMLInputElement; const target = event.target as HTMLInputElement;
if (event.key === 'Enter') { if (event.key === 'Enter') {
if (target.value !== children) { if (target.value !== children) {
await onNameSubmit(target.value); await onConfirm(target.value);
} }
setEditing(false); setEditing(false);
@@ -39,11 +39,11 @@ function EditableTypography(props: EditableTypographyProps) {
const value = event.target.value; const value = event.target.value;
if (value !== children) { if (value !== children) {
await onNameSubmit(value); await onConfirm(value);
} }
setEditing(false); setEditing(false);
}, [onNameSubmit, children]); }, [onConfirm, children]);
let component = ( let component = (
<Typography {...typographyProps}> <Typography {...typographyProps}>

View File

@@ -12,6 +12,7 @@ import EditableTypography from 'components/EditableTypography';
import Container from 'components/Container'; import Container from 'components/Container';
import Editor from 'components/Editor'; import Editor from 'components/Editor';
import useFormatMessage from 'hooks/useFormatMessage'; import useFormatMessage from 'hooks/useFormatMessage';
import { UPDATE_FLOW_STATUS } from 'graphql/mutations/update-flow-status';
import { UPDATE_FLOW } from 'graphql/mutations/update-flow'; import { UPDATE_FLOW } from 'graphql/mutations/update-flow';
import { GET_FLOW } from 'graphql/queries/get-flow'; import { GET_FLOW } from 'graphql/queries/get-flow';
import type { IFlow } from '@automatisch/types'; import type { IFlow } from '@automatisch/types';
@@ -21,6 +22,7 @@ export default function EditorLayout(): React.ReactElement {
const { flowId } = useParams(); const { flowId } = useParams();
const formatMessage = useFormatMessage(); const formatMessage = useFormatMessage();
const [updateFlow] = useMutation(UPDATE_FLOW); const [updateFlow] = useMutation(UPDATE_FLOW);
const [updateFlowStatus] = useMutation(UPDATE_FLOW_STATUS);
const { data, loading } = useQuery(GET_FLOW, { variables: { id: flowId }}); const { data, loading } = useQuery(GET_FLOW, { variables: { id: flowId }});
const flow: IFlow = data?.getFlow; const flow: IFlow = data?.getFlow;
@@ -33,8 +35,8 @@ export default function EditorLayout(): React.ReactElement {
}, },
}, },
optimisticResponse: { optimisticResponse: {
__typename: 'Mutation',
updateFlow: { updateFlow: {
__typename: 'Flow',
id: flow?.id, id: flow?.id,
name, name,
} }
@@ -42,6 +44,26 @@ export default function EditorLayout(): React.ReactElement {
}); });
}, [flow?.id]); }, [flow?.id]);
const onFlowStatusUpdate = React.useCallback(async (event: React.ChangeEvent<HTMLInputElement>) => {
const active = event.target.checked;
await updateFlowStatus({
variables: {
input: {
id: flowId,
active,
},
},
optimisticResponse: {
updateFlowStatus: {
__typename: 'Flow',
id: flow?.id,
active,
}
}
});
}, [flow?.id]);
return ( return (
<> <>
<Stack direction="column" height="100%"> <Stack direction="column" height="100%">
@@ -58,7 +80,7 @@ export default function EditorLayout(): React.ReactElement {
{!loading && ( {!loading && (
<EditableTypography <EditableTypography
variant="body1" variant="body1"
onNameSubmit={onFlowNameUpdate} onConfirm={onFlowNameUpdate}
noWrap noWrap
sx={{ display: 'flex', flex: 1, maxWidth: '50vw', ml: 2 }} sx={{ display: 'flex', flex: 1, maxWidth: '50vw', ml: 2 }}
> >
@@ -70,7 +92,7 @@ export default function EditorLayout(): React.ReactElement {
<Box pr={1}> <Box pr={1}>
<FormControlLabel <FormControlLabel
control={ control={
<Switch checked={flow?.active ?? false} /> <Switch checked={flow?.active ?? false} onChange={onFlowStatusUpdate} />
} }
label={flow?.active ? formatMessage('flow.active') : formatMessage('flow.inactive')} label={flow?.active ? formatMessage('flow.active') : formatMessage('flow.inactive')}
labelPlacement="start" labelPlacement="start"

View File

@@ -0,0 +1,10 @@
import { gql } from '@apollo/client';
export const UPDATE_FLOW_STATUS = gql`
mutation UpdateFlowStatus($input: UpdateFlowStatusInput) {
updateFlowStatus(input: $input) {
id
active
}
}
`;