/* eslint-disable react-hooks/exhaustive-deps */ import { SearchOutlined } from '@ant-design/icons'; import { Avatar, Button, Input, List, Tag } from 'antd'; import React, { ChangeEvent, useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useSelector } from 'react-redux'; import { createFlight, getListUsersInvitedFlight } from '../../../../api/flightApi'; import defaultAvatar from '../../../../assets/defaultAvatar.jpg'; import UniformModal from '../../../../components/CustomLibs/UniformModal'; import { ScrollBars } from '../../../../components/CustomLibs/UniFormScrollBar'; import { TypeOfNotification, useNotification } from '../../../../context/NotificationContext'; import { RootState } from '../../../../redux/store'; import { UserTournament } from '../../../../types/tournament'; import { OverflowElementTitle, useDebounce } from '../../../../utils'; import FlightSelection from './FlightSelection'; import './index.css'; interface CreateFlightProps { isVisible: boolean; onClose: () => void; tournamentId: number; } const CreateFlightModal: React.FC = ({ isVisible, onClose, tournamentId }) => { const { t } = useTranslation(); const { show } = useNotification(); const { user } = useSelector((state: RootState) => state.auth); const [selectedMembers, setSelectedMembers] = useState(user && user.id !== null ? [user.id] : []); const [searchText, setSearchText] = useState(''); const [loading, setLoading] = useState(false); const [page, setPage] = useState(1); const [users, setUsers] = useState([]); const [hasMore, setHasMore] = useState(true); const perPage = 4; const transformUserData = (data: UserTournament[]) => { return data.map((users) => ({ ...users, gender: users.gender === 'FEMALE' ? t('listUserPage.female') : users.gender === 'MALE' ? t('listUserPage.male') : 'Unknown', })) as UserTournament[]; }; const debouncedHandleSearch = useDebounce((newSearchText: string) => { setSearchText(newSearchText); setPage(1); // Reset to the first page when the search changes setHasMore(true); }, 500); const fetchData = async () => { setLoading(true); try { const response = await getListUsersInvitedFlight({ text_search: searchText, per_page: perPage, page }, tournamentId); setTimeout(() => { if (page === 1) { setUsers(transformUserData(response.data.items)); } else { setUsers((prevUsers) => [...prevUsers, ...transformUserData(response.data.items)]); } setHasMore(response.data.items.length === perPage); setLoading(false); }, 1000); } catch (error: any) { show(TypeOfNotification.ERROR, error.response?.data?.detail); } }; useEffect(() => { isVisible && fetchData(); }, [isVisible, page, tournamentId, searchText]); useEffect(() => { if (user && user.id !== null && !selectedMembers.includes(user.id)) { setSelectedMembers((prev) => (user && user.id !== null ? [user.id, ...prev] : prev)); } }, [user, isVisible]); const toggleSelection = (id: number) => { setSelectedMembers((prev) => { if (prev.includes(id)) { return prev.filter((item) => item !== id); } else if (prev.length < 6) { return [...prev, id]; } else { show(TypeOfNotification.WARNING, t('createFlightModal.maxMembersWarning')); return prev; } }); }; const handleClose = () => { setSelectedMembers([]); onClose(); }; const handleSubmit = async () => { try { await createFlight(tournamentId, selectedMembers); show(TypeOfNotification.SUCCESS, t('createFlightModal.successMessage')); handleClose(); // Đóng modal sau khi tạo flight thành công } catch (error: any) { show(TypeOfNotification.ERROR, error.response?.data?.detail || t('createFlightModal.errorMessage')); } }; const handleSearch = (e: ChangeEvent) => { debouncedHandleSearch(e.target.value); }; const renderList = () => ( <> {!loading && ( ( } title={
{member.full_name}} />
} description={ <> {member.vga} {' '}    HDC {member.handicap_index} } />
)} /> )} ); return ( {t('createFlightModal.createFlight')} } onCancel={handleClose} footer={ <>
{selectedMembers.length > 0 && ( )}
} className="tournament-create-flight-team-modal" width={450} >
{selectedMembers.length > 0 && ( <> )}

{t('createFlightModal.memberTournament')}

} allowClear onChange={handleSearch} />
{ setPage((pre) => pre + 1); }} customStyle={{ padding: '8px 8px 0px 0px', }} > {renderList()}
); }; export default CreateFlightModal;