This repository has been archived by the owner on Sep 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
App.js
103 lines (83 loc) · 2.49 KB
/
App.js
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import React from "react";
import "./App.css";
import { ThemeProvider } from '@material-ui/core/styles';
import theme from './theme';
import HeroSection from "./components/HeroSection";
import JobBoard from "./components/JobBoard";
import Footer from "./components/Footer";
// Jobs API URL - relative path.
const JOB_API_URL = "/api/jobs";
// call API jobs.
async function fetchJobs(updateCb) {
const res = await fetch(JOB_API_URL);
const json = await res.json();
// Update state function used as callback
// so that jobsList can be updated.
updateCb(json);
}
function App() {
// Use the State Hook to declare the jobsList state as an
// empty array. The update state function is called on
// the fetched github jobs JSON and then passed to the
// Jobs component to display on the page.
const [jobsList, updateJobs] = React.useState([]);
// Use State Hook to declare filtered jobs list,
// that takes initial value as og jobslist but upates
// as user searches.
const [filteredList, updateFiltered] = React.useState(jobsList);
React.useEffect(() => {
fetchJobs(updateJobs);
fetchJobs(updateFiltered);
//updateFiltered(jobsList);
}, []);
// Search Component functions
// OnChange
const handleSearchChange = (e) => {
// Store the current list of jobs.
let currentJobs = jobsList;
// Create variable to store filtered jobs.
let filteredJobs = [];
// Store input
let input = e.target.value;
// If search is not empty, begin search.
if (input !== "") {
currentJobs = jobsList;
// Use the filter function to determine which jobs to show
// based on the search.
filteredJobs = currentJobs.filter((job) => {
// Change all relevant job.values to lowercase.
let title = job.title.toLowerCase();
let company = job.company.toLowerCase();
let description = job.description.toLowerCase();
let location = job.location.toLowerCase();
// Store input as lowercase.
let inpLC = input.toLowerCase();
if (
title.includes(inpLC) ||
company.includes(inpLC) ||
description.includes(inpLC) ||
location.includes(inpLC)
) {
return true;
}
else {
return false;
}
});
} else {
filteredJobs = currentJobs;
}
console.log(filteredJobs);
updateFiltered(filteredJobs);
};
return (
<ThemeProvider theme={theme}>
<div className="App">
<HeroSection jobs={filteredList} searchOnChange={handleSearchChange} />
<JobBoard jobs={filteredList} />
<Footer />
</div>
</ThemeProvider>
);
}
export default App;