From cf5fb09da412b93c3ee9e84ee24c178da10ba8e2 Mon Sep 17 00:00:00 2001 From: Aditya Khatri Date: Tue, 17 Dec 2024 10:02:03 +0545 Subject: [PATCH] Use graphql for analysis clone feature --- .../Analysis/AnalysisCloneModal/index.tsx | 130 +++++++++++++----- .../Analysis/AnalysisCloneModal/styles.css | 13 +- .../AnalysisDashboard/Analysis/index.tsx | 3 + 3 files changed, 100 insertions(+), 46 deletions(-) diff --git a/app/views/AnalysisDashboard/Analysis/AnalysisCloneModal/index.tsx b/app/views/AnalysisDashboard/Analysis/AnalysisCloneModal/index.tsx index 8c5d409664..5bcf18f74e 100644 --- a/app/views/AnalysisDashboard/Analysis/AnalysisCloneModal/index.tsx +++ b/app/views/AnalysisDashboard/Analysis/AnalysisCloneModal/index.tsx @@ -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, @@ -19,19 +23,35 @@ import { createSubmitHandler, } from '@togglecorp/toggle-form'; -import { useLazyRequest } from '#base/utils/restRequest'; import NonFieldError from '#components/NonFieldError'; 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>; +type FormType = PartialForm; +type FormSchema = ObjectSchema>; type FormSchemaFields = ReturnType; const schema: FormSchema = { @@ -52,19 +72,14 @@ const schema: FormSchema = { }, }; -const defaultFormValue: PartialForm = {}; - -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) { @@ -73,10 +88,23 @@ function AnalysisCloneModal(props: Props) { projectId, analysisId, onClone, + title, + startDate, + endDate, } = props; const alert = useAlert(); + const defaultFormValues: PartialForm = useMemo(() => ({ + title: `${title} (cloned)`, + startDate, + endDate, + }), [ + title, + startDate, + endDate, + ]); + const { pristine, value, @@ -84,37 +112,65 @@ function AnalysisCloneModal(props: Props) { setFieldValue, validate, setError, - } = useForm(schema, defaultFormValue); + } = useForm(schema, defaultFormValues); const error = getErrorObject(riskyError); - const { - pending: pendingAnalysisClone, - trigger: triggerAnalysisClone, - } = useLazyRequest({ - 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( + CLONE_ANALYSIS, + { + onCompleted: () => { + alert.show( + _ts('analysis.cloneModal', 'analysisCloneSuccessful'), + { + variant: 'success', + }, + ); + onClone(); + }, + 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; diff --git a/app/views/AnalysisDashboard/Analysis/AnalysisCloneModal/styles.css b/app/views/AnalysisDashboard/Analysis/AnalysisCloneModal/styles.css index 07f01af670..b0ac15a186 100644 --- a/app/views/AnalysisDashboard/Analysis/AnalysisCloneModal/styles.css +++ b/app/views/AnalysisDashboard/Analysis/AnalysisCloneModal/styles.css @@ -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); } } } diff --git a/app/views/AnalysisDashboard/Analysis/index.tsx b/app/views/AnalysisDashboard/Analysis/index.tsx index 5c0692fc90..e57bd79763 100644 --- a/app/views/AnalysisDashboard/Analysis/index.tsx +++ b/app/views/AnalysisDashboard/Analysis/index.tsx @@ -453,6 +453,9 @@ function Analysis(props: Props) { onClose={setModalHidden} projectId={activeProject} analysisId={analysisId} + title={title} + startDate={startDate} + endDate={endDate} onClone={handleCloneSuccess} /> )}