From c8c4c7bcc0d8a2ffcf4cafbc61d57db6eb72757d Mon Sep 17 00:00:00 2001 From: Sejun Park Date: Mon, 22 Jul 2024 21:45:25 -0400 Subject: [PATCH] Implemented uploading document --- src/App.css | 3 ++ src/components/myinfo/MyinfoDocument.jsx | 43 +++++++++++++----- src/components/myinfo/MyinfoJourney.jsx | 57 +++++++++++++++++++----- 3 files changed, 81 insertions(+), 22 deletions(-) diff --git a/src/App.css b/src/App.css index e69de29bb..c06525aff 100644 --- a/src/App.css +++ b/src/App.css @@ -0,0 +1,3 @@ +.hidden-input { + display: none; +} \ No newline at end of file diff --git a/src/components/myinfo/MyinfoDocument.jsx b/src/components/myinfo/MyinfoDocument.jsx index b6429102c..29730eb28 100644 --- a/src/components/myinfo/MyinfoDocument.jsx +++ b/src/components/myinfo/MyinfoDocument.jsx @@ -11,7 +11,8 @@ import TableContainer from '@mui/material/TableContainer'; import TableHead from '@mui/material/TableHead'; import TableRow from '@mui/material/TableRow'; import axios from 'axios'; -// import '../App.css'; +import '../../App.css' +import { upload } from '@testing-library/user-event/dist/upload'; const rows = [ {name:"Offer letter",date:"Jan 4, 2022"}, @@ -31,24 +32,41 @@ const MyinfoDocument = () => { const [filesize, setFileSize] = useState(0) const [filelist, setFileList] = useState([]); const [uploaded, setUploaded] = useState(false) + + const uploadFileBackend = () => { + if (filelist) { + const reader = new FileReader(); + reader.onload = function(event) { + const base64String = event.target.result.split(',')[1]; // Extract base64 string + sendFileAPI(base64String); + }; + reader.readAsDataURL(filelist); + } else { + alert("Please select a file"); + } +}; - const handleUpload = async () => { + const sendFileAPI = async () => { if (uploaded) { clearFileInput(); return } + try { const response = await axios.post( - "http://localhost:3000/", - filelist, + 'http://localhost:5000/api/documents', + { file: filelist }, { - onUploadProgress: (progressEvent) => { - const percentCompleted = Math.round( - (progressEvent.loaded * 100) / progressEvent.total - ); - setProgress(percentCompleted) + headers: { + 'Content-Type': 'application/json' + }, + onUploadProgress: (progressEvent) => { + const percentCompleted = Math.round( + (progressEvent.loaded * 100) / progressEvent.total + ); + setProgress(percentCompleted); } - } + } ); } @@ -70,6 +88,8 @@ const MyinfoDocument = () => { } + + // Handle file change event const handleFileChange = (e) => { const file = e.target.files[0]; @@ -89,12 +109,13 @@ const MyinfoDocument = () => { }) } console.log(filelist) - handleUpload() + // uploadFileBackend() } // Trigger file input const handleClick = () => { fileInputRef.current.click() + } const handleDragOver = (e) => { diff --git a/src/components/myinfo/MyinfoJourney.jsx b/src/components/myinfo/MyinfoJourney.jsx index 3c4287453..c1db3dadd 100644 --- a/src/components/myinfo/MyinfoJourney.jsx +++ b/src/components/myinfo/MyinfoJourney.jsx @@ -1,4 +1,4 @@ -import React from 'react' +import React, { useEffect,useState } from 'react' import { Typography,Stack } from '@mui/material'; import Table from '@mui/material/Table'; import TableBody from '@mui/material/TableBody'; @@ -15,34 +15,68 @@ const rows = [ ] -{/* or drag and drop - */} + + const MyinfoJourney = () => { + + function formatDate(isoDate) { + const date = new Date(isoDate); + + const options = { year: 'numeric', month: 'short', day: 'numeric' }; + + return date.toLocaleDateString('en-US', options); +} + + const [journeyLists,setJourneyLists] = useState([]) + + useEffect(()=>{ + const getJourneyAPI = async () => { + try { + const res = await fetch("http://localhost:5000/api/changehistories",{ + method:'GET', + headers: { + 'Content-Type' : 'application/json' + } + }); + const result = await res.json(); + setJourneyLists(result) + + + + } + catch(error) { + console.log("Error fetching Myinfo:Journey",error) + } + } + getJourneyAPI() + + },[]) + return ( <> - +
- {rows.map((row) => ( + {journeyLists.map((list) => ( - {row.title} + {list.changeType} From: - {row.from} + {list.changeFrom} To: - {row.to} + {list.changeTo} - {row.date} + {formatDate(list.date)} ))} @@ -53,4 +87,5 @@ const MyinfoJourney = () => { ) } -export default MyinfoJourney \ No newline at end of file +export default MyinfoJourney +