From fcf6e063371ccb9e99462c71e8fb0d20a8ba43fe Mon Sep 17 00:00:00 2001 From: Subina Date: Wed, 15 May 2024 10:58:43 +0545 Subject: [PATCH] USe gql mutation to create new organization --- .../general/AddOrganizationModal/index.tsx | 128 +++++++++++++----- .../SearchStakeholder/index.tsx | 3 +- app/components/lead/LeadInput/index.tsx | 38 +++--- 3 files changed, 113 insertions(+), 56 deletions(-) diff --git a/app/components/general/AddOrganizationModal/index.tsx b/app/components/general/AddOrganizationModal/index.tsx index 2269dd2b45..6243cc56b2 100644 --- a/app/components/general/AddOrganizationModal/index.tsx +++ b/app/components/general/AddOrganizationModal/index.tsx @@ -1,5 +1,5 @@ import React, { useCallback } from 'react'; -import { useQuery, gql } from '@apollo/client'; +import { useMutation, useQuery, gql } from '@apollo/client'; import { useForm, requiredCondition, @@ -19,23 +19,19 @@ import { } from '@the-deep/deep-ui'; import _ts from '#ts'; -import { - useLazyRequest, -} from '#base/utils/restRequest'; import { OrganizationTypesQuery, OrganizationTypesQueryVariables, OrganizationTypeType, + CreateOrganizationMutation, + CreateOrganizationMutationVariables, } from '#generated/types'; -import { - Organization, -} from '#types'; -import { OrganizationItemType } from '#components/general/AddStakeholderModal/SearchStakeholder/Stakeholder'; import styles from './styles.css'; -type FormType = Partial> -type FormSchema = ObjectSchema; +type FormType = NonNullable; +type PartialFormType = Partial; +type FormSchema = ObjectSchema; type FormSchemaFields = ReturnType; const organizationSchema: FormSchema = { fields: (): FormSchemaFields => ({ @@ -47,10 +43,12 @@ const organizationSchema: FormSchema = { }), }; -const organizationTypeKeySelector = (d: OrganizationTypeType): number => Number(d.id); +const organizationTypeKeySelector = (d: OrganizationTypeType): string => d.id; const organizationTypeLabelSelector = (d: OrganizationTypeType): string => d.title; -const defaultFormValue: FormType = {}; +const defaultFormValue: PartialFormType = {}; + +export type OrganizationItemType = NonNullable['result']>; export interface Props { onModalClose: () => void; @@ -70,6 +68,41 @@ const ORGANIZATION_TYPES = gql` } `; +const CREATE_ORGANIZATION = gql` + mutation CreateOrganization ( + $data: OrganizationInputType! + ) { + organizationCreate ( + data: $data + ) { + errors + ok + result { + id + longName + shortName + title + url + verified + organizationType { + id + title + shortName + description + } + logo { + id + title + file { + name + url + } + } + } + } + } +`; + function AddOrganizationModal(props: Props) { const { onModalClose, @@ -85,30 +118,47 @@ function AddOrganizationModal(props: Props) { ORGANIZATION_TYPES, ); - const { - pending: organizationPostPending, - trigger: createOrganization, - } = useLazyRequest({ - url: 'server://organizations/', - method: 'POST', - body: (ctx) => ctx, - onSuccess: (response) => { - if (onOrganizationAdd) { - onOrganizationAdd({ - ...response, - id: response.id, - }); - } - alert.show( - 'Successfully created new organization.', - { - variant: 'success', - }, - ); - onModalClose(); + const [ + createOrganization, + { loading: createOrganizationPending }, + ] = useMutation( + CREATE_ORGANIZATION, + { + onCompleted: (response) => { + if (!response?.organizationCreate?.result) { + return; + } + + if (response?.organizationCreate?.ok) { + alert.show( + 'Successfully created new organization.', + { + variant: 'success', + }, + ); + onModalClose(); + if (onOrganizationAdd) { + onOrganizationAdd(response?.organizationCreate?.result); + } + } else { + alert.show( + 'Failed to create organization.', + { + variant: 'error', + }, + ); + } + }, + onError: () => { + alert.show( + 'Failed to create organization.', + { + variant: 'error', + }, + ); + }, }, - failureMessage: 'Failed to create organization.', - }); + ); const { pristine, @@ -126,13 +176,17 @@ function AddOrganizationModal(props: Props) { const submit = createSubmitHandler( validate, setError, - createOrganization, + (val) => createOrganization({ + variables: { + data: val as FormType, + }, + }), ); submit(); }, [setError, validate, createOrganization], ); - const pending = organizationTypesPending || organizationPostPending; + const pending = organizationTypesPending || createOrganizationPending; return ( (props: Props) { getUserToken(); }, [getUserToken]); - const handleOrganizationAdd = useCallback((val: { id: string; title: string }) => { - const transformedVal = { - id: String(val.id), - title: val.title, - }; - if (organizationAddType === 'publisher') { - setFieldValue(transformedVal.id, 'source'); - onSourceOrganizationOptionsChange((oldVal) => [...oldVal ?? [], transformedVal]); - } else if (organizationAddType === 'author') { - setFieldValue((oldVal: string[] | undefined | null) => [...oldVal ?? [], transformedVal.id], 'authors'); - onAuthorOrganizationOptionsChange((oldVal) => [...oldVal ?? [], transformedVal]); - } - }, [ - organizationAddType, - setFieldValue, - onSourceOrganizationOptionsChange, - onAuthorOrganizationOptionsChange, - ]); + const handleOrganizationAdd = useCallback( + (val: { id: string; title: string }) => { + const transformedVal = { + id: val.id, + title: val.title, + }; + if (organizationAddType === 'publisher') { + setFieldValue(transformedVal.id, 'source'); + onSourceOrganizationOptionsChange((oldVal) => [...oldVal ?? [], transformedVal]); + } else if (organizationAddType === 'author') { + setFieldValue((oldVal: string[] | undefined | null) => [...oldVal ?? [], transformedVal.id], 'authors'); + onAuthorOrganizationOptionsChange((oldVal) => [...oldVal ?? [], transformedVal]); + } + }, [ + organizationAddType, + setFieldValue, + onSourceOrganizationOptionsChange, + onAuthorOrganizationOptionsChange, + ], + ); const pending = pendingFromProps || pendingUserToken || webInfoPending || rawWebInfoPending;