Skip to content

Commit

Permalink
unifies cost-surfaces upload modal and invalidates query
Browse files Browse the repository at this point in the history
  • Loading branch information
agnlez committed Oct 26, 2023
1 parent 7ed9392 commit 5ac9326
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 304 deletions.
32 changes: 0 additions & 32 deletions app/hooks/scenarios/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ import {
UseScenariosOptionsProps,
UseDeleteScenarioProps,
DeleteScenarioProps,
UseUploadScenarioCostSurfaceProps,
UploadScenarioCostSurfaceProps,
UseUploadScenarioPUProps,
UploadScenarioPUProps,
UseSaveScenarioPUProps,
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 0 additions & 9 deletions app/hooks/scenarios/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -42,6 +43,7 @@ export const CostSurfaceUploadModal = ({
onDismiss: () => void;
}): JSX.Element => {
const formRef = useRef<FormProps<FormValues>['form']>(null);
const queryClient = useQueryClient();

const [loading, setLoading] = useState(false);
const [successFile, setSuccessFile] = useState<{ name: FormValues['name'] }>(null);
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -214,7 +218,7 @@ export const CostSurfaceUploadModal = ({
</InfoButton>
</div>

<p className="mt-4 text-sm text-gray-900">
<p className="text-sm text-gray-900">
Please download and fill in the{' '}
<button
className="text-primary-500 underline hover:no-underline"
Expand Down
Loading

0 comments on commit 5ac9326

Please sign in to comment.