Skip to content

Commit

Permalink
libraries: add notification settings
Browse files Browse the repository at this point in the history
* Improve library editor appearance for large screens. It doesn't take
the full width anymore.
* Add notifications settings in library custom editor.
* Displays notification settings in library detail view.
* Closes rero/rero-ils#913.

Co-Authored-by: Alicia Zangger <alicia.zangger@rero.ch>
  • Loading branch information
Alicia Zangger committed Feb 22, 2021
1 parent 2475d82 commit 791322d
Show file tree
Hide file tree
Showing 6 changed files with 439 additions and 194 deletions.
9 changes: 9 additions & 0 deletions projects/admin/src/app/class/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ export interface ExceptionDates {
repeat?: Repeat;
}

export interface NotificationSettings {
type: string;
email: string;
template: string;
delay_availability?: number;
language?: string;
}

export class Library {

// CLASS ATTRIBUTES ================================================
Expand All @@ -63,6 +71,7 @@ export class Library {
code: string = null;
opening_hours: Array<OpeningHours> = [];
exception_dates?: Array<ExceptionDates>;
notification_settings?: Array<NotificationSettings>;
organisation: Organisation;

// GETTER & SETTER ================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@

import { Injectable } from '@angular/core';
import { FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms';
import { TimeValidator } from '@rero/ng-core';
import { RecordService, TimeValidator } from '@rero/ng-core';
import { WeekDays } from '../../../class/week-days';

import { Library } from '../../../class/library';
import { Library, NotificationSettings } from '../../../class/library';
import { Subject } from 'rxjs';

@Injectable({
providedIn: 'root'
Expand All @@ -29,14 +30,22 @@ export class LibraryFormService {

public form;


private notificationTypes = [];

/** Observable for build event */
private buildEvent = new Subject();

constructor(
private fb: FormBuilder
) {
this.build();
}
private _fb: FormBuilder,
private _recordService: RecordService
) { }

/**
* Build form
*/
build() {
this.form = this.fb.group({
this.form = this._fb.group({
name: ['', [
Validators.required,
Validators.minLength(4)
Expand All @@ -48,24 +57,51 @@ export class LibraryFormService {
Validators.required
]
}],
opening_hours: this.fb.array([])
opening_hours: this._fb.array([]),
notification_settings: this._fb.array([])
});
this.initializeOpeningHours();
this.initializeNotificationSettings();
}

/**
* Get build event
*/
getBuildEvent() {
return this.buildEvent.asObservable();
}

create() {
this._recordService
.getSchemaForm('notifications')
.subscribe((jsonSchema: any) => {
this.notificationTypes = jsonSchema.schema.properties.notification_type.enum;
this.build();
this.buildEvent.next(true);
});
}

/**
* Build and set default values for opening hours at form initialization
* @param openingHours - opening hours
*/
initializeOpeningHours(openingHours = []) {
const days = Object.keys(WeekDays);
const hours = this.form.get('opening_hours');
for (let step = 0; step < 7; step++) {
hours.push(this.buildOpeningHours(
false,
days[step],
this.fb.array([])
this._fb.array([])
));
}
this.setOpeningHours(openingHours);
}

/**
* Set opening hours from record data
* @param openingHours - opening hours
*/
setOpeningHours(openingHours = []) {
for (let step = 0; step < 7; step++) {
const atimes = this.getTimesByDayIndex(step);
Expand All @@ -85,8 +121,14 @@ export class LibraryFormService {
}
}

/**
* Create opening hour form control
* @param isOpen - is open
* @param day - day
* @param times - times array
*/
buildOpeningHours(isOpen, day, times): FormGroup {
return this.fb.group({
return this._fb.group({
is_open: [isOpen],
day: [day],
times
Expand All @@ -95,9 +137,14 @@ export class LibraryFormService {
});
}

/**
* Create times form control
* @param startTime - start time
* @param endTime - end time
*/
buildTimes(startTime = '00:01', endTime = '23:59'): FormGroup {
const regex = '^(?!(0:00)|(00:00)$)([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$';
return this.fb.group({
return this._fb.group({
start_time: [startTime, {
validators: [
Validators.required,
Expand All @@ -115,10 +162,10 @@ export class LibraryFormService {
});
}

reset() {
this.build();
}

/**
* Populate the form
* @param library - library
*/
populate(library: Library) {
this.form.patchValue({
name: library.name,
Expand All @@ -127,6 +174,66 @@ export class LibraryFormService {
code: library.code,
});
this.setOpeningHours(library.opening_hours);
this.setNotificationSettings(library.notification_settings);
}

/**
* Build an set default values for notification settings
* @param notificationSettings - notification settings
*/
initializeNotificationSettings(notificationSettings = []) {
const settings = this.form.get('notification_settings');
this.notificationTypes.forEach(type => {
settings.push(this.getSettingsByType(type));
});
this.setNotificationSettings(notificationSettings);
}

/**
* Get setting by type
* @param settingType - setting type
*/
getSettingsByType(settingType: string) {
const model: NotificationSettings = {
type: settingType,
email: '',
template: ''
};
switch (settingType) {
case('availability'):
model.delay_availability = 0;
break;
case('booking'):
case('transit_notice'):
case('request'):
model.language = '';
break;
}
return this._fb.group(model);
}

/**
* Set values from record
* @param notificationSettings - notification settings
*/
setNotificationSettings(notificationSettings = []) {
if (notificationSettings.length > 0) {
const formSettings = this.form.get('notification_settings');
for (let step = 0; step < formSettings.value.length; step++) {
const formSetting = formSettings.get(String(step));
const currentSetting = notificationSettings.find(element => element.type === formSetting.get('type').value);
if (currentSetting !== undefined) {
formSetting.get('email').setValue(currentSetting.email);
formSetting.get('template').setValue(currentSetting.template);
if (currentSetting.language !== undefined) {
formSetting.get('language').setValue(currentSetting.language);
}
if (currentSetting.delay_availability !== undefined) {
formSetting.get('delay_availability').setValue(currentSetting.delay_availability);
}
}
}
}
}

setId(id) { this.form.value.id = id; }
Expand All @@ -140,6 +247,9 @@ export class LibraryFormService {
get opening_hours() {
return this.form.get('opening_hours') as FormArray;
}
get notification_settings() {
return this.form.get('notification_settings') as FormArray;
}

getValues() { return this.form.value; }

Expand Down
Loading

0 comments on commit 791322d

Please sign in to comment.