From 5ac93263a9d1ebb761217e960b619dd01b1df8d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Gonz=C3=A1lez=20Mu=C3=B1oz?= Date: Thu, 26 Oct 2023 11:17:52 +0200 Subject: [PATCH] unifies cost-surfaces upload modal and invalidates query --- app/hooks/scenarios/index.ts | 32 --- app/hooks/scenarios/types.ts | 9 - .../cost-surfaces/modals/upload/index.tsx | 12 +- .../grid-setup/cost-surface/index.tsx | 271 +----------------- 4 files changed, 20 insertions(+), 304 deletions(-) diff --git a/app/hooks/scenarios/index.ts b/app/hooks/scenarios/index.ts index e84166e168..5e7576ba44 100644 --- a/app/hooks/scenarios/index.ts +++ b/app/hooks/scenarios/index.ts @@ -34,8 +34,6 @@ import { UseScenariosOptionsProps, UseDeleteScenarioProps, DeleteScenarioProps, - UseUploadScenarioCostSurfaceProps, - UploadScenarioCostSurfaceProps, UseUploadScenarioPUProps, UploadScenarioPUProps, UseSaveScenarioPUProps, @@ -650,36 +648,6 @@ export function useCostSurfaceRange(id: Scenario['id']) { }, [query, data]); } -export function useUploadCostSurface({ - requestConfig = { - method: 'GET', - }, -}: UseUploadScenarioCostSurfaceProps) { - const { data: session } = useSession(); - - const uploadScenarioCostSurface = ({ id, data }: UploadScenarioCostSurfaceProps) => { - return UPLOADS.request({ - url: `/scenarios/${id}/cost-surface/shapefile`, - data, - headers: { - Authorization: `Bearer ${session.accessToken}`, - 'Content-Type': 'multipart/form-data', - }, - ...requestConfig, - }); - }; - - return useMutation(uploadScenarioCostSurface, { - onSuccess: (data, variables, context) => { - console.info('Success', data, variables, context); - }, - onError: (error, variables, context) => { - // An error happened! - console.info('Error', error, variables, context); - }, - }); -} - // PLANNING UNITS export function useScenarioPU( sid: string, diff --git a/app/hooks/scenarios/types.ts b/app/hooks/scenarios/types.ts index b2de799f45..6ae5791803 100644 --- a/app/hooks/scenarios/types.ts +++ b/app/hooks/scenarios/types.ts @@ -39,15 +39,6 @@ export interface UploadScenarioPUProps { data: FormData; } -export interface UseUploadScenarioCostSurfaceProps { - requestConfig?: AxiosRequestConfig; -} - -export interface UploadScenarioCostSurfaceProps { - id?: string; - data: any; -} - export interface UseSaveScenarioPUProps { requestConfig?: AxiosRequestConfig; } diff --git a/app/layout/project/sidebar/project/inventory-panel/cost-surfaces/modals/upload/index.tsx b/app/layout/project/sidebar/project/inventory-panel/cost-surfaces/modals/upload/index.tsx index 0d05744e32..92401deff0 100644 --- a/app/layout/project/sidebar/project/inventory-panel/cost-surfaces/modals/upload/index.tsx +++ b/app/layout/project/sidebar/project/inventory-panel/cost-surfaces/modals/upload/index.tsx @@ -2,6 +2,7 @@ import React, { useCallback, useEffect, useRef, useState } from 'react'; import { useDropzone, DropzoneProps } from 'react-dropzone'; import { Form as FormRFF, Field as FieldRFF, FormProps } from 'react-final-form'; +import { useQueryClient } from 'react-query'; import { useRouter } from 'next/router'; @@ -42,6 +43,7 @@ export const CostSurfaceUploadModal = ({ onDismiss: () => void; }): JSX.Element => { const formRef = useRef['form']>(null); + const queryClient = useQueryClient(); const [loading, setLoading] = useState(false); const [successFile, setSuccessFile] = useState<{ name: FormValues['name'] }>(null); @@ -114,9 +116,11 @@ export const CostSurfaceUploadModal = ({ data.append('name', name); uploadProjectCostSurfaceMutation.mutate( - { data, id: `${pid}` }, + { data, id: pid }, { - onSuccess: () => { + onSuccess: async () => { + await queryClient.invalidateQueries(['cost-surfaces', pid]); + setSuccessFile({ ...successFile }); onClose(); addToast( @@ -163,7 +167,7 @@ export const CostSurfaceUploadModal = ({ } ); }, - [pid, addToast, onClose, uploadProjectCostSurfaceMutation, successFile] + [pid, addToast, onClose, uploadProjectCostSurfaceMutation, successFile, queryClient] ); const { getRootProps, getInputProps, isDragActive, isDragAccept, isDragReject } = useDropzone({ @@ -214,7 +218,7 @@ export const CostSurfaceUploadModal = ({ -

+

Please download and fill in the{' '} - - - - )} - -

- - - -
- - - ); - }} - /> - + Upload cost surface + + )} {successFile && ( @@ -408,6 +159,8 @@ export const GridSetupCostSurface = (): JSX.Element => { )} + + setOpened(false)} isOpen={opened} /> )}