forked from shyamtawli/devFind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
129 lines (114 loc) · 3.86 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { useState, useEffect } from 'react';
import Profile from './components/Profile/Profile';
import Search from './components/Search/Search';
import Sidebar from './components/Sidebar/Sidebar';
import ErrorPage from './components/ErrorPage/ErrorPage';
import NoResultFound from './components/NoResultFound/NoResultFound';
import Pagination from './components/Pagination/Pagination';
import './App.css';
import './components/Pagination/Pagination.css';
import filenames from './ProfilesList.json';
function App() {
const [profiles, setProfiles] = useState([]);
const [searching, setSearching] = useState(false);
const [combinedData, setCombinedData] = useState([]);
const [currentPage, setCurrentPage] = useState(1);
const [shuffledProfiles, setShuffledProfiles] = useState([]);
const recordsPerPage = 20;
const currentUrl = window.location.pathname;
useEffect(() => {
const fetchData = async (file) => {
try {
const response = await fetch(file);
const data = await response.json();
return data;
} catch (error) {
console.error('Error fetching data:', error);
return [];
}
};
const combineData = async () => {
try {
const promises = filenames.map((file) => fetchData(`/data/${file}`));
const combinedData = await Promise.all(promises);
setCombinedData(combinedData);
setShuffledProfiles(shuffleProfiles(combinedData));
} catch (error) {
console.error('Error combining data:', error);
setCombinedData([]);
setShuffledProfiles([]);
}
};
combineData();
}, []);
const shuffleProfiles = (array) => {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
};
const handleSearch = (searchValue) => {
const lowercaseSearch = searchValue.toLowerCase();
const results = combinedData.filter((object) => {
const lowercaseName = object.name.toLowerCase();
const lowercaseLocation = object.location.toLowerCase();
const matchingSkills = object.skills.filter((skill) => skill.toLowerCase().includes(lowercaseSearch));
return (
matchingSkills.length > 0 ||
lowercaseName.includes(lowercaseSearch) ||
lowercaseLocation.includes(lowercaseSearch)
);
});
setSearching(true);
setProfiles(results);
setCurrentPage(1);
};
const handleNextPage = () => {
const totalPages = Math.ceil((searching ? profiles.length : combinedData.length) / recordsPerPage);
if (currentPage < totalPages) {
setCurrentPage(currentPage + 1);
}
};
const handlePrevPage = () => {
if (currentPage > 1) {
setCurrentPage(currentPage - 1);
}
};
useEffect(() => {
window.scrollTo({
top: 0,
behavior: 'smooth',
});
}, [currentPage]);
const getPaginatedData = () => {
const data = searching ? profiles : shuffledProfiles;
const startIndex = (currentPage - 1) * recordsPerPage;
const endIndex = startIndex + recordsPerPage;
return data.slice(startIndex, endIndex);
};
const renderProfiles = () => {
const paginatedData = getPaginatedData();
return paginatedData.map((currentRecord, index) => <Profile data={currentRecord} key={index} />);
};
return (
<div className="App">
<Sidebar />
<Search onSearch={handleSearch} />
{currentUrl === '/' ? (
<>
{profiles.length === 0 && searching ? <NoResultFound /> : renderProfiles()}
<Pagination
currentPage={currentPage}
totalPages={Math.ceil((searching ? profiles.length : shuffledProfiles.length) / recordsPerPage)}
onNextPage={handleNextPage}
onPrevPage={handlePrevPage}
/>
</>
) : (
<ErrorPage />
)}
</div>
);
}
export default App;