-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Develop #609
base: master
Are you sure you want to change the base?
Develop #609
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,23 @@ | ||
import { Routes, Route, Navigate } from 'react-router-dom'; | ||
import { PeoplePage } from './components/PeoplePage'; | ||
import { Navbar } from './components/Navbar'; | ||
|
||
import './App.scss'; | ||
|
||
export const App = () => { | ||
return ( | ||
<div data-cy="app"> | ||
<Navbar /> | ||
|
||
<div className="section"> | ||
<div className="container"> | ||
<h1 className="title">Home Page</h1> | ||
<h1 className="title">Page not found</h1> | ||
<PeoplePage /> | ||
</div> | ||
export const App = () => ( | ||
<div data-cy="app"> | ||
<Navbar /> | ||
<main className="section"> | ||
<div className="container"> | ||
<Routes> | ||
<Route path="/home" element={<Navigate to="/" replace />} /> | ||
<Route path="/" element={<h1 className="title">Home Page</h1>} /> | ||
<Route path="people"> | ||
<Route index element={<PeoplePage />} /> | ||
<Route path=":userSlug" element={<PeoplePage />} /> | ||
</Route> | ||
<Route path="*" element={<h1 className="title">Page not found</h1>} /> | ||
</Routes> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
</main> | ||
</div> | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,50 @@ | ||
import classnames from 'classnames'; | ||
import { useSearchParams } from 'react-router-dom'; | ||
import { SearchLink } from './SearchLink'; | ||
import { getSearchWith } from '../utils/searchHelper'; | ||
|
||
export const PeopleFilters = () => { | ||
const [searchParams, setSearchParams] = useSearchParams(); | ||
|
||
const sex = searchParams.get('sex'); | ||
const query = searchParams.get('query') || ''; | ||
const centuries = searchParams.getAll('centuries') || []; | ||
|
||
const handleQueryChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
const newQueryParam = getSearchWith(searchParams, { | ||
query: event.target.value.trim().toLocaleLowerCase() || null, | ||
}); | ||
|
||
setSearchParams(newQueryParam); | ||
}; | ||
|
||
const centuriesItem = ['16', '17', '18', '19', '20']; | ||
|
||
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> | ||
<SearchLink | ||
className={classnames({ 'is-active': !sex })} | ||
params={{ sex: null }} | ||
> | ||
All | ||
</SearchLink> | ||
|
||
<SearchLink | ||
className={classnames({ 'is-active': sex === 'm' })} | ||
params={{ sex: 'm' }} | ||
> | ||
Male | ||
</SearchLink> | ||
|
||
<SearchLink | ||
className={classnames({ 'is-active': sex === 'f' })} | ||
params={{ sex: 'f' }} | ||
> | ||
Female | ||
</SearchLink> | ||
</p> | ||
|
||
<div className="panel-block"> | ||
|
@@ -16,6 +54,8 @@ export const PeopleFilters = () => { | |
type="search" | ||
className="input" | ||
placeholder="Search" | ||
value={query} | ||
onChange={handleQueryChange} | ||
/> | ||
|
||
<span className="icon is-left"> | ||
|
@@ -27,66 +67,46 @@ 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> | ||
{centuriesItem.map((century) => { | ||
return ( | ||
<SearchLink | ||
key={century} | ||
params={{ | ||
centuries: centuries.includes(century) | ||
? centuries.filter((cent) => cent !== century) | ||
: [...centuries, century], | ||
}} | ||
className={classnames('button mr-1', { | ||
'is-info': centuries.includes(century), | ||
})} | ||
> | ||
{century} | ||
</SearchLink> | ||
); | ||
})} | ||
</div> | ||
|
||
<div className="level-right ml-4"> | ||
<a | ||
<SearchLink | ||
data-cy="centuryALL" | ||
className="button is-success is-outlined" | ||
href="#/people" | ||
className={classnames('button', 'is-success', { | ||
'is-outlined': centuries.length > 0, | ||
})} | ||
params={{ centuries: null }} | ||
> | ||
All | ||
</a> | ||
</SearchLink> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<div className="panel-block"> | ||
<a | ||
<SearchLink | ||
className="button is-link is-outlined is-fullwidth" | ||
href="#/people" | ||
params={{ centuries: null, query: null, sex: null }} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Find a better way to reset all by just 1 line of code. If you had 20 filters, would you set null to all of them? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all the other options I can think of |
||
> | ||
Reset all filters | ||
</a> | ||
</SearchLink> | ||
</div> | ||
</nav> | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,107 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { useSearchParams } from 'react-router-dom'; | ||
import { PeopleFilters } from './PeopleFilters'; | ||
import { Loader } from './Loader'; | ||
import { PeopleTable } from './PeopleTable'; | ||
import { getPeople } from '../api'; | ||
|
||
import { Person } from '../types/Person'; | ||
|
||
export const PeoplePage = () => { | ||
const [people, setPeople] = useState<Person[] | null>(null); | ||
const [isLoad, setIsLoad] = useState<boolean>(false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is better to make |
||
const [error, setError] = useState<string | null>(null); | ||
const [searchParams] = useSearchParams(); | ||
|
||
const sex = searchParams.get('sex'); | ||
const query = searchParams.get('query')?.trim().toLocaleLowerCase(); | ||
const centuries = searchParams.getAll('centuries') || []; | ||
const sort = searchParams.get('sort'); | ||
const order = searchParams.get('order'); | ||
|
||
useEffect(() => { | ||
setIsLoad(true); | ||
getPeople() | ||
.then((peopleFromServer) => setPeople(peopleFromServer)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
.catch(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try to catch an |
||
setError('Something went wrong'); | ||
}) | ||
.finally(() => setIsLoad(false)); | ||
}, []); | ||
|
||
const getVisiblePeople = () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better move this function outside the component and just call it here with your arguments. |
||
if (!people) { | ||
return []; | ||
} | ||
|
||
let visiblePeople = [...people]; | ||
|
||
if (sex) { | ||
visiblePeople = visiblePeople.filter((person) => person.sex === sex); | ||
} | ||
|
||
if (query) { | ||
visiblePeople = visiblePeople.filter((person) => person | ||
.name.toLocaleLowerCase().includes(query) | ||
|| person.motherName?.toLocaleLowerCase().includes(query) | ||
|| person.fatherName?.toLocaleLowerCase().includes(query)); | ||
} | ||
|
||
if (centuries.length > 0) { | ||
visiblePeople = visiblePeople.filter((person) => centuries | ||
?.includes(Math.ceil(person.born / 100).toString())); | ||
} | ||
|
||
if (sort) { | ||
visiblePeople.sort((a, b) => { | ||
switch (sort) { | ||
case 'name': | ||
return a.name.localeCompare(b.name); | ||
|
||
case 'sex': | ||
return a.sex.localeCompare(b.sex); | ||
|
||
case 'born': | ||
return (a.born - b.born); | ||
|
||
case 'died': | ||
return (a.died - b.died); | ||
|
||
default: | ||
return 0; | ||
} | ||
}); | ||
} | ||
|
||
if (order) { | ||
visiblePeople.reverse(); | ||
} | ||
|
||
return visiblePeople; | ||
}; | ||
|
||
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 /> | ||
{people && <PeopleFilters />} | ||
</div> | ||
|
||
<div className="column"> | ||
<div className="box table-container"> | ||
<Loader /> | ||
|
||
<p data-cy="peopleLoadingError">Something went wrong</p> | ||
|
||
<p data-cy="noPeopleMessage"> | ||
There are no people on the server | ||
</p> | ||
|
||
<p>There are no people matching the current search criteria</p> | ||
|
||
<PeopleTable /> | ||
{isLoad && <Loader />} | ||
{error && ( | ||
<p data-cy="peopleLoadingError" className="has-text-danger"> | ||
{error} | ||
</p> | ||
)} | ||
{people && <PeopleTable people={getVisiblePeople()} />} | ||
{!isLoad && !error && !people && ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe I've already fixed it with lowder, because I don't see this problem now |
||
<p data-cy="noPeopleMessage">no people</p> | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Render as list