import React, { useState, createContext, useEffect, ReactNode, useRef, useContext } from 'react'; import { Student } from '../types'; import { axiosInstance } from '../utils/axiosInstance'; interface StudentContext { students: Array; selectedStudent: Student | null; changeSelectedStudent: (studentId: number) => void; } export const studentsContext = createContext(undefined); interface StudentsProviderProps { children: ReactNode; } export const StudentsProvider = ({ children }: StudentsProviderProps) => { const [students, setStudents] = useState>([]); const [selectedStudent, setSelectedStudent] = useState(null); const getStudents = async () => { try { const { data } = await axiosInstance.get>( `${process.env.REACT_APP_API_URL}/api/v1/users/students`, ); setStudents(data); } catch (e) { console.log(e); } }; const changeSelectedStudent = (studentId: number) => { setSelectedStudent(students.find((student) => student.id === studentId)!); }; useEffect(() => { setTimeout(() => { const userPrivilige = localStorage.getItem('userPrivilige'); userPrivilige === 'DEANERY' && getStudents(); }, 500); }, []); return ( {children} ); };