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

Solution #1051

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

import './App.scss';
import { Outlet } from 'react-router-dom';
import React from 'react';

export const App = () => {
return (
Expand All @@ -10,9 +11,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
32 changes: 32 additions & 0 deletions src/Root.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
HashRouter as Router,
Routes,
Route,
Navigate,
} from 'react-router-dom';

import { App } from './App';
import { PageNotFound } from './page/PageNotFound';
import { HomePageTitle } from './page/HomePageTitle';
import { PeoplePage } from './components/PeoplePage';
import { Routes as AppRoutes } from './enum/routes.enum';

export const Root = () => {
return (
<Router>
<Routes>
<Route path={AppRoutes.Home} element={<App />}>
<Route path={AppRoutes.PageNotFound} element={<PageNotFound />} />

<Route index element={<HomePageTitle />} />

<Route path={AppRoutes.People}>
<Route path={AppRoutes.PeopleWithSlug} element={<PeoplePage />} />
</Route>

<Route path={AppRoutes.HomeRedirect} element={<Navigate to={AppRoutes.Home} replace />} />
</Route>
</Routes>
</Router>
);
};
20 changes: 12 additions & 8 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import { NavLink } from 'react-router-dom';
import cn from 'classnames';

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

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

<a
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
>
<NavLink aria-current="page" className={getNavLinkClass} to="/people">
People
</a>
</NavLink>
</div>
</div>
</nav>
Expand Down
162 changes: 113 additions & 49 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,100 @@
export const PeopleFilters = () => {
import cn from 'classnames';
import { SearchLink } from './SearchLink';
import { FilterBy } from '../types/FilterBy';
import { useEffect, useState } from 'react';
import { centuryList } from '../utils/centuryList';
import { QueryParams } from '../enum/queryParams.enum';
import { Sex } from '../enum/sex.enum';

type PeopleFilterProps = {
searchParams: URLSearchParams;
setQuery: React.Dispatch<React.SetStateAction<string>>;
setSearchParams: React.Dispatch<React.SetStateAction<URLSearchParams>>;
activeQuery: string;
toggleCenturies: (currentCentury: number) => string[];
activeCenturies: string[];
};

export const PeopleFilters = ({
toggleCenturies,
searchParams,
setQuery,
activeQuery,
setSearchParams,
activeCenturies,
}: PeopleFilterProps) => {
const [activeFilter, setActiveFilter] = useState('');

useEffect(() => {
setActiveFilter(searchParams.get(QueryParams.Sex) || '');
setQuery(searchParams.get(QueryParams.Query) || '');
}, [searchParams, setQuery]);

const handleQueryChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const newSearchParams = new URLSearchParams(searchParams);

if (!event.target.value) {
newSearchParams.delete(QueryParams.Query);
} else {
newSearchParams.set(QueryParams.Query, event.target.value);
}

setSearchParams(newSearchParams);
};

const resetAllFilters = () => {
setQuery('');
setActiveFilter('');
setSearchParams(new URLSearchParams());
};

const handleResetSexFilter = (e: React.MouseEvent<HTMLAnchorElement>) => {
e.preventDefault();
const newSearchParams = new URLSearchParams(searchParams);
newSearchParams.delete(QueryParams.Sex);
setSearchParams(newSearchParams);
};

const handleSetMaleFilter = (e: React.MouseEvent<HTMLAnchorElement>) => {
e.preventDefault();
const newSearchParams = new URLSearchParams(searchParams);
newSearchParams.set(QueryParams.Sex, Sex.Male);
setSearchParams(newSearchParams);
};


const handleSetFemaleFilter = (e: React.MouseEvent<HTMLAnchorElement>) => {
e.preventDefault();
const newSearchParams = new URLSearchParams(searchParams);
newSearchParams.set(QueryParams.Sex, Sex.Female);
setSearchParams(newSearchParams);
};

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
className={cn({ 'is-active': activeFilter === '' })}
href="#"
onClick={handleResetSexFilter}
>
{FilterBy.All}
</a>
<a className="" href="#/people?sex=m">
Male
<a
className={cn({ 'is-active': activeFilter === Sex.Male })}
href="#"
onClick={handleSetMaleFilter}
>
{FilterBy.Male}
</a>
<a className="" href="#/people?sex=f">
Female
<a
className={cn({ 'is-active': activeFilter === Sex.Female })}
href="#"
onClick={handleSetFemaleFilter}
>
{FilterBy.Female}
</a>
</p>

Expand All @@ -22,6 +105,8 @@ export const PeopleFilters = () => {
type="search"
className="input"
placeholder="Search"
value={activeQuery}
onChange={handleQueryChange}
/>

<span className="icon is-left">
Expand All @@ -33,51 +118,26 @@ 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>

<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>

<a
data-cy="century"
className="button mr-1"
href="#/people?centuries=20"
>
20
</a>
{centuryList.map((century, index) => (
<SearchLink
key={index}
data-cy="century"
className={cn('button mr-1', {
'is-info': activeCenturies.includes(String(century)),
})}
params={{ centuries: toggleCenturies(century) }}
>
{century}
</SearchLink>
))}
</div>

<div className="level-right ml-4">
<a
data-cy="centuryALL"
className="button is-success is-outlined"
className={cn('button is-success ', {
'is-outlined': !!activeCenturies.length,
})}
href="#/people"
>
All
Expand All @@ -87,10 +147,14 @@ export const PeopleFilters = () => {
</div>

<div className="panel-block">
<a className="button is-link is-outlined is-fullwidth" href="#/people">
<a
className="button is-link is-outlined is-fullwidth"
href="#/people"
onClick={resetAllFilters}
>
Reset all filters
</a>
</div>
</nav>
);
};
}
Loading
Loading