From d98c876f82c09b374a659f7671db0f36242b2404 Mon Sep 17 00:00:00 2001 From: ajaythapliyal Date: Sat, 18 Mar 2023 18:25:23 +0530 Subject: [PATCH] adds training modals --- frontend/src/upload/Upload.tsx | 142 +++++++++++++++++++++++++-------- frontend/tailwind.config.cjs | 2 + 2 files changed, 112 insertions(+), 32 deletions(-) diff --git a/frontend/src/upload/Upload.tsx b/frontend/src/upload/Upload.tsx index ce2dc829..1343929c 100644 --- a/frontend/src/upload/Upload.tsx +++ b/frontend/src/upload/Upload.tsx @@ -1,6 +1,9 @@ -import { useCallback, useState } from 'react'; +import { useCallback, useEffect, useState } from 'react'; import { useDropzone } from 'react-dropzone'; +import { useDispatch } from 'react-redux'; import { ActiveState } from '../models/misc'; +import { getDocs } from '../preferences/preferenceApi'; +import { setSourceDocs } from '../preferences/preferenceSlice'; export default function Upload({ modalState, @@ -11,6 +14,79 @@ export default function Upload({ }) { const [docName, setDocName] = useState(''); const [files, setfiles] = useState([]); + const [progress, setProgress] = useState<{ + type: 'UPLOAD' | 'TRAINIING'; + percentage: number; + taskId?: string; + }>(); + + function Progress({ + title, + isCancellable = false, + }: { + title: string; + isCancellable?: boolean; + }) { + return ( +
+

{title}...

+

This may take several minutes

+

{progress?.percentage}%

+
+
+
+
+ +
+ ); + } + + function UploadProgress() { + return ; + } + + function TrainingProgress() { + const dispatch = useDispatch(); + useEffect(() => { + const id = setInterval(() => { + const apiHost = import.meta.env.VITE_API_HOST; + fetch(`${apiHost}/api/task_status}?task_id=${progress?.taskId}`) + .then((data) => data.json()) + .then((data) => { + if (data.status == 'SUCCESS') { + clearInterval(id); + getDocs().then((data) => dispatch(setSourceDocs(data))); + } + setProgress( + (progress) => + progress && { ...progress, percentage: data.result.current }, + ); + }); + }); + return () => clearInterval(id); + }, []); + return ( + + ); + } const onDrop = useCallback((acceptedFiles: File[]) => { setfiles(acceptedFiles); @@ -19,36 +95,25 @@ export default function Upload({ const doNothing = () => undefined; - const uploadFile = async () => { + const uploadFile = () => { const formData = new FormData(); - - // Add the uploaded files to formData files.forEach((file) => { formData.append('file', file); }); - - // Add the document name to formData formData.append('name', docName); formData.append('user', 'local'); const apiHost = import.meta.env.VITE_API_HOST; - - try { - const response = await fetch(apiHost + '/api/upload', { - method: 'POST', - body: formData, - }); - - if (response.ok) { - console.log('Files uploaded successfully'); - setDocName(''); - setfiles([]); - setModalState('INACTIVE'); - } else { - console.error('File upload failed'); - } - } catch (error) { - console.error('Error during file upload:', error); - } + const xhr = new XMLHttpRequest(); + xhr.upload.addEventListener('progress', (event) => { + const progress = +((event.loaded / event.total) * 100).toFixed(2); + setProgress({ type: 'UPLOAD', percentage: progress }); + }); + xhr.onload = () => { + const { task_id } = JSON.parse(xhr.responseText); + setProgress({ type: 'TRAINIING', percentage: 0, taskId: task_id }); + }; + xhr.open('POST', `${apiHost + '/api/upload'}`); + xhr.send(formData); }; const { getRootProps, getInputProps, isDragActive } = useDropzone({ @@ -58,13 +123,15 @@ export default function Upload({ onDragOver: doNothing, onDragLeave: doNothing, }); - return ( -
-
+ + let view; + if (progress?.type === 'UPLOAD') { + view = ; + } else if (progress?.type === 'TRAINIING') { + view = ; + } else { + view = ( + <>

Upload New Documentation

+ + ); + } + + return ( +
+
+ {view}
); } - // TODO: sanitize all inputs diff --git a/frontend/tailwind.config.cjs b/frontend/tailwind.config.cjs index a824c5cd..64fb76f2 100644 --- a/frontend/tailwind.config.cjs +++ b/frontend/tailwind.config.cjs @@ -24,6 +24,8 @@ module.exports = { 'blue-1000': '#7D54D1', 'blue-2000': '#002B49', 'blue-3000': '#4B02E2', + 'blue-4000': 'rgba(0, 125, 255, 0.36)', + 'blue-5000': 'rgba(0, 125, 255)', }, }, },