mirror of
https://github.com/janishutz/fundamentals-of-webengineering.git
synced 2025-11-25 05:44:24 +00:00
[Task 3] More fixes
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import './css/App.css';
|
||||
import '@fortawesome/fontawesome-free/css/all.css';
|
||||
import './sse';
|
||||
import {
|
||||
CSV_Data, fileInfo, responseObject
|
||||
} from './types';
|
||||
@@ -15,7 +16,6 @@ import DataTable from './components/DataTable';
|
||||
import FileCard from './components/FileCard';
|
||||
import InfoCard from './components/InfoCard';
|
||||
import Layout from './components/Layout';
|
||||
import "./sse"
|
||||
|
||||
function App () {
|
||||
const [
|
||||
@@ -35,25 +35,29 @@ function App () {
|
||||
fileList,
|
||||
setFileList
|
||||
] = useState( null as responseObject | null );
|
||||
|
||||
// For the loading spinner in DataTable
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const [
|
||||
loading,
|
||||
setLoading
|
||||
] = useState( false );
|
||||
// Add evenbt listener for server-sent events on first render
|
||||
const [active, setActive] = useState(false);
|
||||
const [
|
||||
active,
|
||||
setActive
|
||||
] = useState( false );
|
||||
|
||||
useEffect( () => {
|
||||
if ( !active ) {
|
||||
document.addEventListener("sse:uploaded", async _ev => {
|
||||
await fetch( '/status', {
|
||||
document.addEventListener( 'sse:uploaded', () => {
|
||||
fetch( '/status', {
|
||||
'method': 'GET'
|
||||
} )
|
||||
.then( response => response.json() )
|
||||
.then( response => setFileList( response ) )
|
||||
.catch( error => console.log( error ) );
|
||||
} );
|
||||
document.addEventListener("sse:deleted", async _ev => {
|
||||
await fetch( '/status', {
|
||||
document.addEventListener( 'sse:deleted', () => {
|
||||
fetch( '/status', {
|
||||
'method': 'GET'
|
||||
} )
|
||||
.then( response => response.json() )
|
||||
@@ -69,13 +73,13 @@ function App () {
|
||||
.then( response => setFileList( response ) )
|
||||
.catch( error => console.log( error ) );
|
||||
}
|
||||
})
|
||||
} );
|
||||
|
||||
const formRef = useRef( null );
|
||||
|
||||
// This is triggered in CSVCard
|
||||
const handleFileUpload = async ( e: React.ChangeEvent<HTMLInputElement> ): Promise<void> => {
|
||||
setLoading(true)
|
||||
setLoading( true );
|
||||
const file = e.target.files?.[0];
|
||||
|
||||
if ( !file ) throw new Error( 'No file received' );
|
||||
@@ -96,7 +100,7 @@ function App () {
|
||||
// Upload to server
|
||||
const formData = new FormData( formRef.current );
|
||||
|
||||
await fetch( '/upload', {
|
||||
await fetch( '/upload?fname=' + file.name, {
|
||||
'method': 'POST',
|
||||
'body': formData
|
||||
} );
|
||||
@@ -104,7 +108,7 @@ function App () {
|
||||
};
|
||||
|
||||
const handleFileChange = async ( fileName: string ) => {
|
||||
setLoading(true)
|
||||
setLoading( true );
|
||||
|
||||
const response = await fetch( `/download/${ fileName }` );
|
||||
const blob = await response.blob();
|
||||
@@ -114,6 +118,13 @@ function App () {
|
||||
|
||||
const data = await convertCSVtoJSON( text );
|
||||
|
||||
setInfo( {
|
||||
'filesize': blob.size + 'B',
|
||||
'filetype': response.headers.get( 'Content-Type' ) ?? 'text/csv',
|
||||
'filename': fileName,
|
||||
'rowcount': data.length
|
||||
} );
|
||||
|
||||
// Updating fileInfo requires more effort since blob doesn't have the metadata
|
||||
setLoading( false );
|
||||
setData( data );
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
CSV_Data
|
||||
} from '../types';
|
||||
|
||||
|
||||
const DataTable = ( props: {
|
||||
'data': CSV_Data,
|
||||
'loading': boolean
|
||||
@@ -33,6 +34,7 @@ const DataTable = ( props: {
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
if ( sortCol !== 'None' && sortType === 'asc' ) {
|
||||
props.data.sort( ( a, b ) => {
|
||||
if ( a[sortCol]! < b[sortCol]! ) return -1;
|
||||
@@ -49,6 +51,8 @@ const DataTable = ( props: {
|
||||
|
||||
return 0;
|
||||
} );
|
||||
} else {
|
||||
props.data.sort();
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -61,8 +65,9 @@ const DataTable = ( props: {
|
||||
<thead>
|
||||
<tr>
|
||||
{
|
||||
header.map( col => <ColHeader
|
||||
header.map( ( col, i ) => <ColHeader
|
||||
col={col}
|
||||
key={i}
|
||||
sortingHandle={sortingHandler}
|
||||
isSelected={col == sortCol}
|
||||
sortType={sortType}></ColHeader> )
|
||||
@@ -74,7 +79,8 @@ const DataTable = ( props: {
|
||||
props.data.map( ( row, i ) => (
|
||||
<tr key={i}>
|
||||
{
|
||||
header.map( col => <Row key={i} col={col} content={row[col] as string}></Row> )
|
||||
header.map( ( col, j ) => <Row key={`${ i }:${ j }`}
|
||||
col={col} content={row[col] as string}></Row> )
|
||||
}
|
||||
</tr>
|
||||
) )
|
||||
@@ -117,4 +123,3 @@ const Row = ( props: {
|
||||
};
|
||||
|
||||
export default DataTable;
|
||||
|
||||
|
||||
@@ -5,18 +5,21 @@ import {
|
||||
const InfoCard = ( props: {
|
||||
'info': fileInfo
|
||||
} ) => {
|
||||
let noFileMessage = <div></div>;
|
||||
|
||||
if ( props.info.filename === 'None' )
|
||||
noFileMessage = <div id="data-info-placeholder">No file selected</div>;
|
||||
|
||||
return (
|
||||
<article>
|
||||
<header>
|
||||
<h2>Data infos</h2>
|
||||
<InfoRenderer info={props.info}></InfoRenderer>
|
||||
</header>
|
||||
<div className="info">
|
||||
{noFileMessage}
|
||||
</article>
|
||||
);
|
||||
};
|
||||
|
||||
const InfoRenderer = ( props: {
|
||||
'info': fileInfo
|
||||
} ) => {
|
||||
if ( props.info.filename !== 'None' ) {
|
||||
return <div className="info">
|
||||
<h4>Filename</h4>
|
||||
<p>{props.info.filename}</p>
|
||||
|
||||
@@ -28,10 +31,13 @@ const InfoCard = ( props: {
|
||||
|
||||
<h4>Number of rows</h4>
|
||||
<p>{props.info.rowcount}</p>
|
||||
</div>
|
||||
</article>
|
||||
);
|
||||
</div>;
|
||||
} else {
|
||||
return <div className="info">
|
||||
<p>No file selected</p>
|
||||
</div>;
|
||||
}
|
||||
};
|
||||
|
||||
export default InfoCard;
|
||||
|
||||
export default InfoCard;
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
import React from "react";
|
||||
import ReactDOM from "react-dom/client";
|
||||
import "@fortawesome/fontawesome-free/css/all.css";
|
||||
import App from "./App";
|
||||
import "@picocss/pico/css/pico.min.css";
|
||||
import '@fortawesome/fontawesome-free/css/all.css';
|
||||
import './index.css';
|
||||
import '@picocss/pico/css/pico.min.css';
|
||||
import App from './App';
|
||||
import React from 'react';
|
||||
import ReactDOM from 'react-dom/client';
|
||||
|
||||
import "./index.css";
|
||||
|
||||
ReactDOM.createRoot(document.getElementById("root")!).render(
|
||||
<React.StrictMode>
|
||||
ReactDOM.createRoot( document.getElementById( 'root' )! ).render( <React.StrictMode>
|
||||
<App />
|
||||
</React.StrictMode>,
|
||||
);
|
||||
</React.StrictMode>, );
|
||||
|
||||
@@ -11,17 +11,35 @@ import {
|
||||
} from './types';
|
||||
|
||||
const app = express();
|
||||
|
||||
const sanitizeFilePath = ( path: string ) => {
|
||||
// eslint-disable-next-line no-useless-escape
|
||||
return path.replace( /[\/\\].*/, '' ).replace( '..', '' );
|
||||
};
|
||||
|
||||
// Set up file storage
|
||||
const storage = multer.diskStorage( {
|
||||
'destination': './src/server/uploads',
|
||||
'filename': (
|
||||
_req, file, cb
|
||||
req, file, cb
|
||||
) => {
|
||||
// Suggested in Multer's readme
|
||||
const uniqueSuffix = Date.now() + '-' + Math.round( Math.random() * 1E3 );
|
||||
const fname = file.fieldname + '-' + uniqueSuffix;
|
||||
// TODO: We could consider allowing the filename to be overwritten using a query param on the
|
||||
// request (i.e. url would be /upload?fname=<filename>)
|
||||
|
||||
let fname = req.query['fname']
|
||||
? sanitizeFilePath( String( req.query['fname'] ) )
|
||||
: file.fieldname;
|
||||
|
||||
const index = fname.lastIndexOf( '.' );
|
||||
|
||||
let fext = '';
|
||||
|
||||
if ( index > -1 ) {
|
||||
fname = fname.slice( 0, index );
|
||||
fext = fname.substring( index );
|
||||
}
|
||||
|
||||
fname += '-' + uniqueSuffix + fext;
|
||||
|
||||
fileEvent.emit( 'uploaded', fname );
|
||||
|
||||
|
||||
Reference in New Issue
Block a user