Skip to content
Open

haha #1836

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Routes, Route, Navigate } from 'react-router-dom';
import { PeoplePage } from './components/PeoplePage';
import { Navbar } from './components/Navbar';

Expand All @@ -10,9 +11,19 @@ export const App = () => {

<div className="section">
<div className="container">
<h1 className="title">Home Page</h1>
<h1 className="title">Page not found</h1>
<PeoplePage />
<Routes>
<Route path="/" element={<h1 className="title">Home Page</h1>} />
<Route path="home" element={<Navigate to="/" replace />} />
<Route path="people">
<Route index element={<PeoplePage />} />
<Route path=":personSlug" element={<PeoplePage />} />
</Route>

<Route
path="*"
element={<h1 className="title">Page not found</h1>}
/>
</Routes>
</div>
</div>
</div>
Expand Down
28 changes: 21 additions & 7 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { NavLink, useLocation } from 'react-router-dom';

export const Navbar = () => {
const { search } = useLocation();

return (
<nav
data-cy="nav"
Expand All @@ -8,17 +12,27 @@ export const Navbar = () => {
>
<div className="container">
<div className="navbar-brand">
<a className="navbar-item" href="#/">
<NavLink
to="/"
end
className={({ isActive }) =>
`navbar-item ${isActive ? 'is-active has-background-grey-lighter' : ''}`
}
>
Home
</a>
</NavLink>

<a
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
<NavLink
to={{
pathname: '/people',
search: search,
}}
className={({ isActive }) =>
`navbar-item ${isActive ? 'is-active has-background-grey-lighter' : ''}`
}
>
People
</a>
</NavLink>
</div>
</div>
</nav>
Expand Down
119 changes: 73 additions & 46 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
import { useSearchParams } from 'react-router-dom';
import cn from 'classnames';
import { SearchLink } from './SearchLink';

export const PeopleFilters = () => {
const [searchParams, setSearchParams] = useSearchParams();
const query = searchParams.get('query') || '';
const sex = searchParams.get('sex') || '';
const selectedCenturies = searchParams.getAll('centuries');

return (
<nav className="panel">
<p className="panel-heading">Filters</p>

<p className="panel-tabs" data-cy="SexFilter">
<a className="is-active" href="#/people">
<SearchLink
params={{ sex: null }}
className={cn({ 'is-active': !sex })}
>
All
</a>
<a className="" href="#/people?sex=m">
</SearchLink>
<SearchLink
params={{ sex: 'm' }}
className={cn({ 'is-active': sex === 'm' })}
>
Male
</a>
<a className="" href="#/people?sex=f">
</SearchLink>
<SearchLink
params={{ sex: 'f' }}
className={cn({ 'is-active': sex === 'f' })}
>
Female
</a>
</SearchLink>
</p>

<div className="panel-block">
Expand All @@ -22,6 +40,19 @@ export const PeopleFilters = () => {
type="search"
className="input"
placeholder="Search"
value={query}
onChange={e => {
const newParams = new URLSearchParams(searchParams);
const trimmedValue = e.target.value.trim();

if (trimmedValue) {
newParams.set('query', trimmedValue);
} else {
newParams.delete('query');
}

setSearchParams(newParams);
}}
/>

<span className="icon is-left">
Expand All @@ -33,63 +64,59 @@ export const PeopleFilters = () => {
<div className="panel-block">
<div className="level is-flex-grow-1 is-mobile" data-cy="CenturyFilter">
<div className="level-left">
<a
data-cy="century"
className="button mr-1"
href="#/people?centuries=16"
>
16
</a>
{[16, 17, 18, 19, 20].map(century => {
const centuryStr = century.toString();
const isActive = selectedCenturies.includes(centuryStr);

<a
data-cy="century"
className="button mr-1 is-info"
href="#/people?centuries=17"
>
17
</a>
return (
<button
key={century}
data-cy="century"
className={cn('button mr-1', { 'is-info': isActive })}
onClick={() => {
const newParams = new URLSearchParams(searchParams);
const current = newParams.getAll('centuries');

<a
data-cy="century"
className="button mr-1 is-info"
href="#/people?centuries=18"
>
18
</a>
newParams.delete('centuries');

<a
data-cy="century"
className="button mr-1 is-info"
href="#/people?centuries=19"
>
19
</a>
if (isActive) {
current
.filter(c => c !== centuryStr)
.forEach(c => newParams.append('centuries', c));
} else {
[...current, centuryStr].forEach(c =>
newParams.append('centuries', c),
);
}

<a
data-cy="century"
className="button mr-1"
href="#/people?centuries=20"
>
20
</a>
setSearchParams(newParams);
}}
>
{century}
</button>
);
})}
</div>

<div className="level-right ml-4">
<a
<SearchLink
data-cy="centuryALL"
className="button is-success is-outlined"
href="#/people"
params={{ centuries: null }}
>
All
</a>
</SearchLink>
</div>
</div>
</div>

<div className="panel-block">
<a className="button is-link is-outlined is-fullwidth" href="#/people">
<SearchLink
className="button is-link is-outlined is-fullwidth"
params={{ query: null, centuries: null, sort: null, order: null, sex: null }}
>
Reset all filters
</a>
</SearchLink>
</div>
</nav>
);
Expand Down
125 changes: 117 additions & 8 deletions src/components/PeoplePage.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,138 @@
/* eslint-disable max-len */
/* eslint-disable prettier/prettier */
import { PeopleFilters } from './PeopleFilters';
import { Loader } from './Loader';
import { PeopleTable } from './PeopleTable';
import { useSearchParams } from 'react-router-dom';
import { useEffect, useMemo, useState } from 'react';
import { getPeople } from '../api';
import { Person } from '../types';

export const PeoplePage = () => {
const [searchParams] = useSearchParams();
const [people, setPeople] = useState<Person[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(false);
const query = searchParams.get('query')?.toLowerCase() || '';
const sex = searchParams.get('sex') || '';
const centuries = searchParams.getAll('centuries');
const sortBy = searchParams.get('sort') || '';
const order = searchParams.get('order') || '';

useEffect(() => {
setLoading(true);
setError(false);
getPeople()
.then(data => {
setPeople(data);
setLoading(false);
})
.catch(() => {
setError(true);
setLoading(false);
});
}, []);

const visiblePeople = useMemo(() => {
let filtered = [...people];

if (sex) {
filtered = filtered.filter(person => person.sex === sex);
}

if (query) {
filtered = filtered.filter(person => {
const matchesName = person.name.toLowerCase().includes(query);

const matchesMother = person.motherName?.toLowerCase().includes(query);

const matchesFather = person.fatherName?.toLowerCase().includes(query);

return matchesName || matchesMother || matchesFather;
});
}

if (centuries.length > 0) {
filtered = filtered.filter(person => {
const century = Math.ceil(person.born / 100);

return centuries.includes(century.toString());
});
}

if (sortBy) {
filtered.sort((a, b) => {
const valA = a[sortBy as keyof Person];
const valB = b[sortBy as keyof Person];

let result = 0;

if (typeof valA === 'number' && typeof valB === 'number') {
result = valA - valB;
} else {
result = String(valA).localeCompare(String(valB));
}

return order === 'desc' ? -result : result;
});
}

return filtered;
}, [people, sex, query, centuries, sortBy, order]);

const getSortParams = (field: string) => {
if (sortBy !== field) {
return { sort: field, order: null };
}

if (order !== 'desc') {
return { sort: field, order: 'desc' };
}

return { sort: null, order: null };
};

return (
<>
<h1 className="title">People Page</h1>

<div className="block">
<div className="columns is-desktop is-flex-direction-row-reverse">
<div className="column is-7-tablet is-narrow-desktop">
<PeopleFilters />
</div>
{!loading && !error && people.length > 0 && (
<div className="column is-7-tablet is-narrow-desktop">
<PeopleFilters />
</div>
)}

<div className="column">
<div className="box table-container">
<Loader />
{loading && <Loader />}

<p data-cy="peopleLoadingError">Something went wrong</p>
{!loading && error && (
<p data-cy="peopleLoadingError">Something went wrong</p>
)}

<p data-cy="noPeopleMessage">There are no people on the server</p>
{!loading && !error && people.length === 0 && (
<p data-cy="noPeopleMessage">There are no people on the server</p>

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "Reset all filters" link currently removes query, centuries, sort, and order, but it leaves the sex filter untouched. To fully match the requirement to reset all filters (checklist items #1, #3, and #30 implicitly), include sex: null in the params here so the SexFilter is also cleared when this button is clicked.

)}

<p>There are no people matching the current search criteria</p>
{!loading &&
!error &&
people.length > 0 &&
visiblePeople.length === 0 && (
<p>
There are no people matching the current search criteria{' '}
</p>
)}

<PeopleTable />
{!loading && !error && visiblePeople.length > 0 && (
<PeopleTable
people={visiblePeople}
sortBy={sortBy}
order={order}
getSortParams={getSortParams}
/>
)}
</div>
</div>
</div>
Expand Down
Loading
Loading