fetchPage.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import React, { Component } from "react";
  2. import { connect } from "react-redux";
  3. import { bindActionCreators } from "redux";
  4. import actions from "../actions";
  5. import { takeInputValue, getWeather } from "../actions/weather";
  6. class FetchPage extends Component {
  7. state = {
  8. foo: 1,
  9. files: null,
  10. photo: null
  11. };
  12. add = () => this.setState(prevState => ({ foo: prevState.foo + 1 }));
  13. change = e => {
  14. const { takeInputValue } = this.props;
  15. takeInputValue(e.target.value);
  16. };
  17. click = () => {
  18. const { city, getWeather } = this.props;
  19. getWeather(city);
  20. };
  21. change = e => {
  22. const reader = new FileReader();
  23. const file = e.target.files[0];
  24. this.setState({ file });
  25. reader.onloadend = () => {
  26. this.setState({
  27. photo: reader.result
  28. });
  29. };
  30. reader.readAsDataURL(file);
  31. };
  32. post = () => {
  33. const file = new FormData();
  34. file.append("profilePhoto", this.state.file);
  35. };
  36. render() {
  37. const { weather, error } = this.props;
  38. console.log("this.", this.props);
  39. const { photo } = this.state;
  40. return (
  41. <div className="page">
  42. <input className="page__input page__input--blue" type="text" onChange={this.change} />
  43. <button className="page__button" onClick={this.click}>
  44. SHOW
  45. </button>
  46. <div>
  47. <img src={photo} alt="asdfas" />
  48. <input type="file" onChange={e => this.change(e)} />
  49. <button onClick={this.post}>POST FILE</button>
  50. </div>
  51. <div>{weather && weather.location && weather.location.city}</div>
  52. <div>{error && error}</div>
  53. <button onClick={this.add}>PUSH ME</button>
  54. </div>
  55. );
  56. }
  57. }
  58. const mapStateToProps = state => ({
  59. city: state.w.city,
  60. weather: state.w.weather,
  61. error: state.w.error
  62. });
  63. const mapDispatchToProps = dispatch =>
  64. bindActionCreators({ takeInputValue, getWeather, ...actions }, dispatch);
  65. export default connect(
  66. mapStateToProps,
  67. mapDispatchToProps
  68. )(FetchPage);