diff --git a/app/hooks/cost-surface/index.ts b/app/hooks/cost-surface/index.ts index 1d3c852957..fbc6cb9143 100644 --- a/app/hooks/cost-surface/index.ts +++ b/app/hooks/cost-surface/index.ts @@ -1,4 +1,4 @@ -import { useQuery, QueryObserverOptions } from 'react-query'; +import { useQuery, QueryObserverOptions, useMutation } from 'react-query'; import { useSession } from 'next-auth/react'; @@ -16,17 +16,17 @@ export function useProjectCostSurfaces( const mockData: CostSurface[] = [ { - id: 'Cost Surface Rwanda A', + id: 'gfehrtf22534geyg', name: 'Cost Surface Rwanda A', scenarioUsageCount: 3, }, { - id: 'Cost Surface Rwanda B', + id: 'rfjghhrtersdtbkjshfw', name: 'Cost Surface Rwanda B', scenarioUsageCount: 0, }, { - id: 'Cost Surface Rwanda C', + id: '23275455HGVVCMSJHDFk', name: 'Cost Surface Rwanda C', scenarioUsageCount: 0, }, @@ -48,3 +48,32 @@ export function useProjectCostSurfaces( ...queryOptions, }); } + +export function useEditCostSurface() { + const { data: session } = useSession(); + + const editCostSurface = ({ + costSurfaceId, + projectId, + body = {}, + }: { + costSurfaceId: CostSurface['id']; + projectId: Project['id']; + body: Record; + }) => { + // TODO: change this to the correct endpoint + return API.patch( + `projects/${projectId}/cost-surfaces/${costSurfaceId}`, + { + ...body, + }, + { + headers: { + Authorization: `Bearer ${session.accessToken}`, + }, + } + ); + }; + + return useMutation(editCostSurface); +} diff --git a/app/layout/project/sidebar/project/inventory-panel/cost-surfaces/actions-menu/index.tsx b/app/layout/project/sidebar/project/inventory-panel/cost-surfaces/actions-menu/index.tsx index a1180eecec..b5e7754cb4 100644 --- a/app/layout/project/sidebar/project/inventory-panel/cost-surfaces/actions-menu/index.tsx +++ b/app/layout/project/sidebar/project/inventory-panel/cost-surfaces/actions-menu/index.tsx @@ -59,7 +59,7 @@ const ActionsMenu = ({ size="narrow" onDismiss={() => handleModal('edit', false)} > - + {isDeletable && ( @@ -78,7 +78,7 @@ const ActionsMenu = ({ Delete void; }): JSX.Element => { const queryClient = useQueryClient(); @@ -46,83 +30,59 @@ const EditModal = ({ const formRef = useRef['form']>(null); - const featureQuery = useProjectFeatures(pid, featureId); - const editFeatureTagMutation = useEditFeatureTag(); - const deleteFeatureTagMutation = useDeleteFeatureTag(); - const editFeatureMutation = useEditFeature(); + const allProjectCostSurfacesQuery = useProjectCostSurfaces(pid, {}); + + const editCostSurfaceMutation = useEditCostSurface(); const onEditSubmit = useCallback( (values: FormValues) => { - const { featureClassName, tag } = values; - const editFeaturePromise = editFeatureMutation.mutateAsync({ - fid: featureId, - body: { - featureClassName, + const { name } = values; + + editCostSurfaceMutation.mutate( + { + costSurfaceId, + projectId: pid, + body: { + name, + }, }, - }); - - const editFeatureTagPromise = () => { - if (values.tag) { - return editFeatureTagMutation.mutateAsync({ - projectId: pid, - featureId, - data: { - tagName: tag, - }, - }); - } else { - return deleteFeatureTagMutation.mutateAsync({ - projectId: pid, - featureId, - }); + { + onSuccess: async () => { + await queryClient.invalidateQueries(['cost-surfaces', pid]); + handleModal('edit', false); + addToast( + 'success-edit-cost-surfaces', + <> +

Success!

+

Cost surface edited

+ , + { + level: 'success', + } + ); + }, + onError: () => { + addToast( + 'error-edit-cost-surfaces', + <> +

Error!

+

It is not possible to edit this cost surface

+ , + { + level: 'error', + } + ); + }, } - }; - - Promise.all([editFeaturePromise, editFeatureTagPromise()]) - .then(async () => { - await queryClient.invalidateQueries(['all-features', pid]); - handleModal('edit', false); - - addToast( - 'success-edit-feature', - <> -

Success!

-

Features edited

- , - { - level: 'success', - } - ); - }) - .catch(() => { - addToast( - 'error-edit-feature', - <> -

Error!

-

It is not possible to edit this feature

- , - { - level: 'error', - } - ); - }); + ); }, - [ - addToast, - deleteFeatureTagMutation, - editFeatureTagMutation, - editFeatureMutation, - featureId, - handleModal, - pid, - queryClient, - ] + [addToast, costSurfaceId, editCostSurfaceMutation, handleModal, pid, queryClient] ); return ( initialValues={{ - featureClassName: featureQuery.data?.[0]?.featureClassName, + name: allProjectCostSurfacesQuery.data?.[0]?.name, }} ref={formRef} onSubmit={onEditSubmit} @@ -135,12 +95,9 @@ const EditModal = ({

Edit cost surface

- - name="featureClassName" - validate={composeValidators([{ presence: true }])} - > + name="name" validate={composeValidators([{ presence: true }])}> {(fprops) => ( - +