Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import "../css/Global.css";
  2. import React, { useState } from "react";
  3. import _ from "lodash";
  4.  
  5. const CompaniesTable = ({
  6.   companies,
  7.   loading,
  8.   currentPage,
  9.   companiesPerPage,
  10.   totalIncomeArr,
  11.   averageIncomeArr
  12. }) => {
  13.  
  14.   const [currentSort, setCurrentSort] = useState("default");
  15.  
  16.   const  companyData = _.merge(companies, totalIncomeArr, averageIncomeArr);
  17.  
  18.   if (loading) {
  19.     return (
  20.       <div>
  21.         <div className="ui active inverted dimmer">
  22.           <div className="ui text loader">Loading</div>
  23.         </div>
  24.         <p></p>
  25.       </div>
  26.     );
  27.   }
  28.  
  29.   const indexOfLastCompany = currentPage * companiesPerPage;
  30.   const indexOfFirstCompany = indexOfLastCompany - companiesPerPage;
  31.  
  32.   let currentCompanies = companyData.slice(
  33.     indexOfFirstCompany,
  34.     indexOfLastCompany
  35.   );
  36.  
  37.   const onSort = sortKey => {
  38.     let data = companyData;
  39.     let nextSort;
  40.  
  41.     if (currentSort === "default") {
  42.       data = companyData.sort((a, b) =>
  43.         typeof a[sortKey] === "string"
  44.           ? a[sortKey].localeCompare(b[sortKey])
  45.           : a[sortKey] - b[sortKey]
  46.       );
  47.       nextSort = "down";
  48.     }
  49.     if (currentSort === "down") {
  50.       data = companyData.sort((a, b) =>
  51.         typeof a[sortKey] === "string"
  52.           ? b[sortKey].localeCompare(a[sortKey])
  53.           : parseInt(b[sortKey]) - parseInt(a[sortKey])
  54.       );
  55.       nextSort = "up";
  56.     }
  57.     if (currentSort === "up") {
  58.       data = companyData.sort((a, b) =>
  59.         typeof a[sortKey] === "string"
  60.           ? a[sortKey].localeCompare(b[sortKey])
  61.           : a[sortKey] - b[sortKey]
  62.       );
  63.       nextSort = "down";
  64.     }
  65.     setCurrentSort(nextSort);
  66.   };
  67.  
  68.   const fieldOrder = [
  69.     { id: "id", text: "ID" },
  70.     { id: "name", text: "Name" },
  71.     { id: "city", text: "City" },
  72.     { id: "total_income", text: "Total Income" },
  73.     { id: "average_income", text: "Average Income" },
  74.     { id: "last_month_income", text: "Last Month Income" }
  75.   ];
  76.  
  77.   const companyRows = () => {
  78.     console.log(currentCompanies)
  79.     return currentCompanies.map(({ totalInc, averageInc, id, name, city }) => (
  80.       <tr key={id}>
  81.         <td>
  82.           <b>{id}</b>
  83.         </td>
  84.         <td>
  85.           <b>{name}</b>
  86.         </td>
  87.         <td>
  88.           <b>{city}</b>
  89.         </td>
  90.         <td>
  91.           <b>{totalInc}</b>
  92.           <i className="dollar sign icon"></i>
  93.         </td>
  94.         <td>
  95.           <b>{averageInc}</b>
  96.           <i className="dollar sign icon"></i>
  97.         </td>
  98.         <td>
  99.           <b>0</b>
  100.           <i className="dollar sign icon"></i>
  101.         </td>
  102.       </tr>
  103.     ));
  104.   };
  105.  
  106.   return (
  107.     <div>
  108.       <div className="companies-table">
  109.         <table id="companies">
  110.           <thead>
  111.             <tr>
  112.               {fieldOrder.map(field => {
  113.                 return (
  114.                   <th key={field.id}>
  115.                     {field.text}
  116.                     <i
  117.                       key={field.id}
  118.                       onClick={() => onSort(field.id)}
  119.                       className={`sort icon`}
  120.                     />
  121.                   </th>
  122.                 );
  123.               })}
  124.             </tr>
  125.           </thead>
  126.           <tbody>{companyRows()}</tbody>
  127.         </table>
  128.       </div>
  129.     </div>
  130.   );
  131. };
  132.  
  133. export default CompaniesTable;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement