import React, { useState } from 'react'; import BaseModal from '@educt/components/Modal'; import { MdGroup } from 'react-icons/md'; import { UserRoleEnum } from '@educt/enums'; import SelectStudentsInput from '@educt/components/SelectStudentsInput'; import { ICourse, IUser } from '@educt/interfaces'; import { CourseServiceInstance } from '@educt/services'; import { useToast } from '@chakra-ui/react'; import useIsMountedRef from '@educt/hooks/useIsMountedRef'; type AddStudentsModalPropsType = { course: Omit; currentStudents: ICourse['students']; isOpen: boolean; onClose: () => void; onAdded: (users: Array) => void; }; const AddStudentsModal: React.FC = ({ isOpen, onClose, onAdded, currentStudents, course, }) => { const [isLoading, setIsLoading] = useState(false); const isMountedRef = useIsMountedRef(); const [students, setStudents] = useState>([]); const toast = useToast(); const handleSelectStudents = (students: Array) => setStudents(students); const handleAttachStudents = async () => { if (students.length !== 0) { try { setIsLoading(true); const ids = students.map(s => s.id); await CourseServiceInstance.attachStudentsList(course.id, ids); toast({ title: 'Students added.', status: 'info' }); onAdded(students); onClose(); } catch (error) { console.error(error); } finally { if (isMountedRef.current) setIsLoading(false); } } }; return ( handleSelectStudents(selected)} placeholder='Search for students...' searchParams={{ role: UserRoleEnum.STUDENT }} /> ); }; export default AddStudentsModal;