Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use graphql for analysis clone feature #3010

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 106 additions & 37 deletions app/views/AnalysisDashboard/Analysis/AnalysisCloneModal/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import React, { useCallback } from 'react';
import React, {
useCallback,
useMemo,
} from 'react';
import {
compareDate,
} from '@togglecorp/fujs';
import { gql, useMutation } from '@apollo/client';
import {
Modal,
Button,
Expand All @@ -11,6 +15,7 @@ import {
PendingMessage,
} from '@the-deep/deep-ui';
import {
removeNull,
useForm,
ObjectSchema,
PartialForm,
Expand All @@ -19,19 +24,36 @@ import {
createSubmitHandler,
} from '@togglecorp/toggle-form';

import { useLazyRequest } from '#base/utils/restRequest';
import NonFieldError from '#components/NonFieldError';
import { transformToFormError, ObjectError } from '#base/utils/errorTransform';
import _ts from '#ts';
import {
AnalysisCloneInputType,
AnalysisCloneMutation,
AnalysisCloneMutationVariables,
} from '#generated/types';

import styles from './styles.css';

type FormType = {
title: string;
startDate: string;
endDate: string;
};
const CLONE_ANALYSIS = gql`
mutation AnalysisClone(
$projectId: ID!,
$data: AnalysisCloneInputType!,
) {
project(id: $projectId) {
analysisClone(data: $data) {
ok
errors
result {
id
}
}
}
}
`;

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

const schema: FormSchema = {
Expand All @@ -52,19 +74,14 @@ const schema: FormSchema = {
},
};

const defaultFormValue: PartialForm<FormType> = {};

interface CloneProperties {
title: string;
endDate: string;
startDate: string;
}

interface Props {
onClose: () => void;
projectId: string;
analysisId: string;
onClone: () => void;
onClose: () => void;
title: string;
startDate: string | null | undefined;
endDate: string;
}

function AnalysisCloneModal(props: Props) {
Expand All @@ -73,48 +90,100 @@ function AnalysisCloneModal(props: Props) {
projectId,
analysisId,
onClone,
title,
startDate,
endDate,
} = props;

const alert = useAlert();

const defaultFormValues: PartialForm<FormType> = useMemo(() => ({
title: `${title} (cloned)`,
startDate,
endDate,
}), [
title,
startDate,
endDate,
]);

const {
pristine,
value,
error: riskyError,
setFieldValue,
validate,
setError,
} = useForm(schema, defaultFormValue);
} = useForm(schema, defaultFormValues);

const error = getErrorObject(riskyError);

const {
pending: pendingAnalysisClone,
trigger: triggerAnalysisClone,
} = useLazyRequest<CloneProperties, FormType>({
url: `server://projects/${projectId}/analysis/${analysisId}/clone/`,
method: 'POST',
body: (ctx) => ctx,
onSuccess: () => {
alert.show(
_ts('analysis.cloneModal', 'analysisCloneSuccessful'),
{
variant: 'success',
},
);
onClone();
const [
triggerAnalysisClone,
{
loading: pendingAnalysisClone,
},
] = useMutation<AnalysisCloneMutation, AnalysisCloneMutationVariables>(
CLONE_ANALYSIS,
{
onCompleted: (response) => {
const {
ok,
errors,
} = response?.project?.analysisClone ?? {};

if (ok) {
alert.show(
_ts('analysis.cloneModal', 'analysisCloneSuccessful'),
{
variant: 'success',
},
);
onClone();
}
if (errors) {
const formError = transformToFormError(removeNull(errors) as ObjectError[]);
setError(formError);
}
},
onError: () => {
alert.show(
_ts('analysis.cloneModal', 'analysisCloneFailed'),
{
variant: 'error',
},
);
},
},
failureMessage: _ts('analysis.cloneModal', 'analysisCloneFailed'),
});
);

const handleSubmitButtonClick = useCallback(() => {
const submit = createSubmitHandler(
validate,
setError,
(val) => triggerAnalysisClone(val as FormType),
(val) => {
const finalValue = val as AnalysisCloneInputType;
triggerAnalysisClone({
variables: {
data: {
analysisId,
endDate: finalValue.endDate,
startDate: finalValue.startDate,
title: finalValue.title,
},
projectId,
},
});
},
);
submit();
}, [triggerAnalysisClone, setError, validate]);
}, [
analysisId,
projectId,
triggerAnalysisClone,
setError,
validate,
]);

const pending = pendingAnalysisClone;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
.modal {
.body {
padding: calc(var(--dui-spacing-medium) - var(--spacing-small));
display: flex;
flex-direction: column;
gap: var(--dui-spacing-medium);

.title {
margin: 0 var(--spacing-small);
}
.inline {
display: flex;
margin-top: var(--spacing-medium);

.date {
margin: 0 var(--spacing-small);
}
gap: var(--dui-spacing-medium);
}
}
}
3 changes: 3 additions & 0 deletions app/views/AnalysisDashboard/Analysis/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,9 @@ function Analysis(props: Props) {
onClose={setModalHidden}
projectId={activeProject}
analysisId={analysisId}
title={title}
startDate={startDate}
endDate={endDate}
onClone={handleCloneSuccess}
/>
)}
Expand Down
Loading