2022-04-11 11:27:10 +02:00
|
|
|
import {ChangeDetectorRef, Component, Input, OnDestroy, OnInit, ViewChild, ViewEncapsulation} from "@angular/core";
|
2022-09-23 12:25:08 +02:00
|
|
|
import {UntypedFormArray, UntypedFormBuilder, UntypedFormGroup} from "@angular/forms";
|
2022-08-05 13:59:08 +02:00
|
|
|
import {Role, User} from "../../login/utils/helper.class";
|
2021-03-04 12:32:57 +01:00
|
|
|
import {UserManagementService} from "../../services/user-management.service";
|
2022-04-11 11:27:10 +02:00
|
|
|
import {Subscription} from "rxjs";
|
2021-03-04 12:32:57 +01:00
|
|
|
import {NotificationService} from "../notification.service";
|
|
|
|
import {Notification} from "../notifications";
|
|
|
|
import {InputComponent, Option} from "../../sharedComponents/input/input.component";
|
2022-07-17 15:01:39 +02:00
|
|
|
import {NotificationHandler} from "../../utils/notification-handler";
|
2021-03-04 12:32:57 +01:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: '[notify-form]',
|
|
|
|
template: `
|
|
|
|
<form *ngIf="user && form" [formGroup]="form">
|
|
|
|
<ng-template [ngIf]="form.get('notify')">
|
2022-06-16 16:24:36 +02:00
|
|
|
<label><input name="notify" type="checkbox" class="uk-checkbox" formControlName="notify"><span class="uk-margin-small-left">{{label}}</span></label>
|
2022-04-11 11:27:10 +02:00
|
|
|
<div [class.uk-hidden]="!form.get('notify').value" class="uk-position-relative uk-margin-medium-top">
|
|
|
|
<div class="notification-user">
|
2021-03-04 12:32:57 +01:00
|
|
|
<notification-user [name]="user.firstname" [surname]="user.lastname"></notification-user>
|
|
|
|
</div>
|
2022-04-11 11:27:10 +02:00
|
|
|
<div input [rows]="4" [formInput]="form.get('message')" placeholder="Send a notification" type="textarea"></div>
|
2021-03-04 12:32:57 +01:00
|
|
|
</div>
|
|
|
|
</ng-template>
|
|
|
|
<ng-template [ngIf]="form.get('groups') && availableGroups">
|
2022-08-05 13:59:08 +02:00
|
|
|
<div #recipients input type="chips" [options]="groups"
|
2022-04-11 11:27:10 +02:00
|
|
|
placeholder="Sent to:" hint="Add a recipient" inputClass="recipients" [formInput]="form.get('groups')">
|
|
|
|
</div>
|
|
|
|
<div class="uk-position-relative uk-margin-medium-top">
|
|
|
|
<div class="notification-user">
|
2021-03-04 12:32:57 +01:00
|
|
|
<notification-user [name]="user.firstname" [surname]="user.lastname"></notification-user>
|
|
|
|
</div>
|
2022-04-11 11:27:10 +02:00
|
|
|
<div input [formInput]="form.get('message')"
|
|
|
|
[rows]="4" placeholder="Send a notification"
|
|
|
|
type="textarea" inputClass="flat">
|
|
|
|
<div tools class="uk-margin-top uk-width-1-1 uk-flex uk-flex-right">
|
|
|
|
<button (click)="sendNotification()" class="uk-button uk-button-small uk-button-primary"
|
|
|
|
[class.uk-disabled]="sending || !message" [disabled]="sending || !message">Send</button>
|
2021-03-04 12:32:57 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</ng-template>
|
|
|
|
</form>
|
2022-04-11 11:27:10 +02:00
|
|
|
`,
|
2022-07-17 15:01:39 +02:00
|
|
|
styleUrls: ['notify-form.component.less'],
|
2022-04-11 11:27:10 +02:00
|
|
|
encapsulation: ViewEncapsulation.None
|
2021-03-04 12:32:57 +01:00
|
|
|
})
|
|
|
|
export class NotifyFormComponent implements OnInit, OnDestroy {
|
|
|
|
@Input()
|
|
|
|
public label: string = 'Notify Managers';
|
2022-09-23 12:25:08 +02:00
|
|
|
public form: UntypedFormGroup;
|
2021-03-04 12:32:57 +01:00
|
|
|
@Input()
|
|
|
|
public availableGroups: Option[] = null;
|
2022-08-05 13:59:08 +02:00
|
|
|
public groups: Option[] = [];
|
2021-03-04 12:32:57 +01:00
|
|
|
@Input() service: string;
|
2022-08-05 13:59:08 +02:00
|
|
|
public types: string[] = [];
|
2021-03-04 12:32:57 +01:00
|
|
|
public user: User;
|
|
|
|
public focused: boolean = false;
|
2021-07-14 13:19:57 +02:00
|
|
|
@ViewChild('recipients', { static: false }) recipients: InputComponent;
|
2021-03-04 12:32:57 +01:00
|
|
|
private notification: Notification;
|
|
|
|
private subscriptions: any[] = [];
|
|
|
|
public sending: boolean = false;
|
|
|
|
|
2022-09-23 12:25:08 +02:00
|
|
|
constructor(private fb: UntypedFormBuilder,
|
2021-03-04 12:32:57 +01:00
|
|
|
private cdr: ChangeDetectorRef,
|
|
|
|
private userManagementService: UserManagementService,
|
|
|
|
private notificationService: NotificationService) {
|
|
|
|
}
|
|
|
|
|
2022-08-05 13:59:08 +02:00
|
|
|
@Input()
|
|
|
|
set entities(entities: string[]) {
|
|
|
|
this.types = entities.map(entity => Role.typeMember(entity));
|
|
|
|
this.types = this.types.concat(entities.map(entity => Role.typeManager(entity)));
|
|
|
|
}
|
|
|
|
|
2021-03-04 12:32:57 +01:00
|
|
|
ngOnInit() {
|
|
|
|
this.reset();
|
|
|
|
this.subscriptions.push(this.userManagementService.getUserInfo().subscribe(user => {
|
|
|
|
this.user = user;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
this.subscriptions.forEach(subscription => {
|
|
|
|
if (subscription instanceof Subscription) {
|
|
|
|
subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
reset(message: string = null) {
|
|
|
|
if (!this.availableGroups) {
|
|
|
|
this.form = this.fb.group({
|
2022-07-25 14:39:29 +02:00
|
|
|
notify: this.fb.control(false),
|
2021-03-04 12:32:57 +01:00
|
|
|
message: this.fb.control(message)
|
|
|
|
});
|
|
|
|
this.subscriptions.push(this.form.get('notify').valueChanges.subscribe(value => {
|
|
|
|
if (value) {
|
|
|
|
this.form.get('message').markAsUntouched();
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
this.form = this.fb.group({
|
|
|
|
groups: this.fb.array([]),
|
|
|
|
message: this.fb.control(message)
|
|
|
|
});
|
2022-08-05 13:59:08 +02:00
|
|
|
this.groups = this.availableGroups;
|
2021-03-04 12:32:57 +01:00
|
|
|
this.subscriptions.push(this.form.get('groups').valueChanges.subscribe(value => {
|
2022-08-05 13:59:08 +02:00
|
|
|
let typeGroups = this.typeGroups;
|
|
|
|
this.groups = this.availableGroups.map(group => {
|
|
|
|
group.hidden = typeGroups.includes(group.value)
|
|
|
|
return group;
|
2021-03-04 12:32:57 +01:00
|
|
|
});
|
|
|
|
this.cdr.detectChanges();
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sendNotification(notification: Notification = null) {
|
|
|
|
if (this.message) {
|
|
|
|
if(notification === null) {
|
|
|
|
notification = new Notification('CUSTOM', [this.service], null, null);
|
2022-08-05 13:59:08 +02:00
|
|
|
notification.groups = this.parseGroups();
|
2021-03-04 12:32:57 +01:00
|
|
|
this.sending = true;
|
|
|
|
}
|
|
|
|
this.notification = notification;
|
|
|
|
this.notification.message = this.form.value.message;
|
|
|
|
// TODO remove
|
|
|
|
this.notification.name = this.user.firstname;
|
|
|
|
this.notification.surname = this.user.lastname;
|
2022-08-05 13:59:08 +02:00
|
|
|
this.subscriptions.push(this.notificationService.sendNotification(this.notification).subscribe(() => {
|
2021-03-04 12:32:57 +01:00
|
|
|
this.sending = false;
|
2022-07-17 15:01:39 +02:00
|
|
|
NotificationHandler.rise('A notification has been <b>sent</b> successfully');
|
2021-03-04 12:32:57 +01:00
|
|
|
this.reset();
|
|
|
|
}, error => {
|
|
|
|
this.sending = false;
|
2022-07-17 15:01:39 +02:00
|
|
|
NotificationHandler.rise('An error has occurred. Please try again later', 'danger');
|
2021-03-04 12:32:57 +01:00
|
|
|
this.reset();
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-05 13:59:08 +02:00
|
|
|
parseGroups(): string[] {
|
|
|
|
let typeGroups = this.typeGroups;
|
|
|
|
return this.groupsAsFromArray.getRawValue().filter(group => !this.types.includes(group) && !typeGroups.includes(group)).concat(typeGroups);
|
|
|
|
}
|
|
|
|
|
|
|
|
get typeGroups(): string[] {
|
|
|
|
let groups: string[] = [];
|
|
|
|
this.groupsAsFromArray.getRawValue().filter(group => this.types.includes(group)).forEach(roleType => {
|
|
|
|
if(roleType.includes("MANAGER_")) {
|
|
|
|
let type = roleType.replace("MANAGER_", "");
|
|
|
|
groups = groups.concat(this.availableGroups.map(option => option.value).filter(group => group.includes(type) && group.includes("_MANAGER")));
|
|
|
|
} else {
|
|
|
|
groups = groups.concat(this.availableGroups.map(option => option.value).filter(group => group.includes(roleType + "_") && !group.includes("_MANAGER") && !group.includes("MANAGER_")));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return groups;
|
|
|
|
}
|
|
|
|
|
2022-09-23 12:25:08 +02:00
|
|
|
get groupsAsFromArray(): UntypedFormArray {
|
|
|
|
return this.form.get('groups')?(<UntypedFormArray>this.form.get('groups')):null;
|
2021-03-04 12:32:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
get message(): string {
|
|
|
|
if ((this.form.get('notify') && !this.form.get('notify').value) || (this.groupsAsFromArray && this.groupsAsFromArray.length === 0)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return this.form.get('message').value;
|
|
|
|
}
|
|
|
|
|
|
|
|
onFocus(event: boolean) {
|
|
|
|
this.focused = event;
|
|
|
|
}
|
|
|
|
|
|
|
|
focus(event) {
|
|
|
|
this.focused = true;
|
|
|
|
event.stopPropagation();
|
|
|
|
this.cdr.detectChanges();
|
|
|
|
setTimeout(() => {
|
|
|
|
this.recipients.searchInput.nativeElement.focus();
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
}
|