feat(input-csv-twice): initial commit
This commit is contained in:
parent
aece34978e
commit
22187e6564
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"presets": [
|
||||||
|
"@babel/preset-env",
|
||||||
|
"@babel/preset-react"
|
||||||
|
]
|
||||||
|
}
|
|
@ -0,0 +1,107 @@
|
||||||
|
# Others
|
||||||
|
build*
|
||||||
|
|
||||||
|
# Logs
|
||||||
|
logs
|
||||||
|
*.log
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
lerna-debug.log*
|
||||||
|
|
||||||
|
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||||
|
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
||||||
|
|
||||||
|
# Runtime data
|
||||||
|
pids
|
||||||
|
*.pid
|
||||||
|
*.seed
|
||||||
|
*.pid.lock
|
||||||
|
|
||||||
|
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||||
|
lib-cov
|
||||||
|
|
||||||
|
# Coverage directory used by tools like istanbul
|
||||||
|
coverage
|
||||||
|
*.lcov
|
||||||
|
|
||||||
|
# nyc test coverage
|
||||||
|
.nyc_output
|
||||||
|
|
||||||
|
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
||||||
|
.grunt
|
||||||
|
|
||||||
|
# Bower dependency directory (https://bower.io/)
|
||||||
|
bower_components
|
||||||
|
|
||||||
|
# node-waf configuration
|
||||||
|
.lock-wscript
|
||||||
|
|
||||||
|
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||||
|
build/Release
|
||||||
|
|
||||||
|
# Dependency directories
|
||||||
|
node_modules/
|
||||||
|
jspm_packages/
|
||||||
|
|
||||||
|
# TypeScript v1 declaration files
|
||||||
|
typings/
|
||||||
|
|
||||||
|
# TypeScript cache
|
||||||
|
*.tsbuildinfo
|
||||||
|
|
||||||
|
# Optional npm cache directory
|
||||||
|
.npm
|
||||||
|
|
||||||
|
# Optional eslint cache
|
||||||
|
.eslintcache
|
||||||
|
|
||||||
|
# Microbundle cache
|
||||||
|
.rpt2_cache/
|
||||||
|
.rts2_cache_cjs/
|
||||||
|
.rts2_cache_es/
|
||||||
|
.rts2_cache_umd/
|
||||||
|
|
||||||
|
# Optional REPL history
|
||||||
|
.node_repl_history
|
||||||
|
|
||||||
|
# Output of 'npm pack'
|
||||||
|
*.tgz
|
||||||
|
|
||||||
|
# Yarn Integrity file
|
||||||
|
.yarn-integrity
|
||||||
|
|
||||||
|
# dotenv environment variables file
|
||||||
|
.env
|
||||||
|
.env.test
|
||||||
|
|
||||||
|
# parcel-bundler cache (https://parceljs.org/)
|
||||||
|
.cache
|
||||||
|
|
||||||
|
# Next.js build output
|
||||||
|
.next
|
||||||
|
|
||||||
|
# Nuxt.js build / generate output
|
||||||
|
.nuxt
|
||||||
|
dist
|
||||||
|
|
||||||
|
# Gatsby files
|
||||||
|
.cache/
|
||||||
|
# Comment in the public line in if your project uses Gatsby and *not* Next.js
|
||||||
|
# https://nextjs.org/blog/next-9-1#public-directory-support
|
||||||
|
# public
|
||||||
|
|
||||||
|
# vuepress build output
|
||||||
|
.vuepress/dist
|
||||||
|
|
||||||
|
# Serverless directories
|
||||||
|
.serverless/
|
||||||
|
|
||||||
|
# FuseBox cache
|
||||||
|
.fusebox/
|
||||||
|
|
||||||
|
# DynamoDB Local files
|
||||||
|
.dynamodb/
|
||||||
|
|
||||||
|
# TernJS port file
|
||||||
|
.tern-port
|
|
@ -0,0 +1,19 @@
|
||||||
|
# React.js Example
|
||||||
|
|
||||||
|
## Table of Contents
|
||||||
|
0. [General](#general)
|
||||||
|
1. [Links](#links)
|
||||||
|
|
||||||
|
# General
|
||||||
|
|
||||||
|
# Links
|
||||||
|
|
||||||
|
* [React setup with webpack for beginners](https://dev.to/deepanjangh/react-setup-with-webpack-for-beginners-2a8k)
|
||||||
|
* [Production](https://webpack.js.org/guides/production/)
|
||||||
|
* [Setup Development and Production Environment for React App](https://medium.com/freestoneinfotech/setup-development-and-production-environment-for-react-app-397c4cc9e382)
|
||||||
|
* [HtmlWebpackPlugin](https://webpack.js.org/plugins/html-webpack-plugin/)
|
||||||
|
* [load CSS](https://masteringjs.io/tutorials/webpack/css-loader)
|
||||||
|
* [load CSS](https://webpack.js.org/loaders/css-loader/)
|
||||||
|
* [load CSS](https://blog.jakoblind.no/css-modules-webpack/)
|
||||||
|
* [How to Read CSV files in React.js ?](https://www.geeksforgeeks.org/how-to-read-csv-files-in-react-js/)
|
||||||
|
* [csv file as input from user using React](https://stackoverflow.com/questions/70001833/take-a-csv-file-as-input-from-user-using-react-and-send-it-as-a-post-request-usi)
|
|
@ -0,0 +1,10 @@
|
||||||
|
import React from 'react';
|
||||||
|
import Home from './pages/home';
|
||||||
|
export default function App() {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<h1>App</h1>
|
||||||
|
<Home />
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
|
@ -0,0 +1,174 @@
|
||||||
|
import React, { useState } from "react";
|
||||||
|
import Papa from "papaparse";
|
||||||
|
import axios from 'axios';
|
||||||
|
|
||||||
|
const allowedExtensions = ["csv"];
|
||||||
|
|
||||||
|
const CsvReader = () => {
|
||||||
|
const fileStopUnavailable = '/stop\\';
|
||||||
|
const fileZoneUnavailable = '/zone\\';
|
||||||
|
|
||||||
|
const [dataStop, setDataStop] = useState([]);
|
||||||
|
const [dataZone, setDataZone] = useState([]);
|
||||||
|
const [errorStop, setErrorStop] = useState('');
|
||||||
|
const [errorZone, setErrorZone] = useState('');
|
||||||
|
const [fileStop, setFileStop] = useState(fileStopUnavailable);
|
||||||
|
const [fileZone, setFileZone] = useState(fileZoneUnavailable);
|
||||||
|
const [isDataStopCompleted,setIsDataStopCompleted] = useState(false);
|
||||||
|
const [isDataZoneCompleted,setIsDataZoneCompleted] = useState(false);
|
||||||
|
|
||||||
|
const handleChangeStop = (e) => {
|
||||||
|
setErrorStop("");
|
||||||
|
|
||||||
|
//check if user has entered the file
|
||||||
|
if (e.target.files.length) {
|
||||||
|
const inputFile = e.target.files[0];
|
||||||
|
|
||||||
|
//check the file extensions
|
||||||
|
const fileExtension = inputFile?.type.split("/")[1];
|
||||||
|
if (!allowedExtensions.includes(fileExtension)) {
|
||||||
|
setErrorStop("Error! Enter file in CSV format:");
|
||||||
|
}else{
|
||||||
|
setFileStop(inputFile);
|
||||||
|
//console.log('handleChangeStop() file updated');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleChangeZone = (e) => {
|
||||||
|
setErrorZone("");
|
||||||
|
|
||||||
|
//check if user has entered the file
|
||||||
|
if (e.target.files.length) {
|
||||||
|
const inputFile = e.target.files[0];
|
||||||
|
|
||||||
|
//check the file extensions
|
||||||
|
const fileExtension = inputFile?.type.split("/")[1];
|
||||||
|
if (!allowedExtensions.includes(fileExtension)) {
|
||||||
|
setErrorZone("Error! Enter file in CSV format:");
|
||||||
|
}else{
|
||||||
|
setFileZone(inputFile);
|
||||||
|
//console.log('handleChangeZone() file updated');
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
console.error('handleChangeZone() Is this a possible case?');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const postData = async (requestBody) => {
|
||||||
|
//console.log('post() start...');
|
||||||
|
//console.log('post() requestBody: ' + JSON.stringify(requestBody));
|
||||||
|
/*If you pass a JavaScript object as the 2nd parameter to the axios.post() function, Axios will automatically serialize the object to JSON for you. Axios will also set the Content-Type header to 'application/json', so web frameworks like Express can automatically parse it.*/
|
||||||
|
axios.post('http://localhost:3000/', requestBody);
|
||||||
|
//console.log('post() done.');
|
||||||
|
};
|
||||||
|
|
||||||
|
const updateDataStop = (result) => {
|
||||||
|
//console.log('updateDataStop() start...');
|
||||||
|
setDataStop(result.data);
|
||||||
|
setIsDataStopCompleted(true);
|
||||||
|
//console.log('updateDataStop() done.');
|
||||||
|
};
|
||||||
|
|
||||||
|
const updateDataZone = (result) => {
|
||||||
|
//console.log('updateDataZone() start...');
|
||||||
|
setDataZone(result.data);
|
||||||
|
setIsDataZoneCompleted(true);
|
||||||
|
//console.log('updateDataZone() done.');
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSubmit = (e) => {
|
||||||
|
//console.log('handleSubmit() start...');
|
||||||
|
e.preventDefault();
|
||||||
|
if (fileStop === fileStopUnavailable) {
|
||||||
|
//console.log('handleSubmit() CSV stop file NOT valid');
|
||||||
|
return setErrorStop("Error! Enter a valid CSV stop file:");
|
||||||
|
} else if (fileZone === fileZoneUnavailable) {
|
||||||
|
//console.log('handleSubmit() CSV zone file NOT valid');
|
||||||
|
return setErrorZone("Error! Enter a valid CSV zone file:");
|
||||||
|
} else {
|
||||||
|
//console.log('handleSubmit() both files valid');
|
||||||
|
Papa.parse(fileStop, {
|
||||||
|
complete: updateDataStop,
|
||||||
|
header: true
|
||||||
|
});
|
||||||
|
//console.log('handleSubmit() parse called on stops');
|
||||||
|
Papa.parse(fileZone, {
|
||||||
|
complete: updateDataZone,
|
||||||
|
header: true
|
||||||
|
});
|
||||||
|
//console.log('handleSubmit() parse called on zones');
|
||||||
|
}
|
||||||
|
//console.log('handleSubmit() done.');
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSend = () => {
|
||||||
|
const dataPost = [];
|
||||||
|
dataPost[0] = dataStop;
|
||||||
|
dataPost[1] = dataZone;
|
||||||
|
postData(dataPost);
|
||||||
|
setIsDataStopCompleted(false);
|
||||||
|
setDataStop([]);
|
||||||
|
setErrorStop('');
|
||||||
|
setFileStop(fileStopUnavailable);
|
||||||
|
setIsDataZoneCompleted(false);
|
||||||
|
setDataZone([]);
|
||||||
|
setErrorZone('');
|
||||||
|
setFileZone(fileZoneUnavailable);
|
||||||
|
};
|
||||||
|
|
||||||
|
const ButtonSend = () => {
|
||||||
|
if(isDataStopCompleted && isDataZoneCompleted){
|
||||||
|
//console.log('buttonSend() both completed');
|
||||||
|
return(
|
||||||
|
<div>
|
||||||
|
<label htmlFor="button-send">4. Send input:</label>
|
||||||
|
<button className ="button-send" id="button-send" type="button" onClick={handleSend}>Send</button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}else{
|
||||||
|
//console.log('buttonSend() both NOT completed');
|
||||||
|
return(
|
||||||
|
<div>
|
||||||
|
{(!isDataStopCompleted || !isDataZoneCompleted) ?
|
||||||
|
<label htmlFor="button-send">4. Push Submit before Send!</label> :
|
||||||
|
null}
|
||||||
|
<button type="button" disabled>Send</button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return(
|
||||||
|
<div className="csv-reader">
|
||||||
|
<h3>CsvReader</h3>
|
||||||
|
<form onSubmit={handleSubmit}>
|
||||||
|
<label htmlFor="csv-input-stop">{errorStop ? '1. ' + errorStop : '1. CSV stop file:'}</label>
|
||||||
|
<input
|
||||||
|
className="csv-input-stop"
|
||||||
|
id="csv-input-stop"
|
||||||
|
type="file"
|
||||||
|
name="file"
|
||||||
|
placeholfer={null}
|
||||||
|
onChange={handleChangeStop}
|
||||||
|
/>
|
||||||
|
<br/>
|
||||||
|
<label htmlFor="csv-input-zone">{errorZone ? '2. ' + errorZone : '2. CSV zone file:'}</label>
|
||||||
|
<input
|
||||||
|
className="csv-input-zone"
|
||||||
|
id="csv-input-zone"
|
||||||
|
type="file"
|
||||||
|
name="file"
|
||||||
|
placeholfer={null}
|
||||||
|
onChange={handleChangeZone}
|
||||||
|
/>
|
||||||
|
<br />
|
||||||
|
<label htmlFor="button-submit">3. Submit:</label>
|
||||||
|
<button type="submit" className="button-submit" id="button-submit">
|
||||||
|
Submit
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
<ButtonSend />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default CsvReader;
|
|
@ -0,0 +1,17 @@
|
||||||
|
import React from 'react';
|
||||||
|
import ReactDOM from 'react-dom';
|
||||||
|
import App from './app';
|
||||||
|
//TODO remove debugging
|
||||||
|
if (process.env.NODE_ENV !== 'production') {
|
||||||
|
console.log('development mode');
|
||||||
|
}
|
||||||
|
//since react 18
|
||||||
|
import { createRoot } from 'react-dom/client';
|
||||||
|
//create root container
|
||||||
|
const root = createRoot(document.getElementById("root"));
|
||||||
|
//render root app
|
||||||
|
root.render(
|
||||||
|
<React.StrictMode>
|
||||||
|
<App />
|
||||||
|
</React.StrictMode>
|
||||||
|
);
|
|
@ -0,0 +1,15 @@
|
||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
import Csv from '../components/csv-reader';
|
||||||
|
import '../style.css';
|
||||||
|
|
||||||
|
const Home = () => {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<h2>Home</h2>
|
||||||
|
<Csv />
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Home
|
|
@ -0,0 +1,3 @@
|
||||||
|
h1 { color: red; }
|
||||||
|
h2 { color: green; }
|
||||||
|
h3 { color: blue; }
|
|
@ -0,0 +1,41 @@
|
||||||
|
//generate a HTML5 file including all webpack bundles in the body using script tags
|
||||||
|
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
||||||
|
//path is used to resolve properly across the OS
|
||||||
|
const path = require('path');
|
||||||
|
module.exports = {
|
||||||
|
//bundle *.js from this entry point
|
||||||
|
entry: path.resolve(__dirname, '../app/index.jsx'),
|
||||||
|
//create output file to be linked to index.html
|
||||||
|
output: {
|
||||||
|
filename: '[name].bundle.js',
|
||||||
|
path: path.resolve(__dirname, '../dist'),
|
||||||
|
clean: true,
|
||||||
|
},
|
||||||
|
module: {
|
||||||
|
rules: [
|
||||||
|
{
|
||||||
|
//test all *.js using babel-loader
|
||||||
|
//test all *.jsx (e.g. React.js) using babel-loader
|
||||||
|
test: /\.(js|jsx)$/,
|
||||||
|
exclude: /node_modules/,
|
||||||
|
include: path.resolve(__dirname, '../app'),
|
||||||
|
use: ['babel-loader'],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
//test all *.css using style-loader and css-loader
|
||||||
|
test: /\.css$/i,
|
||||||
|
use: ["style-loader", "css-loader"],
|
||||||
|
},
|
||||||
|
]
|
||||||
|
},
|
||||||
|
resolve: {
|
||||||
|
extensions: ['*', '.js', '.jsx'],
|
||||||
|
},
|
||||||
|
plugins: [
|
||||||
|
// create a plugin instance so that you can use it several times anywhere
|
||||||
|
new HtmlWebpackPlugin({
|
||||||
|
title: 'Production',
|
||||||
|
template: path.resolve(__dirname, "../public/index.html")
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
};
|
|
@ -0,0 +1,14 @@
|
||||||
|
//path is used to resolve properly across the OS
|
||||||
|
const path = require('path');
|
||||||
|
const { merge } = require('webpack-merge');
|
||||||
|
const common = require('./webpack.common.js');
|
||||||
|
//merge() calls in the environment-specific configuration to include commons
|
||||||
|
module.exports = merge(common, {
|
||||||
|
//set development mode
|
||||||
|
mode: 'development',
|
||||||
|
//enable strong source mapping
|
||||||
|
devtool: 'inline-source-map',
|
||||||
|
devServer: {
|
||||||
|
static: path.resolve(__dirname, '../dist'),
|
||||||
|
},
|
||||||
|
});
|
|
@ -0,0 +1,8 @@
|
||||||
|
const { merge } = require('webpack-merge');
|
||||||
|
const common = require('./webpack.common.js');
|
||||||
|
module.exports = merge(common, {
|
||||||
|
mode: 'production',
|
||||||
|
//source maps encouraged in production
|
||||||
|
//choose mapping with fairly quick build speed like source-map
|
||||||
|
devtool: 'source-map',
|
||||||
|
});
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,39 @@
|
||||||
|
{
|
||||||
|
"private": true,
|
||||||
|
"name": "react-example",
|
||||||
|
"description": "React.js example",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"main": "index.js",
|
||||||
|
"keywords": [
|
||||||
|
"react",
|
||||||
|
"webpack"
|
||||||
|
],
|
||||||
|
"author": "Software Ingenieur Begerad <dialog@SwIngBe.de>",
|
||||||
|
"license": "GPL-3.0-or-later",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=10"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"start": "webpack serve --config config/webpack.dev.js",
|
||||||
|
"build": "webpack --config config/webpack.prod.js"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@babel/core": "7.21.4",
|
||||||
|
"@babel/preset-env": "7.21.4",
|
||||||
|
"@babel/preset-react": "7.18.6",
|
||||||
|
"babel-loader": "9.1.2",
|
||||||
|
"css-loader": "6.7.3",
|
||||||
|
"html-webpack-plugin": "5.5.1",
|
||||||
|
"style-loader": "3.3.2",
|
||||||
|
"webpack": "5.80.0",
|
||||||
|
"webpack-cli": "5.0.1",
|
||||||
|
"webpack-dev-server": "4.13.3",
|
||||||
|
"webpack-merge": "5.8.0"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"axios": "1.4.0",
|
||||||
|
"papaparse": "5.4.1",
|
||||||
|
"react": "18.2.0",
|
||||||
|
"react-dom": "18.2.0"
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>React Example</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="root"></div>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in New Issue