Advertisement
Guest User

List.js

a guest
Apr 7th, 2020
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, {PureComponent} from 'react';
  2. import Table from '../../components/table';
  3. import {connect} from 'react-redux';
  4. import {Link} from 'react-router-dom';
  5. import {
  6.     Container,
  7.     Button,
  8.     Row,
  9.     Col,
  10.     Spinner
  11. } from 'reactstrap';
  12.  
  13. import {
  14.     fetchCarsRequested,
  15.     sortCar,
  16.     submitCarDataRequested,
  17.     deleteCarRequested
  18. } from '../../actions/car'
  19.  
  20. class App extends PureComponent {
  21.     constructor(props) {
  22.         super(props);
  23.         this.state = {
  24.             modalFlag: null
  25.         };
  26.     }
  27.  
  28.     componentDidMount() {
  29.         this.props.getCars();
  30.     }
  31.  
  32.     handlePagination = (skip) => {
  33.         this.props.getCars({skip});
  34.     }
  35.  
  36.     onDelete(id) {
  37.         this.setState(() => ({modalFlag: null}), () => this.props.deleteCar(id));
  38.     }
  39.  
  40.     handleModal(modalFlag) {
  41.         this.setState(() => ({modalFlag}));
  42.     }
  43.  
  44.     render() {
  45.         const {modalFlag} = this.state;
  46.         const {
  47.             cars,
  48.             limit,
  49.             total,
  50.             tableProps,
  51.             onSort,
  52.             loading
  53.         } = this.props;
  54.         return (
  55.             <Container>
  56.                 <Row>
  57.                     <Col>
  58.                         <h3>Tabla de datos </h3>
  59.                     </Col>
  60.                     <Col sm="3">
  61.                         <Button  
  62.                         className="float-right"
  63.                         color="primary"
  64.                         size="lg"
  65.                         tag={Link}
  66.                         to="/cars/edit/new"> Nuevo </Button>
  67.                     </Col>
  68.                 </Row>
  69.                 <hr/>
  70.                 <Row>
  71.                     <Col>
  72.                         {loading && (
  73.                             <Spinner color="danger" />
  74.                         )}
  75.                         {!loading && (
  76.                             <Table {...{
  77.                                 data: cars,
  78.                                 ...tableProps,
  79.                                 onSort,
  80.                                 limit,
  81.                                 total,
  82.                                 onPageClick: this.handlePagination,
  83.                                 linkTo:'cars',
  84.                                 handleModal: this.handleModal,
  85.                                 onDelete: this.onDelete,
  86.                                 modalFlag
  87.                             }}/>
  88.                         )}
  89.                     </Col>
  90.                 </Row>
  91.             </Container>
  92.         )
  93.     }
  94. }
  95.  
  96. const mapStateToProps = (state /* nuestro Store */, ownProps /*  */ ) => {
  97.     const {documents: {cars, limit, total, loading}, tableProps} = state.car;
  98.     return {
  99.         tableProps,
  100.         cars,
  101.         limit,
  102.         total,
  103.         loading
  104.     };
  105. }
  106.  
  107. const mapDispatchToProps = (dispatch /* acciones a disparar */, ownProps /*  */ ) => ({
  108.     getCars: filters => dispatch(fetchCarsRequested(filters)),
  109.     onSort: sort => dispatch(sortCar(sort)),
  110.     submitCarData: () => dispatch(submitCarDataRequested()),
  111.     deleteCar: id => dispatch(deleteCarRequested(id))
  112. })
  113.  
  114. export default connect(
  115.     mapStateToProps, // MaspStateToProps 1
  116.     mapDispatchToProps // MapDispatchToProps 2
  117.     // MergeProps <<<<<  1 + 2 = 3
  118. )(App);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement