import { Component , OnInit } from '@angular/core' ; import { CourseService , NotificationSettingsService } from '../../shared/services/data.service' ; import { MatTableDataSource } from '@angular/material' ; import { SnackBarService } from '../../shared/services/snack-bar.service' ; import { SelectionModel } from '@angular/cdk/collections' ; import { INotificationSettingsView , NOTIFICATION_TYPE_ALL_CHANGES , NOTIFICATION_TYPE_NONE } from '../../../../../../shared/models/INotificationSettingsView' ; import { ICourseDashboard } from '../../../../../../shared/models/ICourseDashboard' ; @ Component ( { selector : 'app-user-settings' , templateUrl : './user-settings.component.html' , styleUrls : [ './user-settings.component.scss' ] } ) export class UserSettingsComponent implements OnInit { myCourses : ICourseDashboard [ ] ; displayedColumns = [ 'name' , 'Notifications' , 'email' ] ; dataSource : MatTableDataSource < ICourseDashboard > ; notificationSelection = new SelectionModel < ICourseDashboard > ( true , [ ] ) ; emailSelection = new SelectionModel < ICourseDashboard > ( true , [ ] ) ; notificationSettings : INotificationSettingsView [ ] ; constructor ( private courseService , private notificationSettingsService , private snackBar ) { } async ngOnInit ( ) { await this . getCourses ( ) ; await this . getNotificationSettings ( ) ; this . setSelection ( ) ; } async getCourses ( ) { this . myCourses = [ ] ; try { this . myCourses = ( await this . courseService . readItems < ICourseDashboard > ( ) ) . filter ( ( course ) => course . userIsCourseMember ) ; this . dataSource = new MatTableDataSource < ICourseDashboard > ( this . myCourses ) ; } catch ( err ) { this . snackBar . open ( 'Could not fetch courses' ) ; } } async getNotificationSettings ( ) { this . notificationSettings = await this . notificationSettingsService . getNotificationSettings ( ) ; } setSelection ( ) { if ( ! this . notificationSettings ) { return ; } this . notificationSettings . forEach ( ( setting ) => { const course = this . myCourses . find ( tmp => tmp . _id === setting . course ) ; if ( course === undefined ) { return ; } if ( setting . notificationType === NOTIFICATION_TYPE_ALL_CHANGES ) { this . notificationSelection . select ( course ) ; } if ( setting . emailNotification ) { this . emailSelection . select ( course ) ; } } ) ; } async saveNotificationSettings ( ) { const errors = [ ] ; for ( const course of this . myCourses ) { try { let settings = this . notificationSettings . find ( tmp => tmp . course === course . _id ) ; if ( settings === undefined ) { settings = { course : course . _id , notificationType : NOTIFICATION_TYPE_ALL_CHANGES , emailNotification : false } ; this . notificationSettings . push ( settings ) ; } settings . notificationType = this . notificationSelection . isSelected ( course ) ? NOTIFICATION_TYPE_ALL_CHANGES : NOTIFICATION_TYPE_NONE ; settings . emailNotification = this . emailSelection . isSelected ( course ) ; await this . notificationSettingsService . setNotificationSettings ( settings ) ; } catch ( err ) { if ( errors . indexOf ( err . error . message ) === - 1 ) { errors . push ( err . error . message ) ; } } } if ( errors . length === 0 ) { this . snackBar . open ( 'Notification settings updated successfully.' ) ; } else { this . snackBar . openLong ( 'Failed to save notification settings: ' + errors . join ( ', ' ) ) ; } } isAllSelected ( selectionModel ) { return selectionModel . selected . length === this . dataSource . data . length ; } masterToggle ( selectionModel ) { this . isAllSelected ( selectionModel ) ? selectionModel . clear ( ) : this . dataSource . data . forEach ( row => selectionModel . select ( row ) ) ; } }