history first draft
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
import React, { useState, createContext, useEffect, ReactNode } from 'react';
|
||||
import { Course, Group, Basket, GroupType, SchedulerEvent } from '../types';
|
||||
import { Course, Group, Basket, GroupType, SchedulerEvent, TimetableHistory } from '../types';
|
||||
import { useSnackbar } from 'notistack';
|
||||
import { axiosInstance } from '../utils/axiosInstance';
|
||||
import CloseIcon from '@material-ui/icons/Close';
|
||||
@ -16,6 +16,7 @@ const StyledCloseIcon = styled(CloseIcon)`
|
||||
interface CourseContext {
|
||||
courses: Array<Course>;
|
||||
basket: Array<Basket>;
|
||||
timetableHistory: Array<TimetableHistory>;
|
||||
hoveredGroup: Group | undefined | null;
|
||||
userID: string;
|
||||
isDataLoading: boolean;
|
||||
@ -31,7 +32,9 @@ interface CourseContext {
|
||||
selectBasketCourses: () => Array<Course>;
|
||||
selectBasketCourseGroups: (courseId: number) => { lecture: Group | undefined; classes: Group | undefined };
|
||||
getNewestStudentTimetable: (studentId: string) => void;
|
||||
getStudentTimetablesHistory: (studentId: string) => void;
|
||||
changeDataLoading: (isLoading: boolean) => void;
|
||||
setBasketFromHistoryGroups: (groupsIds: Array<number>) => void;
|
||||
}
|
||||
export const coursesContext = createContext<CourseContext | undefined>(undefined);
|
||||
|
||||
@ -46,6 +49,7 @@ export const CoursesProvider = ({ children }: CoursesProviderProps) => {
|
||||
//fetch courses with groups
|
||||
const [courses, setCourses] = useState<Array<Course>>([]);
|
||||
const [basket, setBasket] = useState<Array<Basket>>([]);
|
||||
const [timetableHistory, setTimetableHistory] = useState<Array<TimetableHistory>>([]);
|
||||
const [userID, setUserID] = useState('');
|
||||
const [hoveredGroup, setHoveredGroup] = useState<Group | undefined | null>(null);
|
||||
const [isDataLoading, setIsDataLoading] = useState(false);
|
||||
@ -70,12 +74,9 @@ export const CoursesProvider = ({ children }: CoursesProviderProps) => {
|
||||
return basket.reduce((res, el) => {
|
||||
const { name } = el;
|
||||
if (el.classes) {
|
||||
console.log('element kurwa is: ', el);
|
||||
res.push({ ...el.classes, name });
|
||||
}
|
||||
if (el.lecture) {
|
||||
console.log('element kurwa is: ', el);
|
||||
|
||||
res.push({ ...el.lecture, name });
|
||||
}
|
||||
return res;
|
||||
@ -111,6 +112,7 @@ export const CoursesProvider = ({ children }: CoursesProviderProps) => {
|
||||
setUserID(studentId);
|
||||
setTimeout(() => {
|
||||
getNewestStudentTimetable(studentId);
|
||||
getStudentTimetablesHistory(studentId);
|
||||
}, 100);
|
||||
};
|
||||
|
||||
@ -179,8 +181,6 @@ export const CoursesProvider = ({ children }: CoursesProviderProps) => {
|
||||
`${process.env.REACT_APP_API_URL}/api/v1/commisions/user/schedule`,
|
||||
);
|
||||
const basket = data === '' ? [] : data;
|
||||
console.log('basket is: ', basket);
|
||||
console.log('mordo weź');
|
||||
setBasket(basket);
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
@ -200,17 +200,57 @@ export const CoursesProvider = ({ children }: CoursesProviderProps) => {
|
||||
}
|
||||
};
|
||||
|
||||
const getStudentTimetablesHistory = async (studentId: string) => {
|
||||
try {
|
||||
const { data } = await axiosInstance.get<Array<TimetableHistory> | []>(
|
||||
`${process.env.REACT_APP_API_URL}/api/v1/commisions/user/${studentId}?groups=true`,
|
||||
);
|
||||
console.log('data is mordo: ', data);
|
||||
setTimetableHistory(data);
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
};
|
||||
|
||||
const fetchCourses = async () => {
|
||||
try {
|
||||
const { data: courses } = await axiosInstance.get<Array<Course>>(
|
||||
`${process.env.REACT_APP_API_URL}/api/v1/courses/all?groups=true&takenPlaces=true`,
|
||||
);
|
||||
const sortedCourses = courses.sort((a, b) => (a.name > b.name ? 1 : -1));
|
||||
console.log('sortedCourses: ', sortedCourses);
|
||||
setCourses(sortedCourses);
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const setBasketFromHistoryGroups = (groupsIds: Array<number>) => {
|
||||
const basket: Array<Basket> = [];
|
||||
for (const groupId of groupsIds) {
|
||||
for (const course of courses) {
|
||||
const { lectures, classes, name, id } = course;
|
||||
let basketElement: Basket = { name: name, id: id };
|
||||
if (lectures) {
|
||||
for (const lecture of lectures) {
|
||||
if (groupId === lecture.id) {
|
||||
basketElement = { ...basketElement, lecture: lecture };
|
||||
}
|
||||
}
|
||||
}
|
||||
if (classes) {
|
||||
for (const singleClass of classes) {
|
||||
if (groupId === singleClass.id) {
|
||||
basketElement = { ...basketElement, classes: singleClass };
|
||||
}
|
||||
}
|
||||
}
|
||||
if (basketElement.classes !== undefined || basketElement.lecture !== undefined) {
|
||||
basket.push(basketElement);
|
||||
}
|
||||
}
|
||||
}
|
||||
console.log('baskeeeeeet: ', basket);
|
||||
setBasket(basket);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
@ -229,6 +269,7 @@ export const CoursesProvider = ({ children }: CoursesProviderProps) => {
|
||||
courses,
|
||||
basket,
|
||||
hoveredGroup,
|
||||
timetableHistory,
|
||||
isDataLoading,
|
||||
addCourseToBasket,
|
||||
changeHoveredGroup,
|
||||
@ -242,6 +283,8 @@ export const CoursesProvider = ({ children }: CoursesProviderProps) => {
|
||||
selectBasketCourseGroups,
|
||||
getNewestStudentTimetable,
|
||||
changeStudent,
|
||||
getStudentTimetablesHistory,
|
||||
setBasketFromHistoryGroups,
|
||||
changeDataLoading,
|
||||
}}
|
||||
>
|
||||
|
@ -1,7 +1,6 @@
|
||||
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<Student>;
|
||||
@ -19,7 +18,9 @@ export const StudentsProvider = ({ children }: StudentsProviderProps) => {
|
||||
const [students, setStudents] = useState<Array<Student>>([]);
|
||||
const [selectedStudent, setSelectedStudent] = useState<Student | null>(null);
|
||||
|
||||
//not working currently
|
||||
useEffect(() => {
|
||||
console.log('selected student: ', selectedStudent);
|
||||
}, [selectedStudent]);
|
||||
|
||||
const getStudents = async () => {
|
||||
try {
|
||||
@ -27,7 +28,6 @@ export const StudentsProvider = ({ children }: StudentsProviderProps) => {
|
||||
`${process.env.REACT_APP_API_URL}/api/v1/users/students`,
|
||||
);
|
||||
setStudents(data);
|
||||
console.log(data);
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
@ -40,7 +40,6 @@ export const StudentsProvider = ({ children }: StudentsProviderProps) => {
|
||||
useEffect(() => {
|
||||
setTimeout(() => {
|
||||
const userPrivilige = localStorage.getItem('userPrivilige');
|
||||
console.log('mordo privilidż: ', userPrivilige);
|
||||
userPrivilige === 'DEANERY' && getStudents();
|
||||
}, 500);
|
||||
}, []);
|
||||
|
Reference in New Issue
Block a user