fix: import resource imporements

This commit is contained in:
Ahmed Bouhuolia
2024-03-27 04:01:01 +02:00
parent 973d1832bd
commit ad4e51d81d
59 changed files with 1508 additions and 211 deletions

View File

@@ -2,7 +2,12 @@
import { useMutation, useQuery, useQueryClient } from 'react-query';
import useApiRequest from '../useRequest';
import { transformToCamelCase } from '@/utils';
import { downloadFile, useDownloadFile } from '../useDownloadFile';
const QueryKeys = {
ImportPreview: 'ImportPreview',
ImportFileMeta: 'ImportFileMeta',
};
/**
*
*/
@@ -28,22 +33,36 @@ export function useImportFileMapping(props = {}) {
{
onSuccess: (res, id) => {
// Invalidate queries.
queryClient.invalidateQueries([QueryKeys.ImportPreview]);
queryClient.invalidateQueries([QueryKeys.ImportFileMeta]);
},
...props,
},
);
}
export function useImportFilePreview(importId: string, props = {}) {
const queryClient = useQueryClient();
const apiRequest = useApiRequest();
return useQuery(['importPreview', importId], () =>
return useQuery([QueryKeys.ImportPreview, importId], () =>
apiRequest
.get(`import/${importId}/preview`)
.then((res) => transformToCamelCase(res.data)),
);
}
export function useImportFileMeta(importId: string, props = {}) {
const queryClient = useQueryClient();
const apiRequest = useApiRequest();
return useQuery([QueryKeys.ImportFileMeta, importId], () =>
apiRequest
.get(`import/${importId}`)
.then((res) => transformToCamelCase(res.data)),
);
}
/**
*
*/
@@ -61,3 +80,40 @@ export function useImportFileProcess(props = {}) {
},
);
}
interface SampleSheetImportQuery {
resource: string;
filename: string;
format: 'xlsx' | 'csv';
}
/**
* Initiates a download of the balance sheet in XLSX format.
* @param {Object} query - The query parameters for the request.
* @param {Object} args - Additional configurations for the download.
* @returns {Function} A function to trigger the file download.
*/
export const useSampleSheetImport = () => {
const apiRequest = useApiRequest();
return useMutation<void, AxiosError, IArgs>(
(data: SampleSheetImportQuery) => {
return apiRequest
.get('/import/sample', {
responseType: 'blob',
headers: {
accept:
data.format === 'xlsx' ? 'application/xlsx' : 'application/csv',
},
params: {
resource: data.resource,
format: data.format,
},
})
.then((res) => {
downloadFile(res.data, `${data.filename}.${data.format}`);
return res;
});
},
);
};