diff --git a/src/App.tsx b/src/App.tsx index adcb8594e..82367bb9d 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,20 +1,23 @@ -import { PeoplePage } from './components/PeoplePage'; -import { Navbar } from './components/Navbar'; +import React from 'react'; +import { Routes, Route, Navigate } from 'react-router-dom'; +import { HomePage } from './HomePage/HomePage'; +import { PeoplePage } from './PeoplePage/PeoplePage'; +import { NotFoundPage } from './NotFoundPage/NotFoundPage'; +import { Navigation } from './Navigation/Navigation'; -import './App.scss'; - -export const App = () => { +export const App: React.FC = () => { return (
+ Something went wrong +
+ ); + } + + const query = searchParams.get('query') || ''; + const sex = searchParams.get('sex') || ''; + const centuries = searchParams.getAll('century') || []; + + let filteredPeople = people.filter(person => { + const matchesName = (name: string) => + name.toLowerCase().includes(query.toLowerCase()); + const century = Math.floor((person.born + 99) / 100); + + return ( + (query === '' || + matchesName(person.name) || + matchesName(person.motherName || '') || + matchesName(person.fatherName || '')) && + (sex === '' || person.sex === sex) && + (centuries.length === 0 || centuries.includes(century.toString())) + ); + }); + + if (sortField) { + filteredPeople = [...filteredPeople].sort((a, b) => { + const aValue = a[sortField as keyof Person]; + const bValue = b[sortField as keyof Person]; + + if (typeof aValue === 'string' && typeof bValue === 'string') { + return sortOrder === 'asc' + ? aValue.localeCompare(bValue) + : bValue.localeCompare(aValue); + } + + if (typeof aValue === 'number' && typeof bValue === 'number') { + return sortOrder === 'asc' ? aValue - bValue : bValue - aValue; + } + + return 0; + }); + } + + const handleSort = (field: keyof Person) => { + const newSortOrder = + sortField === field && sortOrder === 'asc' ? 'desc' : 'asc'; + + setSortField(field); + setSortOrder(newSortOrder); + + setSearchParams({ + ...Object.fromEntries(searchParams.entries()), + sortField: field, + sortOrder: newSortOrder, + }); + }; + + return ( + <> ++ There are no people on the server +
+ ) : ( + <> + {filteredPeople.length === 0 ? ( ++ There are no people matching the current search + criteria +
+ ) : ( +onSort('name')}>Name {getSortIcon('name')} | +onSort('sex')}>Sex {getSortIcon('sex')} | +onSort('born')}>Born {getSortIcon('born')} | +onSort('died')}>Died {getSortIcon('died')} | +Mother | +Father | +
---|---|---|---|---|---|
+ + {person.name} + + | +{person.sex} | +{person.born} | +{person.died} | + ++ {motherSlug ? ( + + {person.motherName} + + ) : ( + {person.motherName || '-'} + )} + | + ++ {fatherSlug ? ( + + {person.fatherName} + + ) : ( + {person.fatherName || '-'} + )} + | +