Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Provider
  2. export const ExpensesContext = React.createContext();
  3.  
  4. export class ExpensesPage extends Component {
  5.     constructor() {
  6.         super();
  7.  
  8.         this.auth = new AuthenticationService();
  9.         this.expensesService = new ExpensesService(this.auth);
  10.         this.formatter = new CommonFormatter();
  11.  
  12.         this.state = {
  13.             expenses: null,
  14.             isLoading: true
  15.         };
  16.     }
  17.  
  18.     componentDidMount() {
  19.         this.fetchExpenses();
  20.     }
  21.  
  22.     componentDidUpdate(prevProps, prevState) {
  23.         if (prevState.isLoading !== this.state.isLoading) {
  24.             this.fetchExpenses();
  25.         }
  26.     }
  27.  
  28.     handleButtonClick = (e) => {
  29.         let selectedAction = e.target.innerHTML.replace(/<(?:.|\n)*?>/gm, '').trim();
  30.         let selectedExpenseId = e.target.value;
  31.  
  32.         let selectedExpense = this.state.expenses.find(e => e.expenseId === parseInt(selectedExpenseId));
  33.  
  34.         switch (selectedAction) {
  35.             case "Delete":
  36.                 this.deleteExpense(selectedExpense);
  37.                 break;
  38.         }
  39.  
  40.     }
  41.  
  42.     reload = () => {
  43.         this.setState({
  44.             isLoading: true
  45.         });
  46.     }
  47.  
  48.     fetchExpenses = () => {
  49.         this.expensesService.getExpenses()
  50.             .then(response => {
  51.                 this.setState({
  52.                     expenses: response,
  53.                     isLoading: false
  54.                 });
  55.                 console.log(this.state);
  56.             });
  57.     }
  58.  
  59.     deleteExpense = (expense) => {
  60.         confirmAlert({
  61.             title: 'Confirm expense delete',
  62.             message: 'Are you sure to expense from ' + expense.date + '?',
  63.             buttons: [
  64.                 {
  65.                     label: 'Yes',
  66.                     onClick: () => {
  67.                         this.expensesService.deleteExpense(expense.expenseId).then(() => {
  68.                             this.reload();
  69.                         });
  70.                         toastr.success("Expense deleted!");
  71.                     }
  72.                 },
  73.                 {
  74.                     label: 'No',
  75.                 }
  76.             ]
  77.         });
  78.     }
  79.  
  80.     render() {
  81.         if (this.state.isLoading) {
  82.             return <p>Loading...</p>;
  83.         }
  84.  
  85.         return (
  86.             <div>
  87.                 <h2>Expenses</h2>
  88.  
  89.                 <ExpensesContext.Provider value={{
  90.                     expenses: this.state.expenses,
  91.                     handleButtonClick: this.handleButtonClick,
  92.                 }}
  93.                 >
  94.                     <ExpensesDetails />
  95.                 </ExpensesContext.Provider>
  96.  
  97.             </div>
  98.         );
  99.     }
  100. }
  101.  
  102. // Consumer
  103. export class ExpensesDetails extends Component {
  104.     constructor(props) {
  105.         super(props);
  106.  
  107.         this.formatter = new CommonFormatter();
  108.     }
  109.  
  110.     render() {
  111.         const columns = [
  112.             {
  113.                 Header: 'Date',
  114.                 accessor: 'date',
  115.                 minWidth: 50,
  116.                 Cell: props => this.formatter.formatDate(props.value)
  117.             },
  118.             {
  119.                 Header: 'Type',
  120.                 accessor: 'type',
  121.                 minWidth: 50,
  122.                 Cell: props => this.formatter.formatExpenseType(props.value)
  123.             },
  124.             {
  125.                 Header: 'Count',
  126.                 accessor: 'count',
  127.                 minWidth: 20
  128.             },
  129.             {
  130.                 Header: 'Price',
  131.                 accessor: 'price',
  132.                 minWidth: 40
  133.             },
  134.             {
  135.                 Header: 'Details',
  136.                 accessor: 'details',
  137.                 Cell: props => this.formatter.formatEmptyOrNull(props.value),
  138.                 minWidth: 120
  139.             },
  140.             {
  141.                 Header: 'Odometer',
  142.                 accessor: 'mileage',
  143.                 Cell: props => this.formatter.formatEmptyOrNull(props.value) + " km",
  144.                 minWidth: 80
  145.             },
  146.             {
  147.                 Header: '',
  148.                 accessor: 'expenseId',
  149.                 Cell: props => <Button onClick={this.context.handleButtonClick} bsStyle="warning" bsSize="xsmall" value={props.value}>Edit</Button>,
  150.                 sortable: false,
  151.                 resizable: false,
  152.                 minWidth: 30
  153.             },
  154.             {
  155.                 Header: '',
  156.                 accessor: 'expenseId',
  157.                 Cell: props => <Button onClick={this.context.handleButtonClick} bsStyle="danger" bsSize="xsmall" value={props.value}>Delete</Button>,
  158.                 sortable: false,
  159.                 resizable: false,
  160.                 minWidth: 30
  161.             }
  162.         ];
  163.  
  164.         return (
  165.             <ReactTable data={this.context.expenses} columns={columns} defaultPageSize={10} defaultSorted={[{ id: "date", desc: true }]} />
  166.         );
  167.     }
  168. }
  169. ExpensesDetails.contextType = ExpensesContext;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement