Guest User

Untitled

a guest
Feb 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. import React from 'react'
  2. import { connect } from 'react-redux'
  3. import * as actions from './actions'
  4.  
  5. class File extends React.Component {
  6. constructor (props) {
  7. super(props)
  8. }
  9. handleChange (e) {
  10. e.preventDefault();
  11. let reader = new FileReader();
  12. let file = e.target.files[0];
  13. reader.onloadend = () => {
  14. let text = reader.result;
  15. this.props.jsonLoad(JSON.parse(text))
  16. }
  17. reader.readAsText(file)
  18. }
  19.  
  20. render () {
  21. return (
  22. <div>
  23. <h3> Add your file: </h3>
  24. <input type="file" multiple onChange={this.handleChange.bind(this)}/>
  25. <button >
  26. add text from file
  27. </button>
  28. </div>
  29. )
  30. }
  31. }
  32.  
  33. const mapStateToProps = (state) => {
  34. return {
  35. json: state.json
  36. }
  37. }
  38.  
  39. function mapDispatchToProps (dispatch) {
  40. return {
  41. jsonLoad: (json) => dispatch(actions.jsonLoad(json)),
  42. }
  43. }
  44.  
  45. export default connect(mapStateToProps, mapDispatchToProps)(File)
  46.  
  47. const USER_JSON = 'USER_JSON';
  48. export function jsonLoad(json) {
  49. console.log( json)
  50. return function (dispatch) {
  51. let file = json
  52. dispatch(addFile(file))
  53. }
  54. }
  55.  
  56. export function addFile (json) {
  57. return{
  58. type: 'USER_JSON',
  59. payload: json
  60. }
  61. };
Add Comment
Please, Sign In to add comment