|
1 |
| -import { useState, useEffect, useRef } from 'react'; |
2 |
| -import Profile from './components/Profile/Profile'; |
3 |
| -import ProfileSkeleton from './components/ProfileSkeleton/ProfileSkeleton'; |
4 |
| -import Search from './components/Search/Search'; |
5 |
| -import Sidebar from './components/Sidebar/Sidebar'; |
6 |
| -import ErrorPage from './components/ErrorPage/ErrorPage'; |
7 |
| -import NoResultFound from './components/NoResultFound/NoResultFound'; |
8 |
| -import Pagination from './components/Pagination/Pagination'; |
9 |
| -import './App.css'; |
10 |
| -import filenames from './ProfilesList.json'; |
11 |
| - |
| 1 | +import AboutUS from './components/Sidebar/AboutUs'; |
| 2 | +import Homepage from './Homepage'; |
| 3 | +import { BrowserRouter, Navigate, Route, Routes } from 'react-router-dom'; |
12 | 4 | function App() {
|
13 |
| - const profilesRef = useRef(); |
14 |
| - const [profiles, setProfiles] = useState([]); |
15 |
| - const [searching, setSearching] = useState(false); |
16 |
| - const [combinedData, setCombinedData] = useState([]); |
17 |
| - const [currentPage, setCurrentPage] = useState(1); |
18 |
| - const [shuffledProfiles, setShuffledProfiles] = useState([]); |
19 |
| - const [loadingProfiles, setLoadingProfiles] = useState(false); |
20 |
| - const recordsPerPage = 20; |
21 |
| - |
22 |
| - const currentUrl = window.location.pathname; |
23 |
| - useEffect(() => { |
24 |
| - const fetchData = async (file) => { |
25 |
| - try { |
26 |
| - const response = await fetch(file); |
27 |
| - const data = await response.json(); |
28 |
| - return data; |
29 |
| - } catch (error) { |
30 |
| - console.error('Error fetching data:', error); |
31 |
| - return []; |
32 |
| - } |
33 |
| - }; |
34 |
| - |
35 |
| - const combineData = async () => { |
36 |
| - setLoadingProfiles(true); |
37 |
| - try { |
38 |
| - const promises = filenames.map((file) => fetchData(`/data/${file}`)); |
39 |
| - const combinedData = await Promise.all(promises); |
40 |
| - setCombinedData(combinedData); |
41 |
| - setShuffledProfiles(shuffleProfiles(combinedData)); |
42 |
| - } catch (error) { |
43 |
| - console.error('Error combining data:', error); |
44 |
| - setCombinedData([]); |
45 |
| - setShuffledProfiles([]); |
46 |
| - } |
47 |
| - setLoadingProfiles(false); |
48 |
| - }; |
49 |
| - |
50 |
| - combineData(); |
51 |
| - }, []); |
52 |
| - |
53 |
| - const shuffleProfiles = (array) => { |
54 |
| - for (let i = array.length - 1; i > 0; i--) { |
55 |
| - const j = Math.floor(Math.random() * (i + 1)); |
56 |
| - [array[i], array[j]] = [array[j], array[i]]; |
57 |
| - } |
58 |
| - return array; |
59 |
| - }; |
60 |
| - |
61 |
| - const handleSearch = ({ value, criteria }) => { |
62 |
| - const normalizeString = (str) => |
63 |
| - str |
64 |
| - .toLowerCase() |
65 |
| - .replace(/\s*,\s*/g, ' ') |
66 |
| - .replace(/\s+/g, ' ') |
67 |
| - .trim(); |
68 |
| - |
69 |
| - const normalizedValue = normalizeString(value); |
70 |
| - |
71 |
| - const filteredResults = combinedData.filter((user) => { |
72 |
| - if (criteria === 'name') { |
73 |
| - return normalizeString(user.name).includes(normalizedValue); |
74 |
| - } else if (criteria === 'location') { |
75 |
| - return normalizeString(user.location).includes(normalizedValue); |
76 |
| - } else if (criteria === 'skill') { |
77 |
| - return user.skills.some((skill) => normalizeString(skill).includes(normalizedValue)); |
78 |
| - } |
79 |
| - return false; |
80 |
| - }); |
81 |
| - |
82 |
| - setProfiles(filteredResults); |
83 |
| - setSearching(true); |
84 |
| - }; |
85 |
| - |
86 |
| - const handleNextPage = () => { |
87 |
| - const totalPages = Math.ceil((searching ? profiles.length : combinedData.length) / recordsPerPage); |
88 |
| - if (currentPage < totalPages) { |
89 |
| - setCurrentPage(currentPage + 1); |
90 |
| - } |
91 |
| - }; |
92 |
| - |
93 |
| - const handlePrevPage = () => { |
94 |
| - if (currentPage > 1) { |
95 |
| - setCurrentPage(currentPage - 1); |
96 |
| - } |
97 |
| - }; |
98 |
| - |
99 |
| - useEffect(() => { |
100 |
| - profilesRef.current.scrollTo({ |
101 |
| - top: 0, |
102 |
| - behavior: 'smooth', |
103 |
| - }); |
104 |
| - }, [currentPage]); |
105 |
| - |
106 |
| - const getPaginatedData = () => { |
107 |
| - const data = searching ? profiles : shuffledProfiles; |
108 |
| - const startIndex = (currentPage - 1) * recordsPerPage; |
109 |
| - const endIndex = startIndex + recordsPerPage; |
110 |
| - return data.slice(startIndex, endIndex); |
111 |
| - }; |
112 |
| - |
113 |
| - const renderProfiles = () => { |
114 |
| - if (loadingProfiles) { |
115 |
| - return ( |
116 |
| - <> |
117 |
| - {Array(5) |
118 |
| - .fill('profile-skeleton') |
119 |
| - .map((item, index) => ( |
120 |
| - <ProfileSkeleton key={index} /> |
121 |
| - ))} |
122 |
| - </> |
123 |
| - ); |
124 |
| - } |
125 |
| - const paginatedData = getPaginatedData(); |
126 |
| - return paginatedData.map((currentRecord, index) => <Profile data={currentRecord} key={index} />); |
127 |
| - }; |
128 |
| - |
129 |
| - return currentUrl === '/' ? ( |
130 |
| - <div className="App flex flex-col bg-primaryColor dark:bg-secondaryColor md:flex-row"> |
131 |
| - <Sidebar /> |
132 |
| - <div className="w-full pl-5 pr-4 md:h-screen md:w-[77%] md:overflow-y-scroll md:py-7" ref={profilesRef}> |
133 |
| - <Search onSearch={handleSearch} /> |
134 |
| - {profiles.length === 0 && searching ? <NoResultFound /> : renderProfiles()} |
135 |
| - {combinedData.length > 0 && ( |
136 |
| - <Pagination |
137 |
| - currentPage={currentPage} |
138 |
| - totalPages={Math.ceil((searching ? profiles.length : shuffledProfiles.length) / recordsPerPage)} |
139 |
| - onNextPage={handleNextPage} |
140 |
| - onPrevPage={handlePrevPage} |
141 |
| - /> |
142 |
| - )} |
143 |
| - </div> |
144 |
| - </div> |
145 |
| - ) : ( |
146 |
| - <ErrorPage /> |
| 5 | + return ( |
| 6 | + <BrowserRouter> |
| 7 | + <Routes> |
| 8 | + <Route path="/" element={<Homepage />} /> |
| 9 | + <Route path="/about" element={<AboutUS />} /> |
| 10 | + </Routes> |
| 11 | + </BrowserRouter> |
147 | 12 | );
|
148 | 13 | }
|
149 |
| - |
150 | 14 | export default App;
|
0 commit comments