aboutsummaryrefslogtreecommitdiff
path: root/react-frontend/src/components/HubList.js
blob: f5b1414c5ba035b1a252f0a83ac0c51ce52d02ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// React and component imports
import { useEffect, useState } from "react";
import Hub from "./Hub.js";
import InvestorInfo from "./InvestorInfo.js";

// CSS import
import '../css/UserCheckin.css';

/**
 * Component that build the checkin list and displays checkin info.
 * @returns {import('react').HtmlHTMLAttributes} A div with the hubs 
 * in a vertical layout.
 */
function HubList(props) {
  const [hubItems, setHubItems] = useState([]);
  const [isSelected, setIsSelected] = useState(false);
  const [name, setName] = useState('');
  
  /**
   * Loads new the checkins into the current cache/map of hubs.
   */
  const updateHubItems = () => {
    // sort and create the elemnts
    let hubs = [];
    const sorted = props.data.sort((a, b) => b.suspicionScore - a.suspicionScore);
    sorted.forEach(hub => hubs.push(
      <Hub key={hub.id} id={hub.id} name={hub.name} value={hub.suspicionScore} setSelected={props.setSelected}></Hub>
    ));

    setHubItems(hubs);
  }

  const getName = () => {
    props.data.forEach(hub => {
      if (hub.id == props.selected) {
        setName(hub.name);
      }
    })
    setName('');
  }

  // React hook that updates when the hubs are recalculated
  useEffect(() => updateHubItems(), [props.data]);

  //React hook to show data for an investor
  useEffect(() => {
    setIsSelected(true)
    getName();
  }, [props.selected]);

  return (
    <div className="User-checkin">
        <div className="Checkins">
            <h2>Suspicion Ranks</h2>
            <ul className='Checkin-list'>{hubItems}</ul>
        </div>
        <InvestorInfo personId={props.selected} isSelected={isSelected} name={name} dates={props.dates}></InvestorInfo>
    </div>
  );
}

export default HubList;