]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/local/course-reserves/course-page.component.ts
LP 1849212: Add course term functionality
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / local / course-reserves / course-page.component.ts
1 import {Component, ViewChild, OnInit} from '@angular/core';
2 import {ActivatedRoute} from '@angular/router';
3 import {PcrudService} from '@eg/core/pcrud.service';
4 import {IdlObject, IdlService} from '@eg/core/idl.service';
5 import {GridDataSource} from '@eg/share/grid/grid';
6 import {StringComponent} from '@eg/share/string/string.component';
7 import {ToastService} from '@eg/share/toast/toast.service';
8 import {CourseService} from '@eg/staff/share/course.service';
9 import {CourseAssociateUsersComponent} from './course-associate-users.component';
10 import {CourseAssociateMaterialComponent} from './course-associate-material.component';
11 import {Pager} from '@eg/share/util/pager';
12
13 @Component({
14     selector: 'eg-course-page',
15     templateUrl: './course-page.component.html'
16 })
17
18 export class CoursePageComponent implements OnInit {
19
20     currentCourse: IdlObject;
21     courseId: any;
22
23     // Materials Tab
24     @ViewChild('courseMaterialDialog', {static: true})
25         private courseMaterialDialog: CourseAssociateMaterialComponent;
26     @ViewChild('courseUserDialog', {static: true})
27         private courseUserDialog: CourseAssociateUsersComponent;
28
29     // Edit Tab
30     @ViewChild('archiveFailedString', { static: true })
31         archiveFailedString: StringComponent;
32     @ViewChild('archiveSuccessString', { static: true })
33         archiveSuccessString: StringComponent;
34
35     // Course Tab
36     termsDataSource: GridDataSource = new GridDataSource();
37     defaultNewAcmtcm: IdlObject;
38
39     constructor(
40         private course: CourseService,
41         private idl: IdlService,
42         private pcrud: PcrudService,
43         private route: ActivatedRoute,
44         private toast: ToastService
45     ) {
46     }
47
48     ngOnInit() {
49         this.courseId = +this.route.snapshot.paramMap.get('id');
50         this.course.getCourses([this.courseId]).then(course => {
51             this.currentCourse = course[0];
52         });
53
54         this.defaultNewAcmtcm = this.idl.create('acmtcm');
55         this.defaultNewAcmtcm.course(this.courseId);
56
57         this.termsDataSource.getRows = (pager: Pager, sort: any[]) => {
58             const orderBy: any = {};
59             if (sort.length) {
60                 orderBy.acmtcm = sort[0].name + ' ' + sort[0].dir;
61             }
62             const searchOps = {
63                 offset: pager.offset,
64                 limit: pager.limit,
65                 order_by: orderBy
66             };
67
68             return this.pcrud.search('acmtcm', {course: this.courseId},
69                 searchOps, {fleshSelectors: true});
70         };
71     }
72
73     // Edit Tab
74     archiveCourse() {
75         this.course.disassociateMaterials([this.currentCourse]).then(res => {
76             this.currentCourse.is_archived('t');
77             this.pcrud.update(this.currentCourse).subscribe(val => {
78                 console.debug('archived: ' + val);
79                 this.archiveSuccessString.current()
80                     .then(str => this.toast.success(str));
81             }, err => {
82                 this.archiveFailedString.current()
83                     .then(str => this.toast.danger(str));
84             });
85         });
86     }
87
88     // Materials Tab
89
90 }