Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import matchSorter from 'match-sorter';
  2. import React, { Component } from 'react';
  3. import { Link } from 'react-router-dom';
  4. import ReactTable from "react-table";
  5. import 'react-table/react-table.css';
  6. import { Badge, Button, Card, CardBody, CardHeader, Col, Row } from 'reactstrap';
  7. import AlphabeticalList from '../../../components/AlphabeticalList/AlphabeticalList';
  8. import { connect } from 'react-redux';
  9. import { showModal, hideModal, categoriesLowGetRequest, customFieldsGetByCatRequest, categoriesLowDelRequest } from '../../../redux/actions';
  10.  
  11. class CategoriesLow extends Component {
  12.     constructor(props) {
  13.         super(props);
  14.  
  15.         if(sessionStorage.getItem(this.props.location.pathname)) {
  16.             const local = JSON.parse(sessionStorage.getItem(this.props.location.pathname));
  17.             this.state = {
  18.                 filtered: local.filtered || [],
  19.                 pageIndex: local.pageIndex || 0,
  20.                 pageSize: local.pageSize || 15,
  21.                 sorted: local.sorted || []
  22.             }
  23.         }
  24.     }
  25.        
  26.     onFilteredChangeCustom = (value, accessor) => {
  27.     let filtered = this.state.filtered || [];
  28.     let insertNewFilter = 1;
  29.  
  30.     if (filtered.length) {
  31.       filtered.forEach((filter, i) => {
  32.         if (filter["id"] === accessor) {
  33.           if (value === "" || !value.length) filtered.splice(i, 1);
  34.           else filter["value"] = value;
  35.  
  36.           insertNewFilter = 0;
  37.         }
  38.       });
  39.     }
  40.  
  41.     if (insertNewFilter) {
  42.       filtered.push({ id: accessor, value: value });
  43.     }
  44.  
  45.     this.setState({ filtered: filtered, pageIndex: 0});
  46.     };
  47.    
  48.     onPageChange = (pageIndex) => {
  49.         this.setState({ pageIndex });
  50.     }
  51.  
  52.     onPageSizeChange = (pageSize, pageIndex) => {
  53.         this.setState({
  54.             pageSize,
  55.             pageIndex
  56.         })
  57.     }
  58.  
  59.     onSortedChange = (sorted, column) => {     
  60.         this.setState({ sorted });     
  61.     }
  62.  
  63.     deleteData = (id) => {
  64.         this.props.categoriesLowDelRequest(id);
  65.         this.props.hideModal();
  66.     }
  67.  
  68.     setCustomFields = (id, description) => {
  69.         this.customFieldsGetByCatRequest(id);
  70.  
  71.         this.props.showModal({
  72.             title: `Set custom fields for ${description}`,
  73.             message: <AlphabeticalList data={this.props.customFieldsByCat} onSelect={(id) => this.selectElement(id)} />,
  74.             size: 'modal-xl',
  75.             cancelAction: null,
  76.             confirmAction: this.saveSelection
  77.         })
  78.     }
  79.  
  80.     componentDidMount() {
  81.         this.props.categoriesLowRequest();     
  82.     }
  83.  
  84.     componentWillUnmount() {
  85.         sessionStorage.setItem(
  86.             this.props.location.pathname,
  87.             JSON.stringify({
  88.                 filtered: this.state.filtered,
  89.                 pageIndex: this.state.pageIndex,
  90.                 pageSize: this.state.pageSize,
  91.                 sorted: this.state.sorted
  92.             })
  93.         );
  94.     }
  95.  
  96.     render () {
  97.         const data = this.props.categoriesLow;
  98.    
  99.         const columns = [
  100.             {
  101.                 Header: 'High Category',
  102.                 columns: [
  103.                     {
  104.                         Header: 'Code',
  105.                         accessor: 'cd_categories1',    
  106.                         maxWidth: 50,
  107.                         filterMethod: (filter, rows) =>
  108.                                                     matchSorter(rows, filter.value, { keys: ["cd_categories1"] }),
  109.                         filterAll: true                                        
  110.                     },
  111.                     {
  112.                         Header: 'Description',
  113.                         accessor: 'categories1_desc',  
  114.                         width: 250,
  115.                         filterMethod: (filter, rows) =>
  116.                                                 matchSorter(rows, filter.value, { keys: ["categories1_desc"] }),
  117.                         filterAll: true,                                   
  118.                     }
  119.                 ]              
  120.             },
  121.             {
  122.                 Header: 'Mid Category',
  123.                 columns: [
  124.                     {
  125.                         Header: 'Code',
  126.                         accessor: 'cd_categories2',    
  127.                         maxWidth: 50,  
  128.                         filterMethod: (filter, rows) =>
  129.                                                 matchSorter(rows, filter.value, { keys: ["cd_categories2"] }),
  130.                         filterAll: true,                                   
  131.                     },
  132.                     {
  133.                         Header: 'Description',
  134.                         accessor: 'categories2_desc',  
  135.                         width: 250,                                        
  136.                         filterMethod: (filter, rows) =>
  137.                                                 matchSorter(rows, filter.value, { keys: ["categories2_desc"] }),
  138.                         filterAll: true,                                   
  139.                     }
  140.                 ]              
  141.             },
  142.             {
  143.                 Header: 'Low Category',
  144.                 columns: [
  145.                     {
  146.                         Header: 'Code',
  147.                         accessor: 'cd_categories3',    
  148.                         maxWidth: 50,  
  149.                         filterMethod: (filter, rows) =>
  150.                                                 matchSorter(rows, filter.value, { keys: ["cd_categories3"] }),
  151.                         filterAll: true,
  152.                         Cell: row => (
  153.                             <Link to={`/configuration/categorieslow/${row.original.id}`}>
  154.                                 {row.value}
  155.                             </Link>
  156.                         )
  157.                     },
  158.                     {
  159.                         Header: 'Description',
  160.                         id: "description",
  161.                         width: 300,
  162.                         accessor: d => d.description,
  163.                         filterMethod: (filter, rows) =>
  164.                                                 matchSorter(rows, filter.value, { keys: ["description"] }),
  165.                         filterAll: true
  166.                     },
  167.                 ]
  168.             },
  169.             {
  170.                 Header: 'Category Browse',
  171.                 accessor: 'categoriesbrowse_desc',
  172.                 width: 300,
  173.                 filterMethod: (filter, rows) =>
  174.                                                 matchSorter(rows, filter.value, { keys: ["categoriesbrowse_desc"] }),
  175.                 filterAll: true,
  176.             },
  177.             {
  178.                 Header: '',
  179.                 columns: [
  180.                     {
  181.                         filterable: false,
  182.                         Cell: row => (
  183.                             <span>
  184.                                 <Button color="danger" size="sm" onClick={() => {
  185.                                         this.props.showModal({
  186.                                             title: 'Confirm Operation',
  187.                                             message: `Are you sure you want to delete this element: ${row.original.cd_categories3} - ${row.original.description}?`,
  188.                                             size: 'modal-lg',
  189.                                             cancelAction: null,
  190.                                             confirmAction: () => this.deleteData(row.original.id)
  191.                                         })
  192.                                 }}><i className="fa fa-remove"></i> Delete</Button> {' '}
  193.                                
  194.                                 <Button color="info" size="sm" onClick={() => {
  195.                                                                                                                                 this.props.customFieldsGetByCatRequest(row.original.id);
  196.  
  197.                                                                                                                                 this.props.showModal({
  198.                                                                                                                                     title: `Set custom fields for ${row.original.description}`,
  199.                                                                                                                                     message: <AlphabeticalList data={this.props.customFieldsByCat} onSelect={(id) => this.selectElement(row.original.id)} />,
  200.                                                                                                                                     size: 'modal-xl',
  201.                                                                                                                                     cancelAction: null,
  202.                                                                                                                                     confirmAction: this.saveSelection
  203.                                                                                                                                 })                                                                                                                         
  204.                                                                                                                             }
  205.                                                                                                                 }>                             
  206.                                     <i className="fa fa-address-book"></i> Set custom fields  <Badge color="warning" pill>{row.original.CustomFieldCount}</Badge>
  207.                                 </Button>
  208.                             </span>
  209.                         )
  210.                     }
  211.                 ]              
  212.             }
  213.         ];
  214.        
  215.         return (           
  216.             <div className="animated fadeIn">              
  217.                 <Row>
  218.                     <Col xs="12">
  219.                         <Card>
  220.                             <CardHeader>
  221.                                 <strong>Categories (LOW)</strong>
  222.                                 <small> manage</small>
  223.                                 <div className="card-header-actions">
  224.                                     <Button type="submit" size="sm" color="primary" onClick={() => {
  225.                                                 this.props.history.push('/configuration/categorieslow/new');
  226.                                             }}><i className="fa fa-dot-circle-o"></i> New</Button>{' '}
  227.                                     <Button type="submit" size="sm" color="secondary" onClick={() => {
  228.                                            
  229.                                         }}><i className="fa fa-print"></i> Print</Button>
  230.                 </div>
  231.                             </CardHeader>
  232.                             <CardBody>
  233.                                 <ReactTable
  234.                                     data={data}
  235.                                     columns={columns}
  236.                                     filterable
  237.                                     filtered={this.state.filtered}
  238.                                     page={this.state.pageIndex}
  239.                                     onPageChange={pageIndex => {
  240.                                         this.onPageChange(pageIndex);
  241.                                     }}
  242.                                 onPageSizeChange={(pageSize, pageIndex) => {
  243.                                         this.onPageSizeChange(pageSize, pageIndex);
  244.                                     }}
  245.                                 onSortedChange={(sorted, column) => {
  246.                                         this.onSortedChange(sorted, column);
  247.                                     }}
  248.                                     onFilteredChange={(filtered, column, value) => {
  249.                                         this.onFilteredChangeCustom(value, column.id || column.accessor);
  250.                                     }}
  251.                                     sorted={this.state.sorted}
  252.                                     pageSizeOptions={[15,25,50,100]}
  253.                                     defaultPageSize={this.state.pageSize}
  254.                                     loader={false}
  255.                                     resizable={true}
  256.                                     className="-striped -highlight"
  257.                                 />
  258.                                                        
  259.                             </CardBody>
  260.                         </Card>
  261.                     </Col>
  262.                 </Row>
  263.             </div>
  264.         )
  265.     }
  266. }
  267.  
  268. const mapStateToProps = state => ({
  269.     categoriesLow: state.categoriesLow.list,
  270.     customFieldsByCat: state.customFields.byCategory,
  271.     errorCustomField: state.customFields.error
  272. });
  273.  
  274. const mapDispatchToProps = dispatch => ({
  275.     categoriesLowRequest: () => dispatch(categoriesLowGetRequest()),
  276.     categoriesLowDelRequest: (id) => dispatch(categoriesLowDelRequest(id)),
  277.     customFieldsGetByCatRequest: (categoryId) => dispatch(customFieldsGetByCatRequest(categoryId)),
  278.     showModal: (modalProps) => dispatch(showModal(modalProps)),
  279.     hideModal: () => dispatch(hideModal())
  280. });
  281.  
  282. export default connect(mapStateToProps, mapDispatchToProps)(CategoriesLow);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement