Advertisement
Guest User

topPriorityCustomer.js

a guest
Jun 27th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from "react";
  2.  
  3. // import library
  4. import "../css/custom.css";
  5. import { withRouter } from "react-router-dom";
  6. import apiRoutes from "../routes/api";
  7. import axios from "axios";
  8. import BootstrapTable from "react-bootstrap-table-next";
  9. import paginationFactory from "react-bootstrap-table2-paginator";
  10. import ToolkitProvider from "react-bootstrap-table2-toolkit";
  11. import { positions, Provider } from "react-alert";
  12. import AlertTemplate from "react-alert-template-basic";
  13.  
  14. // import components
  15. import Header from "../components/Header";
  16. import TopWidget from "../components/TopWidget";
  17. import Footer from "../components/Footer";
  18. import ButtonCopyToClipboard from "../components/ButtonCopyToClipboard";
  19. import AddPriorityCustomer from "../components/AddPriorityCustomer";
  20. import EditPriorityCustomer from "../components/EditPriorityCustomer";
  21.  
  22. class TopPriorityCustomer extends Component {
  23.   constructor(props) {
  24.     super(props);
  25.     this.state = {
  26.       dataCustomers: {},
  27.       priority_customers: [],
  28.       value: "",
  29.       copied: false,
  30.       customerDetail: {},
  31.       isOpen: false,
  32.       selectedFile: null
  33.     };
  34.   }
  35.  
  36.   componentDidMount() {
  37.     setTimeout(() => {
  38.       this.getUser();
  39.     }, 500);
  40.   }
  41.  
  42.   getUser = async () => {
  43.     let userData = JSON.parse(localStorage.getItem("userData"));
  44.     this.getTopPriorityCustomer(userData.id_sto);
  45.   };
  46.  
  47.   getTopPriorityCustomer = id_sto => {
  48.     axios.get(apiRoutes + "dashboard_priority/" + id_sto).then(
  49.       res => {
  50.         this.setState({
  51.           dataCustomers: res.data,
  52.           priority_customers: res.data.priority_customers_with_pagination
  53.         });
  54.         console.log(this.state.dataCustomers);
  55.       },
  56.       error => {
  57.         let status = error.response.status;
  58.         console.log(status);
  59.       }
  60.     );
  61.   };
  62.  
  63.   createTopPriorityCustomer = Customer => {
  64.     let data = {
  65.       id_sto: this.state.dataCustomers.id_sto,
  66.       no_internet: parseFloat(Customer.no_internet),
  67.       no_referensi: parseFloat(Customer.no_referensi),
  68.       nama: Customer.nama,
  69.       alamat: Customer.alamat,
  70.       no_hp: parseFloat(Customer.no_hp)
  71.     };
  72.  
  73.     axios.post(apiRoutes + `create_customer_priority`, data).then(
  74.       res => {
  75.         let { priority_customers } = this.state;
  76.         priority_customers.push(res.data);
  77.         this.setState({
  78.           priority_customers
  79.         });
  80.         console.log(this.state.priority_customers);
  81.       },
  82.       error => {
  83.         let status = error.response.status;
  84.         console.log(status);
  85.       }
  86.     );
  87.     setTimeout(() => {
  88.       window.location.reload();
  89.     }, 300);
  90.   };
  91.  
  92.   updateTopPriorityCustomer = data => {
  93.     axios.put(apiRoutes + `edit_customer_priority/` + data.id, data).then(
  94.       res => {  
  95.         let { priority_customers } = this.state;
  96.         priority_customers.push(res.data);
  97.         this.setState({
  98.           priority_customers
  99.         });
  100.       },
  101.       error => {
  102.         let status = error.response.status;
  103.         console.log(status);
  104.       }
  105.     );
  106.   };
  107.  
  108.   handleChange = e => {
  109.     let files = e.target.files[0];
  110.     this.setState({
  111.       selectedFile: files
  112.     });
  113.   };
  114.  
  115.   handleUpload = (e) => {
  116.     let file = this.state.selectedFile;
  117.     let formData = new FormData();
  118.     formData.append('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',file);
  119.  
  120.     const config = { headers: {'Content-Type': 'multipart/form-data' }}
  121.     axios.post(apiRoutes + `import_excel`, formData, config).then(
  122.       res => {
  123.         console.log(res.data);
  124.       },
  125.       error => {
  126.         let status = error.response.status;
  127.         console.log(status);
  128.       }
  129.     );
  130.   };
  131.  
  132.   render() {
  133.     function statusFormatter(cell, row, rowIndex, formatExtraData) {
  134.       let status = "";
  135.       if (cell === "LOS") {
  136.         status = "LOS";
  137.       } else if (cell === "ONLINE") {
  138.         status = "Online";
  139.       } else {
  140.         status = "Undefined";
  141.         return (
  142.           <div
  143.             style={{
  144.               marginTop: -5,
  145.               height: 27,
  146.               width: 70,
  147.               borderRadius: 10.5,
  148.               backgroundColor: "orange",
  149.               fontSize: "11px",
  150.               color: "white",
  151.               justifyContent: "center",
  152.               alignItems: "center",
  153.               display: "flex"
  154.             }}
  155.           >
  156.             {status}
  157.           </div>
  158.         );
  159.       }
  160.       return <span className={formatExtraData[cell]}>{status}</span>;
  161.     }
  162.  
  163.     function firstColumnData(cell, row, rowIndex) {
  164.       return <span>{rowIndex + 1}</span>;
  165.     }
  166.  
  167.     function actionData(cell, row, rowIndex) {
  168.       return (
  169.         <span>
  170.           <a href="/top_priority_customer">
  171.             <i className="fas fa-phone-square icon-phone" />
  172.           </a>{" "}
  173.           |{" "}
  174.           <a href="/top_priority_customer">
  175.             <i className="fas fa-cog icon-gear" />
  176.           </a>{" "}
  177.         </span>
  178.       );
  179.     }
  180.  
  181.     const columns_top_priority_customers = [
  182.       {
  183.         dataField: "no",
  184.         text: "NO",
  185.         formatter: firstColumnData
  186.       },
  187.       {
  188.         dataField: "no_internet",
  189.         text: "NO.INET"
  190.       },
  191.       {
  192.         dataField: "nama",
  193.         text: "NAMA"
  194.       },
  195.       {
  196.         dataField: "alamat",
  197.         text: "ALAMAT"
  198.       },
  199.       {
  200.         dataField: "no_hp",
  201.         text: "NO.HP"
  202.       },
  203.       {
  204.         dataField: "status",
  205.         text: "STATUS",
  206.         formatter: statusFormatter,
  207.         formatExtraData: {
  208.           LOS: "isLOS paddingSize",
  209.           ONLINE: "isOnline paddingSize",
  210.           "-UNDEFINED": "isLOS paddingSize"
  211.         }
  212.       },
  213.       {
  214.         dataField: "action",
  215.         text: "Action",
  216.         formatter: actionData
  217.       }
  218.     ];
  219.  
  220.     let no_inet = "";
  221.     this.state.priority_customers.map(
  222.       (item, key) => (no_inet += item.format_no_internet + "\n")
  223.     );
  224.  
  225.     const options = {
  226.       timeout: 5000,
  227.       position: positions.BOTTOM_CENTER
  228.     };
  229.  
  230.     const rowEvents = {
  231.       onClick: (e, row, rowIndex) => {
  232.         this.setState({
  233.           customerDetail: row
  234.         });
  235.       }
  236.     };
  237.  
  238.     // console.log('test',this.state.customerDetail);
  239.     return (
  240.       // <TopPriorityCustomersProvider value={this.state.dataCustomers}>
  241.       <Provider template={AlertTemplate} {...options}>
  242.         <div>
  243.           <Header />
  244.           <div className="content-wrapper">
  245.             <TopWidget
  246.               totalPelangganPrioritas={
  247.                 this.state.dataCustomers.priority_customers_count
  248.               }
  249.               totalPelangganPrioritasOffline={
  250.                 this.state.dataCustomers.offline_priority_pustomers_count
  251.               }
  252.               totalTeknisi={this.state.dataCustomers.technician_count}
  253.             />
  254.             <section className="content">
  255.               <div className="row">
  256.                 <div className="col">
  257.                   <div className="row">
  258.                     <div className="col">
  259.                       <ButtonCopyToClipboard no_inet={no_inet} />
  260.                       <h5 style={{ marginTop: "15px", marginBottom: "-5px" }}>
  261.                         {this.state.dataCustomers.last_updated_at
  262.                           ? `Last Updated ${
  263.                               this.state.dataCustomers.last_updated_at
  264.                             }`
  265.                           : null}
  266.                       </h5>
  267.                     </div>
  268.                     <div className="col">
  269.                       <div
  270.                         className="btn-group"
  271.                         role="group"
  272.                         aria-label="Basic example"
  273.                       >
  274.                         <AddPriorityCustomer
  275.                           createTopPriorityCustomer={
  276.                             this.createTopPriorityCustomer
  277.                           }
  278.                           isOpen={this.state.isOpen}
  279.                           id_sto={this.state.dataCustomers.id_sto}
  280.                         />
  281.                         <div className="input-group">
  282.                           <div className="custom-file">
  283.                             <input
  284.                               type="file"
  285.                               className="custom-file-input"
  286.                               id="inputGroupFile04"
  287.                               aria-describedby="inputGroupFileAddon04"
  288.                               onChange={this.handleChange}
  289.                               multiple
  290.                             />
  291.                             <label
  292.                               className="custom-file-label"
  293.                               htmlFor="inputGroupFile04"
  294.                             >
  295.                               Choose file
  296.                             </label>
  297.                           </div>
  298.                           <div className="input-group-append">
  299.                             <button
  300.                               className="btn btn-outline-secondary"
  301.                               type="button"
  302.                               id="inputGroupFileAddon04"
  303.                               onClick={this.handleUpload}
  304.                             >
  305.                               Import Status
  306.                             </button>
  307.                           </div>
  308.                         </div>
  309.                       </div>
  310.                     </div>
  311.                   </div>
  312.                   <div className="row" style={{ marginTop: "20px" }}>
  313.                     <div className="col dashboard">
  314.                       {this.state && this.state.priority_customers.length > 0 && (
  315.                         <div
  316.                           className="panel panel-default section-table-one"
  317.                           id="border-red"
  318.                         >
  319.                           {/* Default panel contents */}
  320.                           <div
  321.                             className="panel-body wrapper-heading-one"
  322.                             id="border-bottom-red"
  323.                           >
  324.                             <div className="row">
  325.                               <div className="col">
  326.                                 <div className="wrapper-box-red">
  327.                                   <div className="box-red" />
  328.                                 </div>
  329.                               </div>
  330.                               <div className="col-md-11 wrapper-title">
  331.                                 <h5>TOP PRIORITY CUSTOMER</h5>
  332.                               </div>
  333.                             </div>
  334.                           </div>
  335.                           <div className="panel-body">
  336.                             <div className="row">
  337.                               <div className="col">
  338.                                 <h5>
  339.                                   Total pelanggan :{" "}
  340.                                   <b>
  341.                                     {
  342.                                       this.state.dataCustomers
  343.                                         .priority_customers_count
  344.                                     }
  345.                                   </b>
  346.                                 </h5>
  347.                               </div>
  348.                               <div className="col">
  349.                                 <h5>
  350.                                   Pelanggan gangguan:{" "}
  351.                                   <b className="red-count">
  352.                                     {
  353.                                       this.state.dataCustomers
  354.                                         .offline_priority_pustomers_count
  355.                                     }
  356.                                   </b>
  357.                                 </h5>
  358.                               </div>
  359.                             </div>
  360.                           </div>
  361.                           <ToolkitProvider
  362.                             keyField="no"
  363.                             data={this.state.priority_customers}
  364.                             columns={columns_top_priority_customers}
  365.                           >
  366.                             {props => (
  367.                               <div>
  368.                                 <br />
  369.                                 <BootstrapTable
  370.                                   hover
  371.                                   id={
  372.                                     this.state.dataCustomers
  373.                                       .offline_priority_pustomers_count > 0
  374.                                       ? "display-table"
  375.                                       : "display-none"
  376.                                   }
  377.                                   rowEvents={rowEvents}
  378.                                   pagination={paginationFactory(options)}
  379.                                   {...props.baseProps}
  380.                                 />
  381.                               </div>
  382.                             )}
  383.                           </ToolkitProvider>
  384.                         </div>
  385.                       )}
  386.                     </div>
  387.                   </div>
  388.                 </div>
  389.               </div>
  390.             </section>
  391.           </div>
  392.           <Footer />
  393.         </div>
  394.       </Provider>
  395.       // </TopPriorityCustomersProvider>
  396.     );
  397.   }
  398. }
  399.  
  400. export default withRouter(TopPriorityCustomer);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement