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 2 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
33 changes: 33 additions & 0 deletions src/Root.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {
HashRouter as Router,
Routes,
Route,
Navigate,
} from 'react-router-dom';

import { App } from './App';
// eslint-disable-next-line import/extensions

Choose a reason for hiding this comment

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

do u need this disable?why?

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
153 changes: 105 additions & 48 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,93 @@
export const PeopleFilters = () => {
import cn from 'classnames';

import { SearchLink } from './SearchLink';
// eslint-disable-next-line import/extensions
import { FilterBy } from '../types/FilterBy';
import { useEffect, useState } from 'react';

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('');
const centuryList = [16, 17, 18, 19, 20];

Choose a reason for hiding this comment

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

move it to utils directory. consts file

useEffect(() => {
setActiveFilter(searchParams.get('sex') || '');
setQuery(searchParams.get('query') || '');
}, [searchParams, setQuery]);

Choose a reason for hiding this comment

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

you have the enum QueryParams so use it

Suggested change
setActiveFilter(searchParams.get('sex') || '');
setQuery(searchParams.get('query') || '');
setActiveFilter(searchParams.get(QueryParams.Sex) || '');
setQuery(searchParams.get(QueryParams.Query) || '');


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

if (!event.target.value) {
newSearchParams.delete('query');
} else {

Choose a reason for hiding this comment

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

use enum QueryParams

Suggested change
newSearchParams.delete('query');
newSearchParams.delete(QueryParams.Query);

newSearchParams.set('query', event.target.value);
}

Choose a reason for hiding this comment

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

the same here, use enum QueryParams


setSearchParams(newSearchParams);
};

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

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={e => {
e.preventDefault();
const newSearchParams = new URLSearchParams(searchParams);
newSearchParams.delete('sex');
setSearchParams(newSearchParams);
}}
>

Choose a reason for hiding this comment

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

use separate functions whenever u need a handle function

{FilterBy.All}
</a>
<a className="" href="#/people?sex=m">
Male
<a
className={cn({ 'is-active': activeFilter === 'm' })}
href="#"
onClick={e => {
e.preventDefault();
const newSearchParams = new URLSearchParams(searchParams);
newSearchParams.set('sex', 'm');
setSearchParams(newSearchParams);
}}
>
{FilterBy.Male}

Choose a reason for hiding this comment

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

u can make 1 universal function for all cases

</a>
<a className="" href="#/people?sex=f">
Female
<a
className={cn({ 'is-active': activeFilter === 'f' })}
href="#"
onClick={e => {
e.preventDefault();
const newSearchParams = new URLSearchParams(searchParams);
newSearchParams.set('sex', 'f');
setSearchParams(newSearchParams);
}}
>
{FilterBy.Female}
</a>
</p>

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

<span className="icon is-left">
Expand All @@ -33,51 +111,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,7 +140,11 @@ 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>
Expand Down
Loading
Loading