Skip to content

Commit

Permalink
Add graphql mutation for patch framework in project
Browse files Browse the repository at this point in the history
  • Loading branch information
shreeyash07 committed Jul 31, 2024
1 parent e624d10 commit cff7989
Showing 1 changed file with 73 additions and 28 deletions.
101 changes: 73 additions & 28 deletions app/views/ProjectEdit/Framework/FrameworkDetail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
isDefined,
} from '@togglecorp/fujs';
import { generatePath } from 'react-router-dom';
import { gql, useQuery } from '@apollo/client';
import { gql, useMutation, useQuery } from '@apollo/client';
import {
IoCopyOutline,
IoCheckmark,
Expand Down Expand Up @@ -35,6 +35,7 @@ import {
TextOutput,
RawButton,
Button,
useAlert,
} from '@the-deep/deep-ui';
import { removeNull } from '@togglecorp/toggle-form';

Expand All @@ -44,14 +45,14 @@ import {
DeepReplace,
} from '#utils/types';
import FrameworkImageButton from '#components/framework/FrameworkImageButton';
import { useLazyRequest } from '#base/utils/restRequest';
import { ProjectContext } from '#base/context/ProjectContext';
import { useModalState } from '#hooks/stateManagement';
import routes from '#base/configs/routes';
import Section from '#components/entry/Section';
import _ts from '#ts';
import { ProjectDetails } from '#types';
import {
AnalyticalFrameworkPatchMutation,
AnalyticalFrameworkPatchMutationVariables,
FrameworkDetailsQuery,
FrameworkDetailsQueryVariables,
WidgetType as WidgetRaw,
Expand All @@ -66,6 +67,25 @@ function noop() {}

const emptyObject = {};

const ANALYTICAL_FRAMEWORK_PATCH = gql`
mutation AnalyticalFrameworkPatch(
$projectId: ID!,
$frameworkId: ID,
) {
project(id: $projectId) {
projectUpdate(data: {analysisFramework: $frameworkId}) {
ok
errors
result {
analysisFramework {
id
}
}
}
}
}
`;

const FRAMEWORK_DETAILS = gql`
${FRAMEWORK_FRAGMENT}
query FrameworkDetails(
Expand Down Expand Up @@ -155,6 +175,7 @@ function FrameworkDetail(props: Props) {
const { setProject } = useContext(ProjectContext);

const [activeTab, setActiveTab] = useState<'primary' | 'secondary' | undefined>('primary');
const alert = useAlert();
const [selectedSection, setSelectedSection] = useState<string | undefined>();

const variables = useMemo(
Expand Down Expand Up @@ -236,31 +257,50 @@ function FrameworkDetail(props: Props) {
frameworkDetailsResponse?.projects,
]);

const {
pending: projectPatchPending,
trigger: projectPatch,
} = useLazyRequest<ProjectDetails>({
url: `server://projects/${projectId}/`,
method: 'PATCH',
body: ({
analysisFramework: frameworkId,
}),
onSuccess: (response) => {
setProject((oldProjectDetails) => {
const { analysisFramework } = response;
if (!oldProjectDetails || isNotDefined(analysisFramework)) {
return oldProjectDetails;
const [
projectPatch,
{
loading: projectPatchPending,
},
] = useMutation<AnalyticalFrameworkPatchMutation, AnalyticalFrameworkPatchMutationVariables>(
ANALYTICAL_FRAMEWORK_PATCH,
{
onCompleted: (response) => {
if (!response || !response.project?.projectUpdate) {
return;
}
return ({
...oldProjectDetails,
analysisFramework: {
id: String(analysisFramework),
},
});
});
const {
ok,
errors,
result,
} = response.project.projectUpdate;

const analysisFrameworkId = result?.analysisFramework?.id;

if (errors) {
alert.show(
'Failed to create assessment registry.',
{ variant: 'error' },
);
} else if (ok) {
if (!analysisFrameworkId) {
return;
}
setProject((oldProjectDetails) => {
if (!oldProjectDetails) {
return oldProjectDetails;
}
return ({
...oldProjectDetails,
analysisFramework: {
id: analysisFrameworkId,
},
});
});
}
},
},
failureMessage: _ts('projectEdit', 'projectDetailsLabel'),
});
);

const itemRendererParams = useCallback((_: string, data: { title: string; id: string }) => ({
className: styles.projectTitle,
Expand All @@ -275,8 +315,13 @@ function FrameworkDetail(props: Props) {
] = useModalState(false);

const handleUseFrameworkClick = useCallback(() => {
projectPatch(null);
}, [projectPatch]);
projectPatch({
variables: {
projectId,
frameworkId,
},
});
}, [projectPatch, projectId, frameworkId]);

const handleNewFrameworkAddSuccess = useCallback((newFrameworkId: string) => {
onFrameworkCreate(newFrameworkId);
Expand Down

0 comments on commit cff7989

Please sign in to comment.