Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import { connect } from 'react-redux';
  3. import ContentHeading from 'components/ContentHeading/ContentHeading';
  4. import * as actionCreators from 'redux/modules/reportConfiguration';
  5. import harvestLogo from './harvest_logo.png';
  6. import { Link, push } from 'react-router';
  7. import formattedDate from 'helpers/formattedDate';
  8. import Confirm from 'components/Confirm/Confirm';
  9. import ReactPaginate from 'react-paginate';
  10.  
  11. class ReportConfigurationList extends React.Component {
  12.   static propTypes = {
  13.     loadReportConfigurations: React.PropTypes.func.isRequired,
  14.     deleteReportConfiguration: React.PropTypes.func.isRequired,
  15.     reportConfiguration: React.PropTypes.object.isRequired,
  16.   };
  17.  
  18.   static contextTypes = {
  19.     router: React.PropTypes.object.isRequired,
  20.   }
  21.  
  22.   state= {
  23.     perPage: 4,
  24.     pageNum: 1,
  25.     currentPage: 1,
  26.     total: 0,
  27.   }
  28.  
  29.   componentWillMount() {
  30.     this.setState({
  31.       currentPage: this.props.location.query.currentPage || this.state.currentPage,
  32.     }, () => {
  33.       this.props.loadReportConfigurations(this.state)
  34.       .then(({ meta, reportConfigurations }) => {
  35.         if (!reportConfigurations.length && this.props.location.query.currentPage !== 1) {
  36.           this.context.router.push('/reports');
  37.         } else {
  38.           this.setState({
  39.             total: meta.count,
  40.             pageNum: Math.ceil(meta.count / this.state.perPage),
  41.           });
  42.         }
  43.       });
  44.     });
  45.   }
  46.  
  47.   componentWillReceiveProps(nextProps) {
  48.     if (nextProps.location.query.currentPage == 1 || !nextProps.location.query.currentPage) {
  49.       this.setState({
  50.         currentPage: 1,
  51.       });
  52.     }
  53.   }
  54.  
  55.   onDeleteClick = (id) => {
  56.     this.props.deleteReportConfiguration(id);
  57.   }
  58.  
  59.   handlePageClick = (data) => {
  60.     this.setState({
  61.       currentPage: data.selected + 1,
  62.     }, () => {
  63.       this.props.loadReportConfigurations(this.state)
  64.       .then(() => this.context.router.push(`/reports/?currentPage=${this.state.currentPage}`));
  65.     });
  66.   };
  67.  
  68.   render() {
  69.     const dataTable = (this.props.reportConfiguration.reportConfigurations)
  70.     ? this.props.reportConfiguration.reportConfigurations.map((reportConf) => {
  71.       let contacts = reportConf.contacts.map((c) => c.fullName).slice(0, 3).join(', ');
  72.       if (reportConf.contacts.length > 3) contacts += '...';
  73.       return (<tr key={reportConf._id}>
  74.         <td>{reportConf.name}</td>
  75.         <td>
  76.           <Link to="/integrations/harvest/">
  77.             <img
  78.               className="integration-favicon img-rounded"
  79.               src={harvestLogo}
  80.               alt="Harvest Logo"
  81.             />
  82.           </Link>
  83.         </td>
  84.         <td>{(reportConf.schedule.typeScheduling === 'automated')
  85.           ? formattedDate.getSchedulingToString(reportConf.schedule.settings)
  86.           : '-'}</td>
  87.         <td>{(reportConf.approve)
  88.             ? (<i className="fa fa-eye" aria-hidden="true" title="Yes"></i>)
  89.             : (<i className="fa fa-eye-slash" aria-hidden="true" title="No"></i>)
  90.           }</td>
  91.         <td>{contacts}</td>
  92.         <td className="actions">
  93.           <div className="btn-group btn-group-sm" role="group">
  94.             <Link
  95.               to={`/reports/run/${reportConf._id}`}
  96.               className="btn btn-default btn-actions"
  97.               title="Generate report"
  98.             >
  99.               <i className="fa fa-file-pdf-o" aria-hidden="true"></i>
  100.             </Link>
  101.             <Link
  102.               to={{
  103.                 pathname: '/reports-history/', query: {
  104.                   reportConfigurationId: reportConf._id, reportConfigurationName: reportConf.name,
  105.                 },
  106.               }}
  107.               className="btn btn-default btn-actions"
  108.               title="History"
  109.             >
  110.               <i className="fa fa-hourglass" aria-hidden="true"></i>
  111.             </Link>
  112.             <Link
  113.               to={`/reports/${reportConf._id}`}
  114.               className="btn btn-default btn-actions"
  115.               title="Edit"
  116.             >
  117.               <i className="fa fa-pencil" />
  118.             </Link>
  119.             <Confirm
  120.               onConfirm={this.onDeleteClick.bind(this, reportConf._id)}
  121.               body="Are you sure you want to delete this report?"
  122.               confirmText="Delete"
  123.               title="Deleting report"
  124.             >
  125.               <button
  126.                 className="btn btn-default btn-actions"
  127.                 title="Delete"
  128.               >
  129.                 <i className="fa fa-trash" />
  130.               </button>
  131.             </Confirm>
  132.           </div>
  133.         </td>
  134.       </tr>);
  135.     })
  136.     : null;
  137.     const table = (this.props.reportConfiguration.reportConfigurations.length > 0)
  138.     ? (
  139.       <div className="panel-body">
  140.         <div>
  141.           <table className="table table-responsive table-bordered table-hover">
  142.             <thead>
  143.               <tr>
  144.                 <th className="text-center">Name</th>
  145.                 <th className="text-center">Type</th>
  146.                 <th className="text-center">Schedule</th>
  147.                 <th className="text-center">Approval</th>
  148.                 <th className="text-center">Send to</th>
  149.                 <th className="text-center">Actions</th>
  150.               </tr>
  151.             </thead>
  152.             <tbody>
  153.               {dataTable}
  154.             </tbody>
  155.           </table>
  156.         </div>
  157.  
  158.       </div>)
  159.       : (<div className="no-data">
  160.         <strong>Heads up! </strong>You haven't got created reports.
  161.      </div>);
  162.  
  163.    const pagination = (this.props.reportConfiguration.reportConfigurations.length)
  164.      ? (<div className="col-md-12 text-center">
  165.        <ReactPaginate
  166.          previousLabel={"previous"}
  167.          nextLabel={"next"}
  168.          breakLabel={<a href="">...</a>}
  169.          breakClassName={"break-me"}
  170.          pageNum={this.state.pageNum}
  171.          marginPagesDisplayed={2}
  172.          pageRangeDisplayed={5}
  173.          clickCallback={this.handlePageClick}
  174.          containerClassName={"pagination"}
  175.          subContainerClassName={"pages pagination"}
  176.          activeClassName={"active"}
  177.        />
  178.      </div>)
  179.    : null;
  180.  
  181.    return (
  182.      <div className="wrapper">
  183.        <div className="content-wrapper">
  184.          <ContentHeading name="Reports" />
  185.          {(this.props.reportConfiguration.isLoading)
  186.            ? (<h2><i className="fa fa-spin fa-spinner"></i> Wait...</h2>)
  187.            :
  188.              (<div className="row">
  189.                <div className="panel panel-default">
  190.                  <div className="panel-heading clearfix">
  191.                    <div className="pull-right">
  192.                      <Link
  193.                        to="/new-report"
  194.                        className="btn btn-success"
  195.                      >
  196.                        <em className="fa fa-plus"> </em>
  197.                        Add report
  198.                      </Link>
  199.                    </div>
  200.                  </div>
  201.                  {table}
  202.                </div>
  203.              </div>)
  204.        }
  205.        {pagination}
  206.        </div>
  207.      </div>
  208.    );
  209.  }
  210. }
  211. export default connect(
  212.  (store) => ({
  213.    reportConfiguration: store.reportConfiguration,
  214.  }), actionCreators)(ReportConfigurationList);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement