Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import "../css/Global.css";
  2. import axios from "axios";
  3. import React, { useState } from "react";
  4.  
  5. const sortTypes = {
  6.   up: {
  7.     class: "sort-up",
  8.     fn: (a, b) => a.city.localeCompare(b.city)
  9.   },
  10.   down: {
  11.     class: "sort-down",
  12.     fn: (a, b) => b.city.localeCompare(a.city)
  13.   },
  14.   default: {
  15.     class: "sort",
  16.     fn: (a, b) => a
  17.   }
  18. };
  19.  
  20. const CompaniesTable = ({
  21.   companies,
  22.   loading,
  23.   currentPage,
  24.   companiesPerPage
  25. }) => {
  26.   const [currentState, setCurrentState] = useState("default");
  27.   const companiesIncome = [];
  28.  
  29.   const onSortChange = () => {
  30.     const currentSort = currentState;
  31.     let nextSort;
  32.  
  33.     if (currentSort === "default") nextSort = "up";
  34.     else if (currentSort === "up") nextSort = "down";
  35.     else if (currentSort === "down") nextSort = "default";
  36.  
  37.     setCurrentState(nextSort);
  38.   };
  39.  
  40.   const fetchIncomes = async id => {
  41.     const res = await axios.get(
  42.       `https://recruitment.hal.skygate.io/incomes/${id}`
  43.     );
  44.     companiesIncome.push(res.data);
  45.   };
  46.  
  47.   const calcTotalIncome = id => {
  48.     let totalIncome = 0;
  49.     return totalIncome;
  50.   };
  51.  
  52.   if (loading) {
  53.     return <h2>Loading...</h2>;
  54.   }
  55.   const currentSort = currentState;
  56.   const data = [...companies].sort(sortTypes[currentSort].fn);
  57.   const indexOfLastCompany = currentPage * companiesPerPage;
  58.   const indexOfFirstCompany = indexOfLastCompany - companiesPerPage;
  59.   const currentCompanies = data.slice(indexOfFirstCompany, indexOfLastCompany);
  60.  
  61.   return (
  62.     <div className="companies-table">
  63.       <h1 id="title">Companies Details</h1>
  64.       <table id="companies">
  65.         <tbody>
  66.           <tr>
  67.             <th>
  68.               ID
  69.               <button onClick={onSortChange}>
  70.                 <i className={`fas fa-${sortTypes[currentSort].class}`} />
  71.               </button>
  72.             </th>
  73.  
  74.             <th>Name</th>
  75.             <th>City</th>
  76.             <th>Total Income</th>
  77.             <th>Average Income</th>
  78.             <th>Last Month Income</th>
  79.           </tr>
  80.           {currentCompanies.map(
  81.             company => (
  82.               fetchIncomes(company.id),
  83.               (
  84.                 <tr key={companies.id}>
  85.                   <td>{company.id}</td>
  86.                   <td>{company.name}</td>
  87.                   <td>{company.city}</td>
  88.                   <td>{calcTotalIncome(company.id)}</td>
  89.                   <td>TEST</td>
  90.                   <td>TEST</td>
  91.                 </tr>
  92.               )
  93.             )
  94.           )}
  95.         </tbody>
  96.       </table>
  97.       <table id="companies"></table>
  98.     </div>
  99.   );
  100. };
  101.  
  102. export default CompaniesTable;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement