2020-08-12 20:52:53 +02:00
|
|
|
import React, { useState, createContext, useEffect } from 'react';
|
2020-08-23 16:02:52 +02:00
|
|
|
import { Course, Group, CourseBasket } from '../types';
|
2020-08-17 23:56:34 +02:00
|
|
|
import axios from 'axios';
|
2020-08-23 16:02:52 +02:00
|
|
|
|
2020-08-17 23:56:34 +02:00
|
|
|
interface CourseContext {
|
2020-08-12 20:52:53 +02:00
|
|
|
courses: Array<Course>;
|
2020-08-17 23:56:34 +02:00
|
|
|
choosenCourses: Array<Course>;
|
2020-08-12 20:52:53 +02:00
|
|
|
choosenGroups: Array<Group>;
|
2020-08-23 16:02:52 +02:00
|
|
|
coursesBasket: Array<CourseBasket>;
|
2020-08-17 23:56:34 +02:00
|
|
|
addChoosenCourse: (courses: Course) => void;
|
2020-08-23 16:02:52 +02:00
|
|
|
addChoosenGroup: (group: Group, id: number) => void;
|
2020-08-12 20:52:53 +02:00
|
|
|
}
|
2020-08-17 23:56:34 +02:00
|
|
|
export const coursesContext = createContext<CourseContext | null>(null);
|
2020-08-12 20:52:53 +02:00
|
|
|
|
|
|
|
interface CoursesProviderProps {
|
|
|
|
children: React.ReactNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const CoursesProvider = ({ children }: CoursesProviderProps) => {
|
2020-08-23 16:02:52 +02:00
|
|
|
//fetch courses with groups
|
2020-08-12 20:52:53 +02:00
|
|
|
const [courses, setCourses] = useState<Array<Course>>([]);
|
2020-08-23 16:02:52 +02:00
|
|
|
const [coursesBasket, setCoursesBasket] = useState<Array<CourseBasket>>([]);
|
|
|
|
//with groups
|
2020-08-17 23:56:34 +02:00
|
|
|
const [choosenCourses, setChoosenCourses] = useState<Array<Course>>([]);
|
2020-08-12 20:52:53 +02:00
|
|
|
const [choosenGroups, setChoosenGroups] = useState<Array<Group>>([]);
|
|
|
|
|
2020-08-17 23:56:34 +02:00
|
|
|
const addChoosenCourse = (choosenCourse: Course) => {
|
|
|
|
setChoosenCourses([...choosenCourses, choosenCourse]);
|
2020-08-12 20:52:53 +02:00
|
|
|
};
|
2020-08-23 16:02:52 +02:00
|
|
|
const addChoosenGroup = (choosenGroup: Group, id: number) => {
|
|
|
|
//move to utilities
|
|
|
|
//change type to group type from api
|
|
|
|
function hasChoosenGroupType(type: string) {
|
|
|
|
const res = choosenCourse.groups!.find((group) => group.type === type);
|
|
|
|
console.log(`res is: ${JSON.stringify(res)}`);
|
|
|
|
return !!res;
|
|
|
|
}
|
|
|
|
|
|
|
|
const choosenCourse = choosenCourses.find((cc) => cc.id === id)!;
|
|
|
|
|
|
|
|
const type = choosenGroup.type;
|
|
|
|
console.log(`group type is: ${type}`);
|
|
|
|
if (hasChoosenGroupType(type)) {
|
|
|
|
const group = choosenCourse.groups!.find((group) => group.type === type);
|
|
|
|
console.log(`group is: ${JSON.stringify(group)}`);
|
|
|
|
console.log(choosenCourse.groups);
|
|
|
|
if (group) {
|
|
|
|
group.type = type;
|
|
|
|
}
|
|
|
|
setChoosenGroups([...choosenGroups, choosenGroup]);
|
|
|
|
} else {
|
|
|
|
console.log('pierwsza grupa/wykłąd');
|
|
|
|
//może być złe
|
|
|
|
choosenCourse.groups!.push(choosenGroup);
|
|
|
|
// setChoosenCourses(course => course.id === choosenCourse.id)
|
|
|
|
setChoosenGroups([...choosenGroups, choosenGroup]);
|
|
|
|
}
|
|
|
|
console.log(`choosen courses after changing group: ${JSON.stringify(choosenCourses)}`);
|
|
|
|
|
|
|
|
console.log(choosenCourse);
|
2020-08-12 20:52:53 +02:00
|
|
|
};
|
|
|
|
|
2020-08-17 23:56:34 +02:00
|
|
|
useEffect(() => {
|
|
|
|
const fetchData = async () => {
|
2020-08-23 16:02:52 +02:00
|
|
|
const { data: courses } = await axios.get(`${process.env.REACT_APP_API_URL}/getCourses`);
|
|
|
|
|
|
|
|
for (const course of courses) {
|
|
|
|
const { data: groups } = await axios.get(`${process.env.REACT_APP_API_URL}/getCourseGroups?id=${course.id}`);
|
|
|
|
//porozmawiać z Filipem, żeby odrobinę przerobił endpoint
|
|
|
|
course.groups = groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
setCourses(courses);
|
2020-08-17 23:56:34 +02:00
|
|
|
};
|
|
|
|
fetchData();
|
|
|
|
}, []);
|
|
|
|
|
2020-08-12 20:52:53 +02:00
|
|
|
return (
|
2020-08-17 23:56:34 +02:00
|
|
|
<coursesContext.Provider value={{ courses, choosenGroups, choosenCourses, addChoosenCourse, addChoosenGroup }}>
|
2020-08-12 20:52:53 +02:00
|
|
|
{children}
|
|
|
|
</coursesContext.Provider>
|
|
|
|
);
|
|
|
|
};
|