mirror of
https://github.com/janishutz/fundamentals-of-webengineering.git
synced 2025-11-26 22:34:24 +00:00
Task 3: File Selector
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
import "./css/App.css";
|
||||
import '@fortawesome/fontawesome-free/css/all.css';
|
||||
import { readCSV } from './csv';
|
||||
import { CSV_Data, fileInfo } from './types';
|
||||
import { CSV_Data, fileInfo, responseObject } from './types';
|
||||
import { readCSV, convertCSVtoJSON } from './csv';
|
||||
|
||||
import React, { useState, useRef } from "react";
|
||||
import Layout from "./Layout";
|
||||
import React, { useState, useRef, useEffect } from "react";
|
||||
import Layout from "./components/Layout";
|
||||
import CSVCard from "./components/CSVCard";
|
||||
import InfoCard from "./components/InfoCard";
|
||||
import DataTable from "./components/DataTable";
|
||||
import FileCard from "./components/FileCard";
|
||||
|
||||
function App() {
|
||||
const [data, setData] = useState([] as CSV_Data);
|
||||
@@ -18,10 +19,19 @@ function App() {
|
||||
rowcount: 0
|
||||
});
|
||||
|
||||
const [fileList, setFileList] = useState(null as responseObject | null);
|
||||
// Effect has to be in top level of the component
|
||||
useEffect(() => {
|
||||
fetch("/status", { method: "GET" })
|
||||
.then((response) => response.json())
|
||||
.then((response) => setFileList(response))
|
||||
.catch((error) => console.log(error));
|
||||
});
|
||||
|
||||
const formRef = useRef(null);
|
||||
|
||||
// This is triggered in CSVCard
|
||||
const handleFileChange = async (e: React.ChangeEvent<HTMLInputElement>): Promise<void> => {
|
||||
const handleFileUpload = async (e: React.ChangeEvent<HTMLInputElement>): Promise<void> => {
|
||||
const file = e.target.files?.[0];
|
||||
if (!file) throw new Error("No file received");
|
||||
|
||||
@@ -29,7 +39,7 @@ function App() {
|
||||
|
||||
const newFileInfo: fileInfo = {
|
||||
filename: file.name,
|
||||
filetype: file.type,
|
||||
filetype: ".csv", // file.type delivers weird name
|
||||
filesize: String(file.size) + "B",
|
||||
rowcount: data.length
|
||||
}
|
||||
@@ -39,18 +49,31 @@ function App() {
|
||||
if (formRef.current) {
|
||||
// Upload to server
|
||||
const formData = new FormData(formRef.current);
|
||||
const res = await fetch("/upload", {
|
||||
await fetch("/upload", {
|
||||
method: "POST",
|
||||
body: formData
|
||||
});
|
||||
const result = await res.json();
|
||||
console.log(result);
|
||||
}
|
||||
}
|
||||
|
||||
const handleFileChange = async (fileName: string) => {
|
||||
const response = await fetch(`/download/${fileName}`);
|
||||
const blob = await response.blob();
|
||||
const text = await blob.text();
|
||||
|
||||
if (!response) throw new Error("No file received");
|
||||
|
||||
const data = await convertCSVtoJSON(text);
|
||||
|
||||
// Updating fileInfo requires more effort since blob doesn't have the metadata
|
||||
|
||||
setData(data);
|
||||
}
|
||||
|
||||
return (
|
||||
<Layout>
|
||||
<CSVCard handleChange={handleFileChange} formRef={formRef}></CSVCard>
|
||||
<CSVCard handleChange={handleFileUpload} formRef={formRef}></CSVCard>
|
||||
<FileCard fileList={fileList as responseObject} fileChangeHandle={handleFileChange}></FileCard>
|
||||
<InfoCard info={info}></InfoCard>
|
||||
<DataTable data={data}></DataTable>
|
||||
</Layout>
|
||||
|
||||
@@ -8,7 +8,7 @@ const CSVCard = (props: {
|
||||
return (
|
||||
<article>
|
||||
<header>
|
||||
<h2>Select CSV data</h2>
|
||||
<h2>Upload CSV data</h2>
|
||||
</header>
|
||||
<form ref={props.formRef} action="/upload" method="post" encType="multipart/form-data" >
|
||||
<label htmlFor="file-input" className="custom-file-upload">
|
||||
|
||||
@@ -56,7 +56,7 @@ const DataTable = (props: {data: CSV_Data}) => {
|
||||
<tr key={i}>
|
||||
{
|
||||
header.map( (col) => (
|
||||
<Row col={col} content={row[col] as String}></Row>
|
||||
<Row key={i} col={col} content={row[col] as String}></Row>
|
||||
))
|
||||
}
|
||||
</tr>
|
||||
@@ -84,7 +84,7 @@ const ColHeader = (props: {col: String, sortingHandle: (s: String) => void, isSe
|
||||
);
|
||||
}
|
||||
|
||||
const Row = (props: {col: String, content: String}) => {
|
||||
const Row = (props: {col: String, content: String, key: Key}) => {
|
||||
return <td key={props.col as Key}>{props.content}</td>;
|
||||
}
|
||||
|
||||
|
||||
71
task_3_react/src/client/components/FileCard.tsx
Normal file
71
task_3_react/src/client/components/FileCard.tsx
Normal file
@@ -0,0 +1,71 @@
|
||||
import { responseObject } from "../types";
|
||||
|
||||
const FileCard = (props: {
|
||||
fileList: responseObject,
|
||||
fileChangeHandle: (fileName: string) => Promise<void>
|
||||
}) => {
|
||||
|
||||
const convert = (res: responseObject) => {
|
||||
let list = [];
|
||||
for (let i = 0; i < res.names.length; i++) {
|
||||
const elem = {
|
||||
filename: res.names[i],
|
||||
uploadTime: res.uploadTimes[i]
|
||||
}
|
||||
list.push(elem);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
const list = props.fileList != null ? convert(props.fileList) : null;
|
||||
|
||||
return (
|
||||
<article className="wide">
|
||||
<header>
|
||||
<h2>Select a File</h2>
|
||||
</header>
|
||||
<table id="table-content">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Filename</th>
|
||||
<th>Upload Time</th>
|
||||
<th>Action</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{
|
||||
list ? list.map( (file, i) => (
|
||||
<FileRow key={i} filename={file.filename!} uploadTime={file.uploadTime!} fileChangeHandle={props.fileChangeHandle}></FileRow>
|
||||
)) : <tr></tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</article>
|
||||
);
|
||||
}
|
||||
|
||||
const FileRow = (props: {
|
||||
filename: string,
|
||||
uploadTime: string,
|
||||
fileChangeHandle: (fileName: string) => Promise<void>
|
||||
}) => {
|
||||
|
||||
const remFile = async () => {
|
||||
await fetch(`/delete/${props.filename}`, { method: "DELETE" });
|
||||
}
|
||||
|
||||
return (
|
||||
<tr>
|
||||
<td>{props.filename}</td>
|
||||
<td>{props.uploadTime}</td>
|
||||
<td>
|
||||
<div className="action-icons">
|
||||
<i onClick={() => { remFile()} } className="fa-solid fa-trash-can"></i>
|
||||
<i onClick={() => {props.fileChangeHandle(props.filename)}} className="fa-solid fa-file-arrow-down"></i>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
)
|
||||
}
|
||||
|
||||
export default FileCard;
|
||||
@@ -14,6 +14,7 @@ const InfoCard = (props: {
|
||||
<h2>Data infos</h2>
|
||||
</header>
|
||||
<div className="info">
|
||||
{noFileMessage}
|
||||
<h4>Filename</h4>
|
||||
<p>{props.info.filename}</p>
|
||||
|
||||
@@ -26,7 +27,6 @@ const InfoCard = (props: {
|
||||
<h4>Number of rows</h4>
|
||||
<p>{props.info.rowcount}</p>
|
||||
</div>
|
||||
{noFileMessage}
|
||||
</article>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import React from "react";
|
||||
import "./css/Layout.css";
|
||||
import "../css/Layout.css";
|
||||
|
||||
const Layout = (props: { children: React.ReactNode }) => {
|
||||
return (
|
||||
@@ -36,6 +36,19 @@ article {
|
||||
width: 400px;
|
||||
}
|
||||
|
||||
article.wide {
|
||||
width: 800px
|
||||
}
|
||||
|
||||
.action-icons {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 10px;
|
||||
font-size: 18px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
body>main {
|
||||
max-height: calc(100vh - 100px);
|
||||
overflow-y: auto;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { CSV_Data } from './types';
|
||||
import { csv2json } from 'json-2-csv';
|
||||
|
||||
const convertCSVtoJSON = async ( csvText: string ) => {
|
||||
export const convertCSVtoJSON = async ( csvText: string ) => {
|
||||
// Type cast OK, as the typing of the external library is not perfect -> Actually it is.
|
||||
// NOTE: On transpilation to JS, it will be (more or less) disregarded anyway.
|
||||
// If you claim it isn't good typing, it's the same as expecting it to guess the typing,
|
||||
|
||||
6
task_3_react/src/client/types.d.ts
vendored
6
task_3_react/src/client/types.d.ts
vendored
@@ -9,3 +9,9 @@ export type fileInfo = {
|
||||
filesize: string;
|
||||
rowcount: number;
|
||||
}
|
||||
|
||||
// FileCard receives this via props
|
||||
export type responseObject = {
|
||||
names: string[],
|
||||
uploadTimes: string[]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user