Skip to content

Commit

Permalink
Use graphql mutation to clone analytical framework
Browse files Browse the repository at this point in the history
  • Loading branch information
subinasr authored and AdityaKhatri committed Aug 9, 2024
1 parent 432ac63 commit 87eefe6
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 41 deletions.
130 changes: 89 additions & 41 deletions app/views/ProjectEdit/Framework/CloneFrameworkModal/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React, { useCallback } from 'react';
import React, { useCallback, useMemo } from 'react';
import {
_cs,
isDefined,
} from '@togglecorp/fujs';
import { useMutation, gql } from '@apollo/client';
import {
Modal,
useAlert,
Expand All @@ -20,39 +21,49 @@ import {
createSubmitHandler,
} from '@togglecorp/toggle-form';

import { useLazyRequest } from '#base/utils/restRequest';
import {
AnalysisFrameworkCloneMutation,
AnalysisFrameworkCloneMutationVariables,
} from '#generated/types';
import _ts from '#ts';

import styles from './styles.css';

type FormType = {
title: string;
description?: string;
};
const CLONE_FRAMEWORK = gql`
mutation analysisFrameworkClone(
$data: AnalysisFrameworkCloneInputType!,
) {
analysisFrameworkClone(
data: $data
) {
errors
ok
result {
id
title
}
}
}
`;

interface Framework {
id: number;
}
type FormType = NonNullable<AnalysisFrameworkCloneMutationVariables['data']>;
type PartialFormType = Partial<FormType>;

type FormSchema = ObjectSchema<PartialForm<FormType>>;
type FormSchema = ObjectSchema<PartialForm<PartialFormType>>;
type FormSchemaFields = ReturnType<FormSchema['fields']>

const schema: FormSchema = {
fields: (): FormSchemaFields => ({
afId: [requiredStringCondition],
project: [requiredStringCondition],
title: [requiredStringCondition],
description: [],
}),
};

const defaultFormValue: PartialForm<FormType> = {};

interface ValueToSend {
title: string;
description?: string;
}

interface Props {
className?: string;
projectId: string;
frameworkToClone: string;
frameworkTitle?: string;
frameworkDescription?: string;
Expand All @@ -63,17 +74,30 @@ interface Props {
function CloneFrameworkModal(props: Props) {
const {
className,
projectId,
frameworkToClone,
frameworkTitle,
frameworkDescription,
onCloneSuccess,
onModalClose,
} = props;

const formValueFromProps: PartialForm<FormType> = frameworkTitle ? {
title: `${frameworkTitle} (cloned)`,
description: frameworkDescription,
} : defaultFormValue;
const defaultFormValue: PartialFormType = useMemo(() => ({
afId: frameworkToClone,
project: projectId,
}), [frameworkToClone, projectId]);

const formValueFromProps: PartialFormType = useMemo(() => (
frameworkTitle ? {
...defaultFormValue,
title: `${frameworkTitle} (cloned)`,
description: frameworkDescription,
} : defaultFormValue
), [
frameworkDescription,
frameworkTitle,
defaultFormValue,
]);

const {
pristine,
Expand All @@ -87,36 +111,60 @@ function CloneFrameworkModal(props: Props) {

const error = getErrorObject(riskyError);

const {
pending: pendingCloneAction,
trigger: triggerCreateFramework,
} = useLazyRequest<Framework, ValueToSend>({
url: `server://clone-analysis-framework/${frameworkToClone}/`,
method: 'POST',
body: (ctx) => ctx,
onSuccess: (response) => {
onCloneSuccess(String(response.id));
alert.show(
_ts('projectEdit', 'cloneFrameworkSuccessMessage'),
{ variant: 'success' },
);
const [
cloneFramework,
{ loading: cloneFrameworkPending },
] = useMutation<AnalysisFrameworkCloneMutation, AnalysisFrameworkCloneMutationVariables>(
CLONE_FRAMEWORK,
{
onCompleted: (response) => {
const cloneFrameworkResponse = response
?.analysisFrameworkClone;
if (!cloneFrameworkResponse?.result) {
return;
}
if (cloneFrameworkResponse?.ok) {
alert.show(
'Successfully cloned framework.',
{ variant: 'success' },
);
onCloneSuccess(cloneFrameworkResponse?.result?.id);
} else {
alert.show(
'Failed to clone framework.',
{ variant: 'error' },
);
}
},
onError: () => {
alert.show(
'Failed to clone framework',
{ variant: 'error' },
);
},
},
failureMessage: _ts('projectEdit', 'projectMembershipPostFailed'),
});

);
const handleSubmit = useCallback(
() => {
const submit = createSubmitHandler(
validate,
setError,
(val) => triggerCreateFramework(val as ValueToSend),
(val) => cloneFramework({
variables: {
data: val as FormType,
},
}),
);
submit();
},
[setError, validate, triggerCreateFramework],
[
setError,
validate,
cloneFramework,
],
);

const pendingRequests = pendingCloneAction;
const pendingRequests = cloneFrameworkPending;

return (
<Modal
Expand All @@ -135,7 +183,7 @@ function CloneFrameworkModal(props: Props) {
name="submit"
variant="primary"
type="submit"
disabled={pristine || pendingCloneAction}
disabled={pristine || cloneFrameworkPending}
onClick={handleSubmit}
>
{_ts('projectEdit', 'submitLabel')}
Expand Down
1 change: 1 addition & 0 deletions app/views/ProjectEdit/Framework/FrameworkDetail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ function FrameworkDetail(props: Props) {
</Tabs>
{frameworkCloneModalShown && (
<CloneFrameworkModal
projectId={projectId}
frameworkToClone={frameworkId}
frameworkTitle={frameworkDetails?.title}
frameworkDescription={frameworkDetails?.description}
Expand Down

0 comments on commit 87eefe6

Please sign in to comment.