Browse Source

UI: Refactoring

pull/8903/head
Artem Dzhereleiko 3 years ago
parent
commit
23880ad077
  1. 25
      ui-ngx/src/app/modules/home/pages/notification/settings/notification-setting-form.component.ts
  2. 18
      ui-ngx/src/app/modules/home/pages/notification/settings/notification-settings.component.html
  3. 8
      ui-ngx/src/app/modules/home/pages/notification/settings/notification-settings.component.scss
  4. 22
      ui-ngx/src/app/modules/home/pages/notification/settings/notification-settings.component.ts
  5. 7
      ui-ngx/src/app/shared/models/notification.models.ts

25
ui-ngx/src/app/modules/home/pages/notification/settings/notification-setting-form.component.ts

@ -20,7 +20,7 @@ import { UtilsService } from '@core/services/utils.service';
import { isDefinedAndNotNull } from '@core/utils'; import { isDefinedAndNotNull } from '@core/utils';
import { Subscription } from 'rxjs'; import { Subscription } from 'rxjs';
import { import {
NotificationDeliveryMethod, NotificationDeliveryMethod, NotificationSettingsDeliveryMethod,
NotificationTemplateTypeTranslateMap, NotificationTemplateTypeTranslateMap,
NotificationUserSetting NotificationUserSetting
} from '@shared/models/notification.models'; } from '@shared/models/notification.models';
@ -47,8 +47,8 @@ export class NotificationSettingFormComponent implements ControlValueAccessor, O
notificationSettingsFormGroup: UntypedFormGroup; notificationSettingsFormGroup: UntypedFormGroup;
notificationDeliveryMethod = NotificationDeliveryMethod; notificationDeliveryMethod = NotificationSettingsDeliveryMethod;
notificationDeliveryMethodMap = [NotificationDeliveryMethod.WEB, NotificationDeliveryMethod.SMS, NotificationDeliveryMethod.EMAIL]; notificationDeliveryMethodMap = Object.values(NotificationSettingsDeliveryMethod);
notificationTemplateTypeTranslateMap = NotificationTemplateTypeTranslateMap; notificationTemplateTypeTranslateMap = NotificationTemplateTypeTranslateMap;
private propagateChange = null; private propagateChange = null;
@ -67,11 +67,17 @@ export class NotificationSettingFormComponent implements ControlValueAccessor, O
} }
ngOnInit() { ngOnInit() {
const deliveryMethod = {};
this.notificationDeliveryMethodMap.forEach(value => {
deliveryMethod[value] = true;
});
this.notificationSettingsFormGroup = this.fb.group( this.notificationSettingsFormGroup = this.fb.group(
{ {
name: [''], name: [''],
enabled: [true], enabled: [true],
enabledDeliveryMethods: [] enabledDeliveryMethods: this.fb.group({
...deliveryMethod
})
}); });
this.valueChange$ = this.notificationSettingsFormGroup.valueChanges.subscribe(() => { this.valueChange$ = this.notificationSettingsFormGroup.valueChanges.subscribe(() => {
this.updateModel(); this.updateModel();
@ -99,17 +105,12 @@ export class NotificationSettingFormComponent implements ControlValueAccessor, O
} }
getChecked(deliveryMethod: NotificationDeliveryMethod): boolean { getChecked(deliveryMethod: NotificationDeliveryMethod): boolean {
return this.notificationSettingsFormGroup.get('enabledDeliveryMethods').value.includes(deliveryMethod); return this.notificationSettingsFormGroup.get('enabledDeliveryMethods').get(deliveryMethod).value;
} }
toggleDeliviryMethod(deliveryMethod: NotificationDeliveryMethod) { toggleDeliviryMethod(deliveryMethod: NotificationDeliveryMethod) {
const enabledDeliveryMethods = this.notificationSettingsFormGroup.get('enabledDeliveryMethods').value; this.notificationSettingsFormGroup.get('enabledDeliveryMethods').get(deliveryMethod)
if (enabledDeliveryMethods.includes(deliveryMethod)) { .patchValue(!this.notificationSettingsFormGroup.get('enabledDeliveryMethods').get(deliveryMethod).value);
enabledDeliveryMethods.splice(enabledDeliveryMethods.indexOf(deliveryMethod), 1);
} else {
enabledDeliveryMethods.push(deliveryMethod);
}
this.notificationSettingsFormGroup.get('enabledDeliveryMethods').patchValue(enabledDeliveryMethods);
} }
writeValue(value: NotificationUserSetting): void { writeValue(value: NotificationUserSetting): void {

18
ui-ngx/src/app/modules/home/pages/notification/settings/notification-settings.component.html

@ -16,7 +16,7 @@
--> -->
<div> <div>
<mat-card appearance="outlined" class="settings-card"> <mat-card appearance="outlined" class="settings-card tb-absolute-fill">
<mat-card-header> <mat-card-header>
<div fxFlex fxLayout="row" fxLayout.xs="column" fxLayoutGap.xs="8px" <div fxFlex fxLayout="row" fxLayout.xs="column" fxLayoutGap.xs="8px"
fxLayoutAlign="space-between start" fxLayoutAlign.xs="start start"> fxLayoutAlign="space-between start" fxLayoutAlign.xs="start start">
@ -68,14 +68,14 @@
</div> </div>
</section> </section>
</form> </form>
<div fxLayout="row" fxLayoutAlign="end start">
<button mat-button mat-raised-button color="primary"
type="button"
(click)="save()"
[disabled]="(isLoading$ | async) || notificationSettings.invalid || !notificationSettings.dirty">
{{ 'action.save' | translate }}
</button>
</div>
</mat-card-content> </mat-card-content>
<div fxLayout="row" fxLayoutAlign="end start" style="padding: 16px;">
<button mat-button mat-raised-button color="primary"
type="button"
(click)="save()"
[disabled]="(isLoading$ | async) || notificationSettings.invalid || !notificationSettings.dirty">
{{ 'action.save' | translate }}
</button>
</div>
</mat-card> </mat-card>
</div> </div>

8
ui-ngx/src/app/modules/home/pages/notification/settings/notification-settings.component.scss

@ -17,11 +17,6 @@
:host { :host {
.mat-mdc-card.settings-card { .mat-mdc-card.settings-card {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: 8px; margin: 8px;
@media #{$mat-gt-sm} { @media #{$mat-gt-sm} {
width: 60%; width: 60%;
@ -30,10 +25,9 @@
margin: 0; margin: 0;
} }
.notification-form { .notification-form {
height: calc(100% - 48px); height: 100%;
min-height: min-content; min-height: min-content;
max-height: min-content; max-height: min-content;
margin-bottom: 16px;
} }
.notification-section { .notification-section {
height: 100%; height: 100%;

22
ui-ngx/src/app/modules/home/pages/notification/settings/notification-settings.component.ts

@ -25,7 +25,7 @@ import { ActivatedRoute } from '@angular/router';
import { deepClone, isDefinedAndNotNull } from '@core/utils'; import { deepClone, isDefinedAndNotNull } from '@core/utils';
import { import {
NotificationDeliveryMethod, NotificationDeliveryMethod,
NotificationDeliveryMethodTranslateMap, NotificationDeliveryMethodTranslateMap, NotificationSettingsDeliveryMethod,
NotificationUserSettings NotificationUserSettings
} from '@shared/models/notification.models'; } from '@shared/models/notification.models';
import { NotificationService } from '@core/http/notification.service'; import { NotificationService } from '@core/http/notification.service';
@ -40,7 +40,7 @@ export class NotificationSettingsComponent extends PageComponent implements OnIn
notificationSettings: UntypedFormGroup; notificationSettings: UntypedFormGroup;
notificationDeliveryMethods = [NotificationDeliveryMethod.WEB, NotificationDeliveryMethod.SMS, NotificationDeliveryMethod.EMAIL]; notificationDeliveryMethods = Object.values(NotificationSettingsDeliveryMethod);
notificationDeliveryMethodTranslateMap = NotificationDeliveryMethodTranslateMap; notificationDeliveryMethodTranslateMap = NotificationDeliveryMethodTranslateMap;
allowNotificationDeliveryMethods: Array<NotificationDeliveryMethod>; allowNotificationDeliveryMethods: Array<NotificationDeliveryMethod>;
@ -76,6 +76,10 @@ export class NotificationSettingsComponent extends PageComponent implements OnIn
if (settings.prefs) { if (settings.prefs) {
preparedSettings = this.prepareNotificationSettings(settings.prefs); preparedSettings = this.prepareNotificationSettings(settings.prefs);
preparedSettings.forEach((setting) => { preparedSettings.forEach((setting) => {
setting.enabledDeliveryMethods = Object.assign(
setting.enabledDeliveryMethods,
this.notificationDeliveryMethods.reduce((a, v) => ({ ...a, [v]: true}), {})
);
notificationSettingsControls.push(this.fb.control(setting, [Validators.required])); notificationSettingsControls.push(this.fb.control(setting, [Validators.required]));
}); });
} }
@ -105,7 +109,7 @@ export class NotificationSettingsComponent extends PageComponent implements OnIn
if (settings) { if (settings) {
settings.forEach((setting) => { settings.forEach((setting) => {
setting.enabled = true; setting.enabled = true;
setting.enabledDeliveryMethods = this.notificationDeliveryMethods; setting.enabledDeliveryMethods = this.notificationDeliveryMethods.reduce((a, v) => ({ ...a, [v]: true}), {});
notificationSettingsControls.push(this.fb.control(setting, [Validators.required])); notificationSettingsControls.push(this.fb.control(setting, [Validators.required]));
}); });
} }
@ -119,7 +123,7 @@ export class NotificationSettingsComponent extends PageComponent implements OnIn
getChecked = (method: NotificationDeliveryMethod = null): boolean => { getChecked = (method: NotificationDeliveryMethod = null): boolean => {
const type = this.notificationSettings.get('prefs').value; const type = this.notificationSettings.get('prefs').value;
if (isDefinedAndNotNull(method)) { if (isDefinedAndNotNull(method)) {
return isDefinedAndNotNull(type) && type.every(resource => resource.enabledDeliveryMethods.includes(method)); return isDefinedAndNotNull(type) && type.every(resource => resource.enabledDeliveryMethods[method]);
} }
return isDefinedAndNotNull(type) && type.every(resource => resource.enabled); return isDefinedAndNotNull(type) && type.every(resource => resource.enabled);
}; };
@ -133,7 +137,7 @@ export class NotificationSettingsComponent extends PageComponent implements OnIn
const type = this.notificationSettings.get('prefs').value; const type = this.notificationSettings.get('prefs').value;
if (isDefinedAndNotNull(type)) { if (isDefinedAndNotNull(type)) {
const checkedResource = isDefinedAndNotNull(deliveryMethod) ? const checkedResource = isDefinedAndNotNull(deliveryMethod) ?
type.filter(resource => resource.enabledDeliveryMethods.includes(deliveryMethod)) : type.filter(resource => resource.enabledDeliveryMethods[deliveryMethod]) :
type.filter(resource => resource.enabled); type.filter(resource => resource.enabled);
return checkedResource.length !== 0 && checkedResource.length !== type.length; return checkedResource.length !== 0 && checkedResource.length !== type.length;
} }
@ -143,13 +147,7 @@ export class NotificationSettingsComponent extends PageComponent implements OnIn
changeInstanceTypeCheckBox = (value: boolean, deliveryMethod: NotificationDeliveryMethod = null): void => { changeInstanceTypeCheckBox = (value: boolean, deliveryMethod: NotificationDeliveryMethod = null): void => {
const type = deepClone(this.notificationSettings.get('prefs').value); const type = deepClone(this.notificationSettings.get('prefs').value);
if (isDefinedAndNotNull(deliveryMethod)) { if (isDefinedAndNotNull(deliveryMethod)) {
type.forEach(notificationType => { type.forEach(notificationType => notificationType.enabledDeliveryMethods[deliveryMethod] = value);
if (value && !notificationType.enabledDeliveryMethods.includes(deliveryMethod)) {
notificationType.enabledDeliveryMethods.push(deliveryMethod);
} else if (!value && notificationType.enabledDeliveryMethods.includes(deliveryMethod)) {
notificationType.enabledDeliveryMethods.splice(notificationType.enabledDeliveryMethods.indexOf(deliveryMethod), 1);
}
});
} else { } else {
type.forEach(notificationType => notificationType.enabled = value); type.forEach(notificationType => notificationType.enabled = value);
} }

7
ui-ngx/src/app/shared/models/notification.models.ts

@ -596,6 +596,11 @@ export interface NotificationUserSettings {
export interface NotificationUserSetting { export interface NotificationUserSetting {
enabled: boolean; enabled: boolean;
enabledDeliveryMethods: Array<NotificationDeliveryMethod>; enabledDeliveryMethods: {[key: string]: boolean};
} }
export enum NotificationSettingsDeliveryMethod {
WEB = 'WEB',
SMS = 'SMS',
EMAIL = 'EMAIL'
}

Loading…
Cancel
Save