Skip to content

Commit

Permalink
move vars, types and enums into separate files, change logic of filte…
Browse files Browse the repository at this point in the history
…r function, create vars for 'magic values'
  • Loading branch information
KatOlista committed Oct 6, 2023
1 parent dabe14a commit dbeac6d
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 98 deletions.
115 changes: 35 additions & 80 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,84 +1,42 @@
import { FilterType } from './types/FilterType';
import { Person } from './types/Person';

// eslint-disable-next-line max-len
const API_URL = 'https://mate-academy.github.io/react_people-table/api/people.json';

export const NOT_SET_VALUE = '-';

export enum ColumnNames {
Name = 'Name',
Sex = 'Sex',
Born = 'Born',
Died = 'Died',
}

export enum PersonSex {
All = '',
Male = 'm',
Female = 'f',
}

export type FilterType = {
query: string | '';
centuries: string[];
sex: string | '';
};

export const CENTURIES = ['16', '17', '18', '19', '20'];
import { SortParam } from './types/SortParam';
import { getParent } from './utils/getParentHelper';
import { API_URL, YEARS_IN_CENTURY } from './utils/variables';

function wait(delay: number) {
return new Promise(resolve => setTimeout(resolve, delay));
}

export async function getPeople(): Promise<Person[]> {
// keep this delay for testing purpose
return wait(500)
.then(() => fetch(API_URL))
.then(response => response.json());
}

function getParent(people: Person[], parentName: string) {
return people.find(({ name }) => name === parentName);
}

export function addParent(people: Person[]) {
return people.map(person => {
let mother;
let father;

if (person.motherName) {
mother = getParent(people, person.motherName);
}

if (person.fatherName) {
father = getParent(people, person.fatherName);
}
const { motherName, fatherName } = person;

return {
...person,
mother,
father,
mother: motherName ? getParent(people, motherName) : undefined,
father: fatherName ? getParent(people, fatherName) : undefined,
};
});
}

export const sortPeople = (people: Person[], sortParam: string) => {
export function sortPeople(people: Person[], sortParam: SortParam | string) {
if (sortParam) {
return [...people].sort((a, b) => {
switch (sortParam) {
case ('name'): {
return a[sortParam].localeCompare(b[sortParam]);
}

case ('sex'): {
case (SortParam.Name):
case (SortParam.Sex): {
return a[sortParam].localeCompare(b[sortParam]);
}

case ('born'): {
return a[sortParam] - (b[sortParam]);
}

case ('died'): {
case (SortParam.Born):
case (SortParam.Died): {
return a[sortParam] - (b[sortParam]);
}

Expand All @@ -90,36 +48,33 @@ export const sortPeople = (people: Person[], sortParam: string) => {
}

return [...people];
};
}

export const filterPeople = (
export function filterPeople(
filterOption: FilterType,
people: Person[],
) => {
return people
.filter(person => {
if (filterOption.sex) {
return person.sex === filterOption.sex;
}

return person;
})
.filter(person => {
if (filterOption.centuries.length) {
const personBirthCentury = Math.ceil(person.born / 100);
) {
let filteredPeople = people;

return filterOption.centuries.includes(personBirthCentury.toString());
}
if (filterOption.sex) {
filteredPeople = filteredPeople
.filter(person => person.sex === filterOption.sex);
}

return person;
})
.filter(person => {
if (filterOption.query) {
return person.name.includes(filterOption.query)
|| person.motherName?.includes(filterOption.query)
|| person.fatherName?.includes(filterOption.query);
}
if (filterOption.centuries.length) {
filteredPeople = filteredPeople.filter(person => {
const personBirthCentury = Math.ceil(person.born / YEARS_IN_CENTURY);

return person;
return filterOption.centuries.includes(personBirthCentury.toString());
});
};
}

if (filterOption.query) {
filteredPeople = filteredPeople
.filter(person => person.name.includes(filterOption.query)
|| person.motherName?.includes(filterOption.query)
|| person.fatherName?.includes(filterOption.query));
}

return filteredPeople;
}
2 changes: 1 addition & 1 deletion src/components/ColumnNameItem/ColumnNameItem.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';
import { useSearchParams } from 'react-router-dom';

import { ColumnNames } from '../../api';
import { SearchLink } from '../SearchLink';
import { ColumnNames } from '../../types';

type Props = {
value: keyof typeof ColumnNames,
Expand Down
2 changes: 1 addition & 1 deletion src/components/FilterSexItem/FilterSexItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useSearchParams } from 'react-router-dom';
import cn from 'classnames';

import { SearchLink } from '../SearchLink';
import { PersonSex } from '../../api';
import { PersonSex } from '../../types';

type Props = {
sexValue: keyof typeof PersonSex,
Expand Down
3 changes: 2 additions & 1 deletion src/components/PeopleFilters/PeopleFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { useSearchParams } from 'react-router-dom';
import cn from 'classnames';

import { CenturyItem } from '../FilterCenturyItem';
import { CENTURIES, PersonSex } from '../../api';
import { PersonSexItem } from '../FilterSexItem';
import { SearchLink } from '../SearchLink';
import { getSearchWith } from '../../utils/searchHelper';
import { PersonSex } from '../../types';
import { CENTURIES } from '../../utils/variables';

export const PeopleFilters = () => {
const [searchParams, setSearchParams] = useSearchParams();
Expand Down
15 changes: 6 additions & 9 deletions src/components/PeopleTable/PeopleTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ import { Outlet, useParams, useSearchParams } from 'react-router-dom';
import { PersonItem } from '../PersonItem';
import { ColumnName } from '../ColumnNameItem';

import {
ColumnNames,
FilterType,
filterPeople,
sortPeople,
} from '../../api';
import { filterPeople, sortPeople } from '../../api';

import { Person } from '../../types';
import { ColumnNames, FilterType, Person } from '../../types';
import { SortParam } from '../../types/SortParam';
import { DESC_SORT } from '../../utils/variables';

type Props = {
people: Person[],
Expand All @@ -30,7 +27,7 @@ export const PeopleTable: React.FC<Props> = ({
const [sortedPeople, setSortedPeople] = useState(people);

const [searchParams] = useSearchParams();
const sort = searchParams.get('sort') || '';
const sort: typeof SortParam | string = searchParams.get('sort') || '';
const order = searchParams.get('order') || '';
const query = searchParams.get('query') || '';
const centuries = searchParams.getAll('centuries') || [];
Expand All @@ -50,7 +47,7 @@ export const PeopleTable: React.FC<Props> = ({
setFilteredPeople(filterPeople(filters, sortedPeople));
}, [query, centuries.length, sex, sortedPeople]);

if (order === 'desc') {
if (order === DESC_SORT) {
filteredPeople.reverse();
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/PersonItem/PersonItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import cn from 'classnames';

import { Person } from '../../types';
import { PersonLink } from '../PersonLink';
import { NOT_SET_VALUE } from '../../api';
import { NOT_SET_VALUE } from '../../utils/variables';

type Props = {
person: Person;
Expand Down
6 changes: 1 addition & 5 deletions src/components/PersonLink/PersonLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ import { Link, useSearchParams } from 'react-router-dom';
import cn from 'classnames';

import { Person } from '../../types';
import { Sex } from '../../types/Sex';

type Props = {
person: Person,
};

enum Sex {
FEMALE = 'f',
MALE = 'm',
}

export const PersonLink: React.FC<Props> = ({ person }) => {
const {
name,
Expand Down
6 changes: 6 additions & 0 deletions src/types/ColumnNames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum ColumnNames {
Name = 'Name',
Sex = 'Sex',
Born = 'Born',
Died = 'Died',
}
5 changes: 5 additions & 0 deletions src/types/FilterType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type FilterType = {
query: string;
centuries: string[];
sex: string;
};
5 changes: 5 additions & 0 deletions src/types/PersonSex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum PersonSex {
All = '',
Male = 'm',
Female = 'f',
}
4 changes: 4 additions & 0 deletions src/types/Sex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum Sex {
FEMALE = 'f',
MALE = 'm',
}
6 changes: 6 additions & 0 deletions src/types/SortParam.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum SortParam {
Name = 'name',
Sex = 'sex',
Born = 'born',
Died = 'died',
}
3 changes: 3 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export * from './Person';
export * from './ColumnNames';
export * from './FilterType';
export * from './PersonSex';
5 changes: 5 additions & 0 deletions src/utils/getParentHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Person } from '../types';

export function getParent(people: Person[], parentName: string) {
return people.find(({ name }) => name === parentName);
}
10 changes: 10 additions & 0 deletions src/utils/variables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const NOT_SET_VALUE = '-';

export const YEARS_IN_CENTURY = 100;

export const DESC_SORT = 'desc';

export const CENTURIES = ['16', '17', '18', '19', '20'];

// eslint-disable-next-line max-len
export const API_URL = 'https://mate-academy.github.io/react_people-table/api/people.json';

0 comments on commit dbeac6d

Please sign in to comment.