From 54027d19805ee64d97fe683a91fb67e1e44aa026 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciek=20G=C5=82owacki?= Date: Sat, 12 Dec 2020 20:32:09 +0100 Subject: [PATCH] admin panel changes --- src/components/Admin.tsx | 4 ++-- src/components/Dropdown.tsx | 8 +++++--- src/components/Scheduler.tsx | 4 ++-- src/components/SchedulerRow.tsx | 1 - src/contexts/CoursesProvider.tsx | 28 +++++++++++++++++++--------- src/contexts/StudentsProvider.tsx | 8 ++++++-- 6 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/components/Admin.tsx b/src/components/Admin.tsx index 632a24a..51cdd22 100644 --- a/src/components/Admin.tsx +++ b/src/components/Admin.tsx @@ -12,7 +12,7 @@ const LeftSide = styled.div` flex: 1; flex-direction: column; background-color: white; - text-align:center; + text-align: center; `; const Wrap = styled.div` @@ -90,7 +90,7 @@ const Icon = styled.img` `; export const Admin = () => { - const [currentTab, setCurrentTab] = useState(null); + const [currentTab, setCurrentTab] = useState(1); const handleClick = (e: MouseEvent) => { setCurrentTab(Number(e.currentTarget.id)); diff --git a/src/components/Dropdown.tsx b/src/components/Dropdown.tsx index f5c2411..d9fc7a7 100644 --- a/src/components/Dropdown.tsx +++ b/src/components/Dropdown.tsx @@ -50,7 +50,9 @@ interface DropdownProps { } export const Dropdown = ({ open, input, handleCloseDropdown, selectedOption }: DropdownProps) => { - const { courses, selectBasketNames, addCourseToBasket, getUserID } = useContext(coursesContext)!; + const { courses, selectBasketNames, addCourseToBasket, changeStudent } = useContext( + coursesContext, + )!; const { students } = useContext(studentsContext)!; const basketNames = useMemo(() => selectBasketNames(), [selectBasketNames]); const [filteredCourses, setFilteredCourses] = useState>([]); @@ -67,7 +69,7 @@ export const Dropdown = ({ open, input, handleCloseDropdown, selectedOption }: D const onUserClick = (event: MouseEvent) => { const target = event.currentTarget; - getUserID(target.id); + changeStudent(target.id); handleCloseDropdown(); }; @@ -119,7 +121,7 @@ export const Dropdown = ({ open, input, handleCloseDropdown, selectedOption }: D {filteredStudents.map(({ name, surname, id }, index) => (

- {name} {surname}{' '} + {name} {surname}

))} diff --git a/src/components/Scheduler.tsx b/src/components/Scheduler.tsx index e811a0b..2996e45 100644 --- a/src/components/Scheduler.tsx +++ b/src/components/Scheduler.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useRef } from 'react'; +import React, { useEffect, useLayoutEffect, useRef } from 'react'; import { useState } from 'react'; import { SchedulerEvents } from './SchedulerEvents'; import { days, hours } from '../constants/index'; @@ -66,7 +66,7 @@ export const Scheduler = () => { const [cellWidth, setCellWidth] = useState(0); const [cellHeight, setCellHeight] = useState(0); - useEffect(() => { + useLayoutEffect(() => { const handleResize = () => { if (cellRef.current) { setCellWidth(cellRef.current.getBoundingClientRect().width); diff --git a/src/components/SchedulerRow.tsx b/src/components/SchedulerRow.tsx index ccf7329..68e279c 100644 --- a/src/components/SchedulerRow.tsx +++ b/src/components/SchedulerRow.tsx @@ -140,7 +140,6 @@ export const SchedulerRow = ({ groups, indexRow, rowTop, cellWidth, cellHeight } const [popoverId, setPopoverId] = useState(null); //looks weird const handlePopoverOpen = (event: MouseEvent) => { - console.log('I was clicked!!!!'); setAnchorEl(event.currentTarget); setPopoverId(event.currentTarget.id); }; diff --git a/src/contexts/CoursesProvider.tsx b/src/contexts/CoursesProvider.tsx index c169875..52b9f22 100644 --- a/src/contexts/CoursesProvider.tsx +++ b/src/contexts/CoursesProvider.tsx @@ -26,12 +26,13 @@ interface CourseContext { restoreGroupInBasket: (restoreGroup: Group, courseId: number) => void; deleteFromBasket: (id: number) => void; saveBasket: (userID: string) => Promise; - getUserID: (userID: string) => Promise; + changeStudent: (studentId: string) => void; selectSchedulerEvents: () => Array; selectBasketNames: () => Array; selectBasketCourses: () => Array; selectBasketCourseGroups: (courseId: number) => { lecture: Group | undefined; classes: Group | undefined }; - getUserTimetable: (userID: string) => Promise; + getNewestStudentTimetable: (studentId: string) => void; + changeDataLoading: (isLoading: boolean) => void; } export const coursesContext = createContext(undefined); @@ -92,6 +93,8 @@ export const CoursesProvider = ({ children }: CoursesProviderProps) => { const changeHoveredGroup = (group: Group | null) => setHoveredGroup(group); + const changeDataLoading = (isLoading: boolean) => setIsDataLoading(isLoading); + const addCourseToBasket = (course: Course) => { const courseToBasket: Basket = { name: course.name, @@ -104,8 +107,11 @@ export const CoursesProvider = ({ children }: CoursesProviderProps) => { const deleteFromBasket = (id: number) => setBasket(basket.filter((course) => course.id !== id)); - const getUserID = async (userID: string) => { - setUserID(userID); + const changeStudent = async (studentId: string) => { + setUserID(studentId); + setTimeout(() => { + getNewestStudentTimetable(studentId); + }, 100); }; const saveBasket = async (userID: string) => { @@ -177,10 +183,13 @@ export const CoursesProvider = ({ children }: CoursesProviderProps) => { } }; - const getUserTimetable = async (userID: string) => { + const getNewestStudentTimetable = async (studentId: string) => { try { - const { data } = await axiosInstance.get(`${process.env.REACT_APP_API_URL}/api/v1/commisions/user/${userID}`); - console.log(data); + const { data } = await axiosInstance.get( + `${process.env.REACT_APP_API_URL}/api/v1/commisions/user/${studentId}/schedule`, + ); + const basket = data === '' ? [] : data; + setBasket(basket); } catch (e) { console.log(e); } @@ -225,8 +234,9 @@ export const CoursesProvider = ({ children }: CoursesProviderProps) => { selectBasketNames, selectBasketCourses, selectBasketCourseGroups, - getUserTimetable, - getUserID, + getNewestStudentTimetable, + changeStudent, + changeDataLoading, }} > {children} diff --git a/src/contexts/StudentsProvider.tsx b/src/contexts/StudentsProvider.tsx index 7990a2d..33b8cc1 100644 --- a/src/contexts/StudentsProvider.tsx +++ b/src/contexts/StudentsProvider.tsx @@ -1,6 +1,7 @@ -import React, { useState, createContext, useEffect, ReactNode, useRef } from 'react'; +import React, { useState, createContext, useEffect, ReactNode, useRef, useContext } from 'react'; import { Student } from '../types'; import { axiosInstance } from '../utils/axiosInstance'; +import { CASContext } from './CASProvider'; interface StudentContext { students: Array; @@ -15,7 +16,9 @@ interface StudentsProviderProps { export const StudentsProvider = ({ children }: StudentsProviderProps) => { const [students, setStudents] = useState>([]); + //not working currently const userPrivilige = localStorage.getItem('userPrivilige'); + const { user } = useContext(CASContext)!; const getStudents = async () => { try { @@ -31,7 +34,8 @@ export const StudentsProvider = ({ children }: StudentsProviderProps) => { useEffect(() => { setTimeout(() => { - userPrivilige === 'DEANERY' && getStudents(); + // user?.authorityRole === 'DEANERY' && + getStudents(); }, 500); }, []);