aboutsummaryrefslogtreecommitdiff
path: root/react-frontend/src/components/HubList.js
blob: 383d570be7c3dcb51ee1d3b2b180b2eabe9435c4 (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
63
64
// React and component imports
import { useEffect, useState } from "react";
import Hub from "./Hub.js";
import uuid from 'react-uuid';

// 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 [displayedItems, setDisplayedItems] = useState([]);

  /**
   * Method that determines whehter the Hub should be showed.
   * @returns {Boolean} True if to be shown, false if not.
   */
  const toInclude = holder => {
    // TODO: add number search or differentiate between it
    // TODO: add sus score range....
    if (!holder) {
      return false;
    };

    // const matchingId = holder.id.toString().includes(queryString.toLowerCase());
    //console.log(props.queryString.toLowerCase(), props.data.length);
    const matchingName = holder.name.toLowerCase().includes(props.queryString.toLowerCase());
    return matchingName;
  }

  const toHubElement = hub => 
    <Hub
      id={hub.id}
      name={hub.name}
      value={hub.suspicionScore}
      setSelectedId={props.setSelectedId}
      searching={props.searching}
    ></Hub>;

  /**
   * Filters the items to be shown, then created the iteams and sets the state with the items.
   */
  const filterItems = () => {
    if (!props.queryString) {
      // don't need to show all unless searching
      return setDisplayedItems(props.data.slice(0,600).map(hub => toHubElement(hub)))
    }

    const criteria = props.data.filter(holder => toInclude(holder));
    setDisplayedItems(criteria.map(hub => toHubElement(hub)));
  }

  /**
   * Hook to update the items on change of the search string or update of data.
   */
  useEffect(() => filterItems(), [props.queryString, props.data, props.searching]);

  return <ul className='Checkin-list'>{displayedItems}</ul>;
}

export default HubList;