Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. import React, { Component } from "react";
  2. import NewItemForm from "../../common-components/NewItemForm";
  3. import {
  4. deleteHero,
  5. loadHeroes,
  6. postHero
  7. } from "../../store/hero/hero-actions";
  8. import { connect } from "react-redux";
  9. import { Link } from "react-router-dom";
  10.  
  11. class Heroes extends Component {
  12. state = {
  13. hero: {
  14. id: "",
  15. firstName: "",
  16. lastName: "",
  17. house: "",
  18. knownAs: ""
  19. },
  20. isShowNewItemForm: false
  21. };
  22.  
  23. async componentDidMount() {
  24. await this.props.onLoadData(); // Dispatches the loadHeroes action
  25. }
  26.  
  27. removeItem = async (id, name) => {
  28. const isConfirmed = window.confirm(`Delete ${name}?`);
  29. if (!isConfirmed) return;
  30.  
  31. await this.props.onRemoveHero(id); // Takes an id to be dispatched. This will delete the hero
  32. };
  33.  
  34. onChange = ({ currentTarget: input }) => {
  35. const newHero = { ...this.state.hero };
  36. const { name, value } = input;
  37. newHero[name] = value;
  38. this.setState({ hero: newHero });
  39. };
  40.  
  41. onSubmit = event => {
  42. event.preventDefault();
  43.  
  44. /*
  45. onAddHero takes a hero object to be dispatched.
  46. If successful, it sends a dispatch to load again the heroes array
  47. */
  48. this.props.onAddHero(this.state.hero).then(() => this.props.onLoadData());
  49. // .then(() => this.props.onLoadData());
  50. // is not necessary if you don't care about the id of the new created item
  51. // An id is required when deleting or getting an item
  52.  
  53. const { isShowNewItemForm } = this.state;
  54. this.setState({ isShowNewItemForm: !isShowNewItemForm });
  55. };
  56.  
  57. showNewItemForm = () => {
  58. const { isShowNewItemForm } = this.state;
  59. this.setState({ isShowNewItemForm: !isShowNewItemForm });
  60. };
  61.  
  62. render() {
  63. const { error } = this.props;
  64. return (
  65. <>
  66. <NewItemForm
  67. isShowNewItemForm={this.state.isShowNewItemForm}
  68. handleOnChange={this.onChange}
  69. handleOnSubmit={this.onSubmit}
  70. handleShowNewItemForm={this.showNewItemForm}
  71. />
  72. {error && (
  73. <div
  74. className="col-3 col-md-3 offset-9 alert alert-info"
  75. role="alert"
  76. >
  77. Something wrong happened: {error}
  78. </div>
  79. )}
  80. {this.props.heroes.map(item => ( // the array heroes here is the array heroes from your store
  81. <div key={item.id} className="card mt-3" style={{ width: "auto" }}>
  82. <div className="card-header">
  83. <h3 className="card-title">
  84. {item.firstName} {item.lastName}
  85. </h3>
  86. <h5 className="card-subtitle mb-2 text-muted">{item.house}</h5>
  87. <p className="card-text">{item.knownAs}</p>
  88. </div>
  89. <section className="card-body">
  90. <div className="row">
  91. <button
  92. onClick={() => this.removeItem(item.id, item.firstName)}
  93. className="btn btn-outline-danger card-link col text-center"
  94. >
  95. <span className="fas fa-eraser mr-2" />
  96. Delete
  97. </button>
  98. <Link
  99. to={`/edit-hero/${item.id}`}
  100. className="btn btn-outline-primary card-link col text-center"
  101. >
  102. <span className="fas fa-edit mr-2" />
  103. Edit
  104. </Link>
  105. </div>
  106. </section>
  107. </div>
  108. ))}
  109. </>
  110. );
  111. }
  112. }
  113.  
  114. const mapStateToProps = state => {
  115. return state.heroState;
  116. };
  117.  
  118. const mapDispatchToProps = dispatch => {
  119. return {
  120. onLoadData: () => dispatch(loadHeroes()), // onLoadDate will dispatch loadHeroes when gets invoke
  121. onRemoveHero: id => dispatch(deleteHero(id)), // onRemoveHero will dispatch deleteHero when gets invoke
  122. onAddHero: hero => dispatch(postHero(hero)) // onAddHero will dispatch postHero when gets invoke
  123. };
  124. };
  125.  
  126. /*
  127. connects the store's dispatches and states to the props of this class
  128. */
  129. export default connect(
  130. mapStateToProps,
  131. mapDispatchToProps
  132. )(Heroes);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement