Guest User

Untitled

a guest
Feb 15th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { connect } from 'react-redux';
  4.  
  5. import DetailSection from './DetailSection';
  6. import DetailEdit from './DetailEdit';
  7. import { editDetail } from './../../actions';
  8.  
  9. import Grid from '@material-ui/core/Grid';
  10.  
  11.  
  12. class Detail extends React.Component {
  13. constructor(props){
  14. super(props);
  15. this.dispatch = this.props.dispatch;
  16. this.student = this.props.student;
  17. this.studentId = this.props.studentId;
  18.  
  19. this.state = {
  20. updating : {
  21. bio: {
  22. updating: false,
  23. content: this.student.bio
  24. },
  25. techInterests: {
  26. updating: false,
  27. content: this.student.techInterests
  28. },
  29. notes: {
  30. updating: false,
  31. content: this.student.notes
  32. },
  33. redFlagNotes: {
  34. updating: false,
  35. content: this.student.redFlags[0].details
  36. }
  37. }
  38. };
  39. this.handleFormSubmit=this.handleFormSubmit.bind(this);
  40. this.toggleForm=this.toggleForm.bind(this);
  41. }
  42.  
  43. handleFormSubmit(section, content) {
  44. this.dispatch(editDetail(section, content, this.studentId));
  45. this.toggleForm(section, content);
  46. }
  47.  
  48. toggleForm(section, content) {
  49. let visible=!this.state.updating[section].updating;
  50. const sectionUpdated = { ...this.state.updating[section], updating: visible, content: content};
  51. const updating = { ...this.state.updating, [section]: sectionUpdated };
  52. this.setState({
  53. updating
  54. });
  55. }
  56.  
  57. render(){
  58. return (
  59. <Grid item container>
  60. {Object.keys(this.state.updating).map((component, i) => {
  61. const ComponentName = this.state.updating[component].updating ? DetailEdit : DetailSection;
  62. return (
  63. <Grid item key={i}>
  64. <ComponentName
  65. componentName={component}
  66. content={this.state.updating[component].content}
  67. onButtonClick={this.handleFormSubmit}
  68. studentId={this.studentId}
  69. />
  70. </Grid>
  71. );
  72. })}
  73. </Grid>
  74. );
  75. }
  76. }
  77.  
  78. Detail.propTypes = {
  79. studentId: PropTypes.string,
  80. student: PropTypes.object,
  81. dispatch: PropTypes.func
  82. };
  83.  
  84. // export default connect()(Detail);
  85. export default connect()(Detail);
Add Comment
Please, Sign In to add comment