Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add task solution #619

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 2 additions & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PeoplePage } from './components/PeoplePage';
import { Outlet } from 'react-router-dom';
import { Navbar } from './components/Navbar';

import './App.scss';
Expand All @@ -10,9 +10,7 @@ export const App = () => {

<div className="section">
<div className="container">
<h1 className="title">Home Page</h1>
<h1 className="title">Page not found</h1>
<PeoplePage />
<Outlet />
</div>
</div>
</div>
Expand Down
21 changes: 14 additions & 7 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import cn from 'classnames';
import { NavLink } from 'react-router-dom';

export const Navbar = () => {
const getLinkClass = ({ isActive }: { isActive: boolean }) => {
return cn('navbar-item', {
'has-background-grey-lighter': isActive,
});
};

return (
<nav
data-cy="nav"
Expand All @@ -8,15 +17,13 @@ export const Navbar = () => {
>
<div className="container">
<div className="navbar-brand">
<a className="navbar-item" href="#/">Home</a>
<NavLink to="/" className={getLinkClass}>
Home
</NavLink>

<a
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
>
<NavLink to="/people" className={getLinkClass}>
People
</a>
</NavLink>
</div>
</div>
</nav>
Expand Down
129 changes: 75 additions & 54 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,52 @@
import { Link, useSearchParams } from 'react-router-dom';
import cn from 'classnames';
import { getSearchWith } from '../utils/searchHelper';
import { SearchLink } from './SearchLink';

enum SexFilter {
All = '',
Male = 'm',
Female = 'f',
}

export const PeopleFilters = () => {
const [searchParams, setSearchParams] = useSearchParams();
const query = searchParams.get('query') || '';
const centuries = searchParams.getAll('centuries') || [];
const sex = searchParams.get('sex') || '';
const centuriesList = [16, 17, 18, 19, 20];

function setSearchWith(params: any) {

Check warning on line 19 in src/components/PeopleFilters.tsx

View workflow job for this annotation

GitHub Actions / run_linter (14.x)

Unexpected any. Specify a different type
const search = getSearchWith(params, searchParams);

setSearchParams(search);
}

function toggleCentury(century: number) {
return getSearchWith({
centuries: centuries.includes(century.toString())
? centuries.filter(item => item !== century.toString())
: [...centuries, century.toString()],
}, searchParams);
}

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

<p className="panel-tabs" data-cy="SexFilter">
<a className="is-active" href="#/people">All</a>
<a className="" href="#/people?sex=m">Male</a>
<a className="" href="#/people?sex=f">Female</a>
{Object.entries(SexFilter)
.map(([key, value]: [key: string, value: string]) => (
<SearchLink
key={key}
className={cn({
'is-active': sex === value,
})}
params={{ sex: value || null }}
>
{key}
</SearchLink>
))}
</p>

<div className="panel-block">
Expand All @@ -16,6 +56,10 @@
type="search"
className="input"
placeholder="Search"
value={query}
onChange={(event) => {
setSearchWith({ query: event.target.value || null });
}}
/>

<span className="icon is-left">
Expand All @@ -27,66 +71,43 @@
<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>

<a
data-cy="century"
className="button mr-1 is-info"
href="#/people?centuries=17"
>
17
</a>

<a
data-cy="century"
className="button mr-1 is-info"
href="#/people?centuries=18"
>
18
</a>

<a
data-cy="century"
className="button mr-1 is-info"
href="#/people?centuries=19"
>
19
</a>
{centuriesList.map(century => (
<Link

Choose a reason for hiding this comment

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

Also should be SearchLink

data-cy="century"
key={century}
className={cn('button mr-1', {
'is-info': centuries.includes(century.toString()),
})}
to={{
search: toggleCentury(century),
}}
>
{century}
</Link>
))}

<a
data-cy="century"
className="button mr-1"
href="#/people?centuries=20"
>
20
</a>
</div>

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

<div className="panel-block">
<a
<SearchLink
className="button is-link is-outlined is-fullwidth"
href="#/people"
params={{ query: null, sex: null, centuries: null }}

Choose a reason for hiding this comment

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

Just pass new URLSearchParams(), it is too much work to reset each separately

>
Reset all filters
</a>
</SearchLink>
</div>
</nav>
);
Expand Down
35 changes: 0 additions & 35 deletions src/components/PeoplePage.tsx

This file was deleted.

Loading
Loading