From 0a9ec0ca99c3896f0cf8d6ea20c36d50b0c11fcf Mon Sep 17 00:00:00 2001 From: Denys Date: Tue, 21 Nov 2023 13:08:31 +0200 Subject: [PATCH 1/5] save --- src/App.tsx | 49 +- src/components/Navbar.tsx | 19 +- src/components/PeopleFilters.tsx | 109 +++-- src/components/PeoplePage.tsx | 118 ++++- src/components/PeopleTable.tsx | 772 +++++-------------------------- src/components/PersonLink.tsx | 20 + 6 files changed, 342 insertions(+), 745 deletions(-) create mode 100644 src/components/PersonLink.tsx diff --git a/src/App.tsx b/src/App.tsx index adcb8594e..7418a0f83 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,20 +1,39 @@ +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 ( -
- - -
-
-

Home Page

-

Page not found

- -
+export const App = () => ( +
+ +
+
+ + } /> + Home Page} /> + + } /> + } /> + + Page not found} /> +
-
- ); -}; + +
+); + +// export const App = () => { +// return ( +//
+// + +//
+//
+//

Home Page

+//

Page not found

+// +//
+//
+//
+// ); +// }; diff --git a/src/components/Navbar.tsx b/src/components/Navbar.tsx index c2aa20f1c..67426c0ff 100644 --- a/src/components/Navbar.tsx +++ b/src/components/Navbar.tsx @@ -1,3 +1,10 @@ +import { NavLink } from 'react-router-dom'; +import classNames from 'classnames'; + +const getLinkClass = ({ isActive }: { isActive: boolean }) => { + return classNames('navbar-item', { 'has-background-grey-lighter': isActive }); +}; + export const Navbar = () => { return ( diff --git a/src/components/PeopleFilters.tsx b/src/components/PeopleFilters.tsx index 2f1608bef..e257b9f13 100644 --- a/src/components/PeopleFilters.tsx +++ b/src/components/PeopleFilters.tsx @@ -1,12 +1,52 @@ +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') || []; + + // console.log(centuries); + + const handleQueryChange = (event: React.ChangeEvent) => { + const newQueryParam = getSearchWith(searchParams, { + query: event.target.value.trim().toLocaleLowerCase() || null, + }); + + setSearchParams(newQueryParam); + }; + + const centuriesItem = ['16', '17', '18', '19', '20']; + return ( ); diff --git a/src/components/PeopleTable.tsx b/src/components/PeopleTable.tsx index 83ad8418b..0290468ad 100644 --- a/src/components/PeopleTable.tsx +++ b/src/components/PeopleTable.tsx @@ -19,7 +19,6 @@ export const PeopleTable: React.FC = ({ people }) => { const order = searchParams.get('order'); function togglesort(column: string) { - // console.log(`column-- ${column}`); let newParam = {}; if (sort !== column.toLowerCase() && !order) { @@ -37,48 +36,48 @@ export const PeopleTable: React.FC = ({ people }) => { return newParam; } - // console.log(`sort-- ${sort}`); - return ( - - - {columnsName.map((colum) => { - console.log(sort); - - return ( - - ); - })} + {people.length > 0 ? ( + + + {columnsName.map((colum) => { + return ( + + ); + })} - - - - + + + + + ) : ( +

There are no people matching the current search criteria

