Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import { Grid, Button } from 'semantic-ui-react';
  3. import Table from 'components/Table';
  4.  
  5. import { formatDateTime } from 'utils/datetimeHelper';
  6. import { formatMoney } from 'utils/moneyFormatHelper';
  7. import history from 'utils/history';
  8. import httpHandler from 'utils/httpHandler';
  9. import {
  10.   PORTFOLIO_AGGREGATION_LIST_URL,
  11.   PORTFOLIO_CURRENT_AGGREGATION_LIST_URL,
  12. } from 'utils/constants';
  13.  
  14. export default class PortfolioAggregation extends React.Component {
  15.   constructor(props) {
  16.     super(props);
  17.  
  18.     const { match } = this.props;
  19.     const { employeeId, id } = match.params;
  20.  
  21.     this.state = {
  22.       data: [],
  23.       portfolioId: id,
  24.       isLoading: false,
  25.       employeeId,
  26.       totalPages: 1,
  27.     };
  28.   }
  29.  
  30.   fetchData = (filter) => {
  31.     this.setState({ isLoading: true });
  32.     const { portfolioId, employeeId } = this.state;
  33.  
  34.     const url = employeeId === 'current' ? PORTFOLIO_CURRENT_AGGREGATION_LIST_URL : PORTFOLIO_AGGREGATION_LIST_URL;
  35.  
  36.     httpHandler.post(url, { ...filter, portfolioId })
  37.       .then((response) => {
  38.         this.setState({
  39.           data: response.data.data,
  40.           name: response.data.name,
  41.           totalPages: response.data.totalPages,
  42.           isLoading: false,
  43.         });
  44.       })
  45.       .catch(() => {
  46.         this.setState({ isLoading: false });
  47.       });
  48.   };
  49.  
  50.   getColumns = () => [
  51.     {
  52.       id: 0, title: 'NAME', dataJsonPath: 'accountName', width: 4,
  53.     },
  54.     {
  55.       id: 1,
  56.       title: 'LAST LOAD',
  57.       dataJsonPath: 'lastLoadDate',
  58.       render: ({ lastLoadDate }) => formatDateTime(lastLoadDate),
  59.       width: 1,
  60.       align: 'center',
  61.     },
  62.     {
  63.       id: 2,
  64.       title: 'SHIPMENTS',
  65.       dataJsonPath: 'totalShipmentCount',
  66.       width: 1,
  67.       align: 'center',
  68.     },
  69.     {
  70.       id: 3,
  71.       title: 'REVENUE',
  72.       dataJsonPath: 'revenue',
  73.       render: ({ revenue }) => `$${formatMoney(revenue)}`,
  74.       width: 3,
  75.     },
  76.     {
  77.       id: 4,
  78.       title: 'EXPENSE',
  79.       dataJsonPath: 'expense',
  80.       render: ({ expense }) => `$${formatMoney(expense)}`,
  81.       width: 3,
  82.     },
  83.     {
  84.       id: 5,
  85.       title: 'GROSS MARGIN',
  86.       dataJsonPath: 'grossMargin',
  87.       render: ({ grossMargin }) => `$${formatMoney(grossMargin)}`,
  88.       width: 3,
  89.     },
  90.     {
  91.       id: 6,
  92.       title: 'COMMISSION',
  93.       dataJsonPath: 'commissionPercentage',
  94.       render: ({ commissionPercentage }) => `${formatMoney(commissionPercentage)}%`,
  95.       width: 3,
  96.     },
  97.   ];
  98.  
  99.   handleRowRedirect = (row) => {
  100.     const { employeeId } = this.state;
  101.     const { accountId, commissionPeriodId } = row;
  102.     history.push(`/account/aggregation/${accountId}/${employeeId}/${commissionPeriodId}`);
  103.   };
  104.  
  105.   handleBackClick = () => {
  106.     history.goBack();
  107.   };
  108.  
  109.   render() {
  110.     const {
  111.       data, isLoading, name, totalPages,
  112.     } = this.state;
  113.     const firstDataValue = data[0];
  114.  
  115.     return (
  116.       <Grid>
  117.         <Grid.Row columns={1}>
  118.           <h1 className="aggregation">
  119.             {`PORTFOLIO INFO: ${name || ''}`}
  120.           </h1>
  121.         </Grid.Row>
  122.         <Grid.Row columns={1} centered>
  123.           <Table
  124.             data={data}
  125.             columns={this.getColumns()}
  126.             isLoading={isLoading}
  127.             renderLabel={() => (
  128.               <h2 className="aggregation">
  129.                 {
  130.                   firstDataValue
  131.                   && (
  132.                   <div>
  133.                     Delivered or paid from:
  134.                     <br />
  135.                     {`${formatDateTime(firstDataValue.periodDate)} to ${formatDateTime(firstDataValue.periodEndDate)}`}
  136.                   </div>
  137.                   )
  138.                 }
  139.               </h2>
  140.             )}
  141.             labelWidth={4}
  142.             onRowRedirect={row => this.handleRowRedirect(row)}
  143.             search
  144.             onFetchData={this.fetchData}
  145.             totalPages={totalPages}
  146.           />
  147.         </Grid.Row>
  148.         <Grid.Row columns={2} verticalAlign="bottom">
  149.           <Grid.Column width={1} floated="right" style={{ paddingRight: 0 }}>
  150.             <Button
  151.               size="big"
  152.               color="teal"
  153.               style={{ float: 'right', marginRight: 0 }}
  154.               onClick={this.handleBackClick}
  155.             >
  156.               Back
  157.             </Button>
  158.           </Grid.Column>
  159.         </Grid.Row>
  160.       </Grid>
  161.     );
  162.   }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement