aboutsummaryrefslogtreecommitdiff
path: root/react-frontend/src/components/HubSearch.js
blob: 827ea6d236818568efb062cd186c5513e76f3658 (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 InvestorInfo from "./InvestorInfo.js";

// CSS import
import '../css/UserCheckin.css';
import Hub from "./HubList.js";
import Search from "./HubSearch.js";

/**
 * Component that build the checkin list and displays checkin info.
 * @returns {import('react').HtmlHTMLAttributes} A div with the hubs 
 * in a vertical layout.
 */
function HubSearch(props) {
  const [queryString, setQueryString] = useState("", (s) => s.toLowerCase());
  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());
    const matchingName = holder.name.toLowerCase().includes(queryString);
    return matchingName;
  }

  /**
   * Filters the items to be shown, then created the iteams and sets the state with the items.
   */
  const filterItems = () => {
    console.log(queryString);
    const criteria = props.data.filter(holder => toInclude(holder));
    setDisplayedItems(criteria.map(hub => <p>{hub.name}</p>))
  }

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

  // TODO: maybe have a quick explanation of what search gives...
  // TODO: have number of ceos that make it...
  // TODO: weighted search or sort after search....
  // TODO: highlight part of string that matched...
  return (
    <div className="User-checkin">
      <div className="Checkins">
        <h2>Search</h2>
        <input type="text" onChange={(e) => setQueryString(e.target.value)}></input>
        <ul className='Checkin-list'>{displayedItems}</ul>;
      </div>
    </div>
  );
}

export default HubSearch;