-
-
Notifications
You must be signed in to change notification settings - Fork 477
Expand file tree
/
Copy pathHomepage.jsx
More file actions
171 lines (153 loc) · 5.31 KB
/
Homepage.jsx
File metadata and controls
171 lines (153 loc) · 5.31 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { useState, useEffect, useRef } from 'react';
import Profile from './components/Profile/Profile';
import ProfileSkeleton from './components/ProfileSkeleton/ProfileSkeleton';
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 filenames from './ProfilesList.json';
// import GTranslateLoader from './components/GTranslateLoader';
function App() {
const profilesRef = useRef();
const [profiles, setProfiles] = useState([]);
const [searching, setSearching] = useState(false);
const [combinedData, setCombinedData] = useState([]);
const [currentPage, setCurrentPage] = useState(1);
const [shuffledProfiles, setShuffledProfiles] = useState([]);
const [loadingProfiles, setLoadingProfiles] = useState(false);
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 () => {
setLoadingProfiles(true);
try {
const promises = filenames.map((file, index) =>
fetchData(`/data/${file}`).then((data) => ({ ...data, id: index + 1, fileName: file.replace('.json', '') })),
);
const combinedData = await Promise.all(promises);
const flattenedData = combinedData.flat();
setCombinedData(flattenedData);
setShuffledProfiles(shuffleProfiles(flattenedData));
} catch (error) {
console.error('Error combining data:', error);
setCombinedData([]);
setShuffledProfiles([]);
}
setLoadingProfiles(false);
};
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 normalizeString = (str) => {
if (!str) return ''; // Return an empty string if str is undefined or null
return str
.toLowerCase()
.replace(/\s*,\s*/g, ' ')
.replace(/\s+/g, ' ')
.trim();
};
const handleSearch = ({ value, criteria }) => {
const normalizedValue = normalizeString(value);
if (criteria !== 'skill') {
const filteredResults = combinedData.filter((user) => {
if (criteria === 'name') {
return normalizeString(user.name).includes(normalizedValue);
} else if (criteria === 'location') {
return normalizeString(user.location).includes(normalizedValue);
}
return false;
});
setProfiles(filteredResults);
} else if (criteria === 'skill') {
if (value.length > 0) {
const setOfSearchSkills = new Set(value.map((skill) => skill.toLowerCase()));
const filteredUsers = shuffledProfiles.filter((user) =>
user.skills.some((skill) => setOfSearchSkills.has(skill.toLowerCase())),
);
setProfiles(filteredUsers);
} else {
setProfiles(shuffledProfiles);
}
} else {
setProfiles([]);
}
setSearching(true);
};
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(() => {
profilesRef.current.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 = () => {
if (loadingProfiles) {
return (
<>
{Array(5)
.fill('profile-skeleton')
.map((item, index) => (
<ProfileSkeleton key={index} />
))}
</>
);
}
const paginatedData = getPaginatedData();
return paginatedData.map((currentRecord, index) => <Profile data={currentRecord} key={index} />);
};
return currentUrl === '/' ? (
<div className="App flex flex-col bg-primaryColor dark:bg-secondaryColor md:flex-row">
<Sidebar />
<div className="w-full pl-5 pr-4 md:h-screen md:w-[77%] md:overflow-y-scroll md:py-7" ref={profilesRef}>
<Search onSearch={handleSearch} />
{combinedData.length > 0 && (
<Pagination
currentPage={currentPage}
totalPages={Math.ceil((searching ? profiles.length : shuffledProfiles.length) / recordsPerPage)}
onNextPage={handleNextPage}
onPrevPage={handlePrevPage}
/>
)}
{profiles.length === 0 && searching ? <NoResultFound /> : renderProfiles()}
</div>
{/* <GTranslateLoader /> */}
</div>
) : (
<ErrorPage />
);
}
export default App;