mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-09 01:13:55 +00:00
wip: get preview data from excel import
This commit is contained in:
@@ -161,6 +161,24 @@ export const uploadData = async (file: File, setProgress: (value: number) => voi
|
||||
.then((response) => response.data.id);
|
||||
};
|
||||
|
||||
export async function postPreviewExcel(file: File, setProgress: (value: number) => void, options?: UploadDataOptions) {
|
||||
const formData = new FormData();
|
||||
formData.append('userFile', file);
|
||||
const response = await axios
|
||||
.post(`${ontimeURL}/previewExcel`, formData, {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data',
|
||||
},
|
||||
onUploadProgress: (progressEvent) => {
|
||||
const complete = progressEvent?.total ? Math.round((progressEvent.loaded * 100) / progressEvent.total) : 0;
|
||||
setProgress(complete);
|
||||
},
|
||||
})
|
||||
.then((response) => response);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
export type HasUpdate = {
|
||||
url: string;
|
||||
version: string;
|
||||
|
||||
@@ -18,7 +18,7 @@ import { IoWarningOutline } from '@react-icons/all-files/io5/IoWarningOutline';
|
||||
import { useQueryClient } from '@tanstack/react-query';
|
||||
|
||||
import { RUNDOWN_TABLE } from '../../../common/api/apiConstants';
|
||||
import { uploadData } from '../../../common/api/ontimeApi';
|
||||
import { postPreviewExcel, uploadData } from '../../../common/api/ontimeApi';
|
||||
import { useEmitLog } from '../../../common/stores/logger';
|
||||
import ModalSplitInput from '../ModalSplitInput';
|
||||
|
||||
@@ -58,17 +58,25 @@ export default function UploadModal({ onClose, isOpen }: UploadModalProps) {
|
||||
|
||||
const handleSubmit = useCallback(async () => {
|
||||
setSubmitting(true);
|
||||
console.log(file);
|
||||
if (file) {
|
||||
try {
|
||||
const options = {
|
||||
onlyRundown: overrideOptionRef.current?.checked || false,
|
||||
};
|
||||
await uploadData(file, setProgress, options);
|
||||
} catch (error) {
|
||||
emitError(`Failed uploading file: ${error}`);
|
||||
} finally {
|
||||
await queryClient.invalidateQueries(RUNDOWN_TABLE);
|
||||
setSuccess(true);
|
||||
// not the most robust, but ok for now
|
||||
if (file.name.endsWith('.xlsx')) {
|
||||
console.log('pushing to preview');
|
||||
const r = await postPreviewExcel(file, setProgress, {});
|
||||
console.log('got from preview', r);
|
||||
} else {
|
||||
try {
|
||||
const options = {
|
||||
onlyRundown: overrideOptionRef.current?.checked || false,
|
||||
};
|
||||
await uploadData(file, setProgress, options);
|
||||
} catch (error) {
|
||||
emitError(`Failed uploading file: ${error}`);
|
||||
} finally {
|
||||
await queryClient.invalidateQueries(RUNDOWN_TABLE);
|
||||
setSuccess(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
setSubmitting(false);
|
||||
|
||||
@@ -43,6 +43,23 @@ export const dbDownload = async (req, res) => {
|
||||
});
|
||||
};
|
||||
|
||||
async function justUploadAndParse(file, req, res, options) {
|
||||
if (!fs.existsSync(file)) {
|
||||
res.status(500).send({ message: 'Upload failed' });
|
||||
return;
|
||||
}
|
||||
|
||||
const result = await fileHandler(file);
|
||||
|
||||
if ('error' in result && result.error) {
|
||||
throw new Error(result.message);
|
||||
} else if ('data' in result && result.message === 'success') {
|
||||
return result.data;
|
||||
} else {
|
||||
throw new Error('Failed parsing, no data');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* handles file upload
|
||||
* @param file
|
||||
@@ -319,6 +336,23 @@ export const postOSC = async (req, res) => {
|
||||
}
|
||||
};
|
||||
|
||||
// Create controller for POST request to '/ontime/previewExcel'
|
||||
// Returns -
|
||||
export async function previewExcel(req, res) {
|
||||
if (!req.file) {
|
||||
res.status(400).send({ message: 'File not found' });
|
||||
return;
|
||||
}
|
||||
const options = req.query;
|
||||
const file = req.file.path;
|
||||
try {
|
||||
const data = await justUploadAndParse(file, req, res, options);
|
||||
res.status(200).send(data);
|
||||
} catch (error) {
|
||||
res.status(500).send(error);
|
||||
}
|
||||
}
|
||||
|
||||
// Create controller for POST request to '/ontime/db'
|
||||
// Returns -
|
||||
export const dbUpload = async (req, res) => {
|
||||
|
||||
@@ -17,6 +17,7 @@ import {
|
||||
postSettings,
|
||||
postUserFields,
|
||||
postViewSettings,
|
||||
previewExcel,
|
||||
} from '../controllers/ontimeController.js';
|
||||
|
||||
import {
|
||||
@@ -40,6 +41,9 @@ router.get('/db', dbDownload);
|
||||
// create route between controller and '/ontime/db' endpoint
|
||||
router.post('/db', uploadFile, dbUpload);
|
||||
|
||||
// create route between controller and '/ontime/db' endpoint
|
||||
router.post('/previewExcel', uploadFile, previewExcel);
|
||||
|
||||
// create route between controller and '/ontime/settings' endpoint
|
||||
router.get('/settings', getSettings);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user