Last active
March 23, 2019 05:30
-
-
Save lawreyios/7425836e5359eaba9befdb369a893ec7 to your computer and use it in GitHub Desktop.
csv-uploader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import Dropzone from 'react-dropzone'; | |
import csv from 'csv'; | |
class App extends Component { | |
onDrop(files) { | |
this.setState({ files }); | |
var file = files[0]; | |
const reader = new FileReader(); | |
reader.onload = () => { | |
csv.parse(reader.result, (err, data) => { | |
var userList = []; | |
for (var i = 0; i < data.length; i++) { | |
const name = data[i][0]; | |
const phoneNumber = data[i][1]; | |
const address = data[i][2]; | |
const classType = data[i][3]; | |
const newUser = { "name": name, "phoneNumber": phoneNumber, "address": address, "class": classType }; | |
userList.push(newUser); | |
fetch('https://[FIREBASE_URL]/users.json', { | |
method: 'POST', | |
headers: { | |
'Accept': 'application/json', | |
'Content-Type': 'application/json', | |
}, | |
body: JSON.stringify(newUser) | |
}) | |
}; | |
}); | |
}; | |
reader.readAsBinaryString(file); | |
} | |
render() { | |
const wellStyles = { maxWidth: 400, margin: '0 auto 10px' }; | |
const fontSize = 5; | |
return ( | |
<div align="center" oncontextmenu="return false"> | |
<br /><br /><br /> | |
<div className="dropzone"> | |
<Dropzone accept=".csv" onDropAccepted={this.onDrop.bind(this)}> | |
</Dropzone> | |
<br /><br /><br /> | |
</div> | |
<h2>Upload or drop your <font size={fontSize} color="#00A4FF">CSV</font><br /> file here.</h2> | |
</div> | |
) | |
} | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment