From 192078a57113adec28063f1eeb791cc793a3be0f Mon Sep 17 00:00:00 2001 From: AlexDyakonov Date: Sun, 1 Dec 2024 09:33:43 +0300 Subject: [PATCH] Added find page --- src/pages/find.page.tsx | 116 ++++++++++++++++++++++++++++++++++- src/shared/api/people.api.ts | 27 ++++++++ 2 files changed, 142 insertions(+), 1 deletion(-) diff --git a/src/pages/find.page.tsx b/src/pages/find.page.tsx index 01d81e9..353a878 100644 --- a/src/pages/find.page.tsx +++ b/src/pages/find.page.tsx @@ -1,3 +1,117 @@ +import { useState, useEffect } from 'react'; +import { getAllPeople } from '@/shared/api/people.api'; +import { searchPeople } from '@/shared/api/people.api'; +import { Person } from '@/shared/interfaces/person.interface'; + export const FindPage = () => { - return <>; + const [searchTerm, setSearchTerm] = useState(''); + const [people, setPeople] = useState([]); + const [allPeople, setAllPeople] = useState([]); + const [loading, setLoading] = useState(false); + const [error, setError] = useState(''); + + useEffect(() => { + const fetchAllPeople = async () => { + setLoading(true); + try { + const fetchedPeople = await getAllPeople(); + setAllPeople(fetchedPeople); + setPeople(fetchedPeople); + } catch (err) { + setError('Не удалось загрузить список людей'); + console.error(err); + } finally { + setLoading(false); + } + }; + + fetchAllPeople(); + }, []); + + const handleSearchChange = (event: { target: { value: string } }) => { + setSearchTerm(event.target.value); + }; + + const handleSearchClick = async () => { + if (searchTerm.trim() === '') { + setPeople(allPeople); + return; + } + + setLoading(true); + try { + const results = await searchPeople(searchTerm); + setPeople(results); + } catch (err) { + setError('Не удалось выполнить поиск'); + console.error(err); + } finally { + setLoading(false); + } + }; + + const handleKeyDown = (event: React.KeyboardEvent) => { + if (event.key === 'Enter') { + handleSearchClick(); + } + }; + + return ( +
+
+ +
+ + +
+ +
+ {loading ? ( +
Загрузка...
+ ) : error ? ( +
{error}
+ ) : ( +
+ {people.length > 0 ? ( + people.map((person) => ( +
+ {`${person.name} +

+ {person.name} {person.surname} +

+

{person.jobtitle}

+ + {person.division && ( +

Отдел: {person.division}

+ )} +
+ )) + ) : ( +
Пользователи не найдены
+ )} +
+ )} +
+
+
+ ); }; diff --git a/src/shared/api/people.api.ts b/src/shared/api/people.api.ts index 56edfe7..e91e2cf 100644 --- a/src/shared/api/people.api.ts +++ b/src/shared/api/people.api.ts @@ -14,3 +14,30 @@ export const getPersonById = async (id: string): Promise => { throw error; } }; + +export const getAllPeople = async (): Promise => { + try { + const response = await fetch( + `${API_URL}/persons` + ); + const person: Person[] = await response.json(); + return person; + } catch (error) { + console.error('Error fetching person:', error); + throw error; + } +}; + + +export const searchPeople = async (query: string): Promise => { + try { + const response = await fetch( + `${API_URL}/persons/search?text=${encodeURIComponent(query)}` + ); + const person: Person[] = await response.json(); + return person; + } catch (error) { + console.error('Error fetching person:', error); + throw error; + } +};