1 | import {Component, Input, OnInit} from '@angular/core';
|
---|
2 | import {ActiveUserInterface} from "../ActiveUserInterface";
|
---|
3 | import {LoginService} from "../login.service";
|
---|
4 | import {CourseInterface} from "../courseInterface";
|
---|
5 | import {CourseService} from "../course.service";
|
---|
6 | import {AddStudentCoursesService} from "../add-student-courses.service";
|
---|
7 | import {Router} from "@angular/router";
|
---|
8 |
|
---|
9 | @Component({
|
---|
10 | selector: 'app-all-courses',
|
---|
11 | templateUrl: './all-courses.component.html',
|
---|
12 | styleUrls: ['./all-courses.component.css']
|
---|
13 | })
|
---|
14 | export class AllCoursesComponent implements OnInit {
|
---|
15 |
|
---|
16 | courses: CourseInterface[] = [];
|
---|
17 |
|
---|
18 | selectedCourses = [];
|
---|
19 | activeUser: ActiveUserInterface | undefined;
|
---|
20 | @Input() subjectId: number | undefined;
|
---|
21 |
|
---|
22 | constructor(private service: CourseService,
|
---|
23 | private addService: AddStudentCoursesService,
|
---|
24 | private loginService: LoginService,
|
---|
25 | private router: Router) {
|
---|
26 | }
|
---|
27 |
|
---|
28 | ngOnInit(): void {
|
---|
29 | this.service.getAllCourses().subscribe(courses => this.courses = courses)
|
---|
30 | this.activeUser = this.loginService.activeUser!!;
|
---|
31 | if (!this.activeUser) {
|
---|
32 | this.router.navigate(['/'])
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | loadCourses(subjectId: number) {
|
---|
37 | this.service.findCoursesBySubjectId(subjectId).subscribe(courses => this.courses = courses);
|
---|
38 | }
|
---|
39 |
|
---|
40 | checked({id}: { id: any }) {
|
---|
41 | // @ts-ignore
|
---|
42 | if (this.selectedCourses.indexOf(id) != -1) {
|
---|
43 | return true;
|
---|
44 | }
|
---|
45 | return false;
|
---|
46 | }
|
---|
47 |
|
---|
48 | onChange({event, id}: { event: any, id: any }) {
|
---|
49 | if (event.target.checked) {
|
---|
50 | // @ts-ignore
|
---|
51 | this.selectedCourses.push(id.id);
|
---|
52 | } else {
|
---|
53 | // @ts-ignore
|
---|
54 | this.selectedCourses.splice(this.selectedCourses.indexOf(id.id), 1)
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | submit() {
|
---|
59 | this.activeUser = this.loginService.activeUser!!;
|
---|
60 | this.selectedCourses = this.selectedCourses.sort()
|
---|
61 | if (this.loginService.activeUser!!.userType == 'STUDENT') {
|
---|
62 | for (let course of this.selectedCourses) {
|
---|
63 | this.addService.addCourseToStudent(this.activeUser.username, +course)
|
---|
64 | .subscribe(() => this.router.navigate(['/courses']));
|
---|
65 | }
|
---|
66 | }
|
---|
67 | if (this.loginService.activeUser!!.userType == 'PROFESSOR') {
|
---|
68 | for (let course of this.selectedCourses) {
|
---|
69 | this.addService.addCourseToProfessor(this.activeUser.username, +course)
|
---|
70 | .subscribe(() => this.router.navigate(['/courses']));
|
---|
71 | }
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|