Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit } from '@angular/core';
  2. import {AccountClient, ChangePasswordParams, NotificationType, UserDetailsDTO} from 'rcc-client/api';
  3. import { WholePageLoaderService} from '@corcode/components-base';
  4. import {ConstsService} from '../../services/utils/consts-service.service';
  5. import {IKeyValueModel} from '../../interfaces/common/key-value.model';
  6. import {Observable} from 'rxjs';
  7. import {FormGroup} from '@angular/forms';
  8. import {FormlyFieldConfig, FormlyFormOptions} from '@ngx-formly/core';
  9. import {FormlyFieldsRow, SwitchFormlyField, TextInputFormlyField} from '../../@theme/formly/models';
  10. import {ToastrService} from 'ngx-toastr';
  11. import {sum} from '../../utils/collection-extensions';
  12.  
  13. @Component({
  14.   selector: 'app-user-settings',
  15.   templateUrl: './user-settings.component.html',
  16.   styleUrls: ['./user-settings.component.scss']
  17. })
  18. export class UserSettingsComponent implements OnInit {
  19.  
  20.  
  21.   public dataUser: UserDetailsDTO = {} as UserDetailsDTO;
  22.   public printConstFnc: (id: number, options: IKeyValueModel[]) => string | Observable<string>;
  23.   public translateEnumLogicalSum: (id: number, options: IKeyValueModel[]) => string | Observable<string>;
  24.   public changePasswordParam: ExpandedChangePasswordParams = {};
  25.   public notificationsList: IUserFormNotifications[];
  26.   public model = { requestedWebsocketNotifications: {} };
  27.  
  28.  
  29.  
  30.   public form = new FormGroup({});
  31.   public options: FormlyFormOptions = {};
  32.   public changePasswordFields: FormlyFieldConfig[] = [
  33.     new FormlyFieldsRow([
  34.       new TextInputFormlyField('oldPassword', 'FORMLY.FORM.OLD_PASSWORD', {type: 'password', required: true}),
  35.       new TextInputFormlyField('newPassword', 'FORMLY.FORM.NEW_PASSWORD', {type: 'password', required: true}),
  36.       new TextInputFormlyField('confirmNewPassword', 'FORMLY.FORM.CONFIRM_NEW_PASSWORD',
  37.         {type: 'password', required: true })])
  38.   ];
  39.  
  40.   public requestedWebsocketNotificationsFormlyFields: FormlyFieldConfig[] = [];
  41.  
  42.   constructor(private accountClient: AccountClient,
  43.               private loader: WholePageLoaderService,
  44.               private constsService: ConstsService,
  45.               private toastr: ToastrService) {
  46.     this.printConstFnc = this.constsService.printConst.bind(this.constsService);
  47.     this.translateEnumLogicalSum = this.constsService.translateEnumLogicalSum.bind(this.constsService);
  48.   }
  49.  
  50.   setFF() {
  51.     for (const notification of this.notificationsList) {
  52.       const key = notification.Id;
  53.       const singleFormlyForm = new FormlyFieldsRow([
  54.         new SwitchFormlyField( `${{key}}`, notification.Name,
  55.           'FORMLY.FORM.OFF', 'FORMLY.FORM.ON')]);
  56.       this.requestedWebsocketNotificationsFormlyFields.push(singleFormlyForm);
  57.     }
  58.   }
  59.  
  60.   ngOnInit() {
  61.     this.notificationsList = this.constsService.NOTIFICATION_TYPE as IUserFormNotifications[];
  62.     this.loader.on();
  63.     this.accountClient.getMyAccountDetails().subscribe( res => {
  64.       this.dataUser = res;
  65.       this.setCheckedUserNotifications(this.dataUser.requestedWebsocketNotifications);
  66.       this.loader.off();
  67.     }, () => this.loader.off);
  68.     this.setFF();
  69.   }
  70.  
  71.   isPasswordCorrect(): boolean {
  72.     return this.changePasswordParam.newPassword === this.changePasswordParam.confirmNewPassword;
  73.   }
  74.  
  75.   onSubmit() {
  76.     if (!this.form.valid) {
  77.       return;
  78.     }
  79.  
  80.     if (!this.isPasswordCorrect()) {
  81.       this.toastr.error('TOASTR.CONFIRM_PASSWORD_INCORRECT');
  82.       return;
  83.     }
  84.  
  85.     this.loader.on();
  86.     this.accountClient.changePassword(this.changePasswordParam).subscribe(() => {
  87.       this.loader.off();
  88.     }, () => this.loader.off());
  89.   }
  90.   getUserNotificationsSum(): NotificationType {
  91.     return sum(this.notificationsList.filter(x => x.isChecked === true), x => x.Id as number);
  92.   }
  93.  
  94.   setCheckedUserNotifications(notificationsSum: number) {
  95.     for (const notification of this.notificationsList) {
  96.       if (((notification.Id as number) & notificationsSum) !== 0) {
  97.         notification.isChecked = true;
  98.       }
  99.     }
  100.   }
  101.  
  102.   saveUserNotifications() {
  103.     console.log(this.model, this.getUserNotificationsSum(), 'poczatek');
  104.     this.accountClient.editMyAccountSettings({
  105.       requestedWebsocketNotifications: this.getUserNotificationsSum(),
  106.       requestedMailNotifications: 0}).subscribe( () => {
  107.         console.log(this.model, this.getUserNotificationsSum(), 'e subscribie');
  108.     });
  109.     console.log(this.dataUser);
  110.   }
  111. }
  112.  
  113. export interface IUserFormNotifications extends IKeyValueModel {
  114.   isChecked: boolean;
  115. }
  116.  
  117. export interface ExpandedChangePasswordParams extends ChangePasswordParams {
  118.   confirmNewPassword?: string | undefined;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement