mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 04:40:32 +00:00
fix: import resource imporements
This commit is contained in:
@@ -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;
|
||||
});
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user