mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-21 23:19:09 +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);
|
.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 = {
|
export type HasUpdate = {
|
||||||
url: string;
|
url: string;
|
||||||
version: string;
|
version: string;
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ import { IoWarningOutline } from '@react-icons/all-files/io5/IoWarningOutline';
|
|||||||
import { useQueryClient } from '@tanstack/react-query';
|
import { useQueryClient } from '@tanstack/react-query';
|
||||||
|
|
||||||
import { RUNDOWN_TABLE } from '../../../common/api/apiConstants';
|
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 { useEmitLog } from '../../../common/stores/logger';
|
||||||
import ModalSplitInput from '../ModalSplitInput';
|
import ModalSplitInput from '../ModalSplitInput';
|
||||||
|
|
||||||
@@ -58,17 +58,25 @@ export default function UploadModal({ onClose, isOpen }: UploadModalProps) {
|
|||||||
|
|
||||||
const handleSubmit = useCallback(async () => {
|
const handleSubmit = useCallback(async () => {
|
||||||
setSubmitting(true);
|
setSubmitting(true);
|
||||||
|
console.log(file);
|
||||||
if (file) {
|
if (file) {
|
||||||
try {
|
// not the most robust, but ok for now
|
||||||
const options = {
|
if (file.name.endsWith('.xlsx')) {
|
||||||
onlyRundown: overrideOptionRef.current?.checked || false,
|
console.log('pushing to preview');
|
||||||
};
|
const r = await postPreviewExcel(file, setProgress, {});
|
||||||
await uploadData(file, setProgress, options);
|
console.log('got from preview', r);
|
||||||
} catch (error) {
|
} else {
|
||||||
emitError(`Failed uploading file: ${error}`);
|
try {
|
||||||
} finally {
|
const options = {
|
||||||
await queryClient.invalidateQueries(RUNDOWN_TABLE);
|
onlyRundown: overrideOptionRef.current?.checked || false,
|
||||||
setSuccess(true);
|
};
|
||||||
|
await uploadData(file, setProgress, options);
|
||||||
|
} catch (error) {
|
||||||
|
emitError(`Failed uploading file: ${error}`);
|
||||||
|
} finally {
|
||||||
|
await queryClient.invalidateQueries(RUNDOWN_TABLE);
|
||||||
|
setSuccess(true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setSubmitting(false);
|
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
|
* handles file upload
|
||||||
* @param file
|
* @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'
|
// Create controller for POST request to '/ontime/db'
|
||||||
// Returns -
|
// Returns -
|
||||||
export const dbUpload = async (req, res) => {
|
export const dbUpload = async (req, res) => {
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import {
|
|||||||
postSettings,
|
postSettings,
|
||||||
postUserFields,
|
postUserFields,
|
||||||
postViewSettings,
|
postViewSettings,
|
||||||
|
previewExcel,
|
||||||
} from '../controllers/ontimeController.js';
|
} from '../controllers/ontimeController.js';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@@ -40,6 +41,9 @@ router.get('/db', dbDownload);
|
|||||||
// create route between controller and '/ontime/db' endpoint
|
// create route between controller and '/ontime/db' endpoint
|
||||||
router.post('/db', uploadFile, dbUpload);
|
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
|
// create route between controller and '/ontime/settings' endpoint
|
||||||
router.get('/settings', getSettings);
|
router.get('/settings', getSettings);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user