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

Added find page #2

Merged
merged 1 commit into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
116 changes: 115 additions & 1 deletion src/pages/find.page.tsx
Original file line number Diff line number Diff line change
@@ -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<Person[]>([]);
const [allPeople, setAllPeople] = useState<Person[]>([]);
const [loading, setLoading] = useState<boolean>(false);
const [error, setError] = useState<string>('');

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 (
<div className="p-5 pt-20">
<div className="mt-10 w-full max-w-screen-xl mx-auto p-5">

<div className="flex justify-center gap-3">
<input
type="search"
value={searchTerm}
onChange={handleSearchChange}
onKeyDown={handleKeyDown}
placeholder="Введите запрос..."
className="w-96 p-3 border rounded-xl focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
<button
onClick={handleSearchClick}
className="p-3 bg-blue-500 text-white rounded-xl hover:bg-blue-600 focus:outline-none transform active:scale-95 transition-transform duration-200"
>
Поиск
</button>
</div>

<div className="mt-10">
{loading ? (
<div>Загрузка...</div>
) : error ? (
<div>{error}</div>
) : (
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-6">
{people.length > 0 ? (
people.map((person) => (
<div
key={person.id}
className="bg-white p-4 rounded-xl shadow-lg flex flex-col items-center justify-center text-center space-y-3"
>
<img
src={person.image}
alt={`${person.name} ${person.surname}`}
className="w-18 h-20 rounded-full mb-3"
/>
<h3 className="text-lg font-semibold">
{person.name} {person.surname}
</h3>
<p className="text-sm text-gray-600">{person.jobtitle}</p>

{person.division && (
<p className="text-sm text-gray-500 mt-1">Отдел: {person.division}</p>
)}
</div>
))
) : (
<div>Пользователи не найдены</div>
)}
</div>
)}
</div>
</div>
</div>
);
};
27 changes: 27 additions & 0 deletions src/shared/api/people.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,30 @@ export const getPersonById = async (id: string): Promise<Person> => {
throw error;
}
};

export const getAllPeople = async (): Promise<Person[]> => {
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<Person[]> => {
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;
}
};