+ )} {people.map((person: Person) => { const searchMother = people.find( From e7f5a6ecde30a191b05c17439ca7f994784d3b60 Mon Sep 17 00:00:00 2001 From: Denys Date: Tue, 21 Nov 2023 17:49:42 +0200 Subject: [PATCH 3/5] fix1 --- src/App.tsx | 16 ---------------- src/components/PeopleFilters.tsx | 2 -- src/components/PeoplePage.tsx | 18 ------------------ src/components/PeopleTable.tsx | 2 +- 4 files changed, 1 insertion(+), 37 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 7418a0f83..3bb1154d8 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -21,19 +21,3 @@ export const App = () => ( ); - -// export const App = () => { -// return ( -//
-// - -//
-//
-//

Home Page

-//

Page not found

-// -//
-//
-//
-// ); -// }; diff --git a/src/components/PeopleFilters.tsx b/src/components/PeopleFilters.tsx index ea4e91c21..911b897da 100644 --- a/src/components/PeopleFilters.tsx +++ b/src/components/PeopleFilters.tsx @@ -93,7 +93,6 @@ export const PeopleFilters = () => { 'is-outlined': centuries.length > 0, })} params={{ centuries: null }} - // to="#/people" > All @@ -104,7 +103,6 @@ export const PeopleFilters = () => {
Reset all filters diff --git a/src/components/PeoplePage.tsx b/src/components/PeoplePage.tsx index 1be248f85..f6cba9591 100644 --- a/src/components/PeoplePage.tsx +++ b/src/components/PeoplePage.tsx @@ -19,8 +19,6 @@ export const PeoplePage = () => { const sort = searchParams.get('sort'); const order = searchParams.get('order'); - // console.log(order); - useEffect(() => { setIsLoad(true); getPeople() @@ -82,10 +80,6 @@ export const PeoplePage = () => { return visiblePeople; }; - // const visiblePeople = people - // ? people.filter((person) => (sex ? person.sex === sex : true)) - // : []; - return ( <>

People Page

@@ -115,15 +109,3 @@ export const PeoplePage = () => { ); }; - -// Something went wrong - -// There are no people on the server - -// There are no people matching the current search criteria -// -//

Something went wrong

- -//

There are no people on the server

- -//

There are no people matching the current search criteria

diff --git a/src/components/PeopleTable.tsx b/src/components/PeopleTable.tsx index 0290468ad..778bf8f52 100644 --- a/src/components/PeopleTable.tsx +++ b/src/components/PeopleTable.tsx @@ -21,7 +21,7 @@ export const PeopleTable: React.FC = ({ people }) => { function togglesort(column: string) { let newParam = {}; - if (sort !== column.toLowerCase() && !order) { + if (sort !== column.toLowerCase()) { newParam = { sort: column.toLowerCase(), order: null }; } From a1d6e48f6bcd1ddddf007a757b53df1902fbb224 Mon Sep 17 00:00:00 2001 From: Denys Date: Wed, 22 Nov 2023 20:14:58 +0200 Subject: [PATCH 4/5] fix3 --- src/components/PeoplePage.tsx | 69 +++++++---------------------------- src/utils/getVisiblePeople.ts | 61 +++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 55 deletions(-) create mode 100644 src/utils/getVisiblePeople.ts diff --git a/src/components/PeoplePage.tsx b/src/components/PeoplePage.tsx index f6cba9591..728d544f6 100644 --- a/src/components/PeoplePage.tsx +++ b/src/components/PeoplePage.tsx @@ -4,12 +4,12 @@ import { PeopleFilters } from './PeopleFilters'; import { Loader } from './Loader'; import { PeopleTable } from './PeopleTable'; import { getPeople } from '../api'; - +import { getVisiblePeople } from '../utils/getVisiblePeople'; import { Person } from '../types/Person'; export const PeoplePage = () => { const [people, setPeople] = useState(null); - const [isLoad, setIsLoad] = useState(false); + const [isLoad, setIsLoad] = useState(true); const [error, setError] = useState(null); const [searchParams] = useSearchParams(); @@ -20,7 +20,6 @@ export const PeoplePage = () => { const order = searchParams.get('order'); useEffect(() => { - setIsLoad(true); getPeople() .then((peopleFromServer) => setPeople(peopleFromServer)) .catch(() => { @@ -29,57 +28,6 @@ export const PeoplePage = () => { .finally(() => setIsLoad(false)); }, []); - const getVisiblePeople = () => { - 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 ( <>

People Page

@@ -98,7 +46,18 @@ export const PeoplePage = () => { {error}

)} - {people && } + {people && ( + + )} {!isLoad && !error && !people && (

no people

)} diff --git a/src/utils/getVisiblePeople.ts b/src/utils/getVisiblePeople.ts new file mode 100644 index 000000000..1d734050b --- /dev/null +++ b/src/utils/getVisiblePeople.ts @@ -0,0 +1,61 @@ +import { Person } from '../types/Person'; + +export const getVisiblePeople = ( + people: Person[], + sex: string | null, + query: string | undefined, + centuries: string[], + sort: string | null, + order: string | null, +) => { + 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; +}; From f2ddd55467b2df76ae2b6e47cb4c6fdf7dc2b088 Mon Sep 17 00:00:00 2001 From: Denys Date: Sun, 26 Nov 2023 01:36:50 +0200 Subject: [PATCH 5/5] fix-- --- src/components/PeopleFilters.tsx | 35 ++++++++++++++------------------ src/components/PeoplePage.tsx | 24 ++++++++++------------ src/utils/getVisiblePeople.ts | 2 +- 3 files changed, 27 insertions(+), 34 deletions(-) diff --git a/src/components/PeopleFilters.tsx b/src/components/PeopleFilters.tsx index 911b897da..3cd9387a8 100644 --- a/src/components/PeopleFilters.tsx +++ b/src/components/PeopleFilters.tsx @@ -18,6 +18,12 @@ export const PeopleFilters = () => { setSearchParams(newQueryParam); }; + const sexFilters = [ + { label: 'All', value: null }, + { label: 'Male', value: 'm' }, + { label: 'Female', value: 'f' }, + ]; + const centuriesItem = ['16', '17', '18', '19', '20']; return ( @@ -25,26 +31,15 @@ export const PeopleFilters = () => {

Filters

- - All - - - - Male - - - - Female - + {sexFilters.map(({ label, value }) => ( + + {label} + + ))}

diff --git a/src/components/PeoplePage.tsx b/src/components/PeoplePage.tsx index 728d544f6..c49945133 100644 --- a/src/components/PeoplePage.tsx +++ b/src/components/PeoplePage.tsx @@ -21,13 +21,22 @@ export const PeoplePage = () => { useEffect(() => { getPeople() - .then((peopleFromServer) => setPeople(peopleFromServer)) + .then(setPeople) .catch(() => { setError('Something went wrong'); }) .finally(() => setIsLoad(false)); }, []); + const visiblePeople = getVisiblePeople( + people, + sex, + query, + centuries, + sort, + order, + ); + return ( <>

People Page

@@ -46,18 +55,7 @@ export const PeoplePage = () => { {error}

)} - {people && ( - - )} + {people && } {!isLoad && !error && !people && (

no people

)} diff --git a/src/utils/getVisiblePeople.ts b/src/utils/getVisiblePeople.ts index 1d734050b..dd1ef5f02 100644 --- a/src/utils/getVisiblePeople.ts +++ b/src/utils/getVisiblePeople.ts @@ -1,7 +1,7 @@ import { Person } from '../types/Person'; export const getVisiblePeople = ( - people: Person[], + people: Person[] | null, sex: string | null, query: string | undefined, centuries: string[],
- - {colum} - - - - - - -
+ + {colum} + + + + + + + MotherFather
MotherFather