Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Component, OnInit } from '@angular/core';
- import {AccountClient, ChangePasswordParams, NotificationType, UserDetailsDTO} from 'rcc-client/api';
- import { WholePageLoaderService} from '@corcode/components-base';
- import {ConstsService} from '../../services/utils/consts-service.service';
- import {IKeyValueModel} from '../../interfaces/common/key-value.model';
- import {Observable} from 'rxjs';
- import {FormGroup} from '@angular/forms';
- import {FormlyFieldConfig, FormlyFormOptions} from '@ngx-formly/core';
- import {FormlyFieldsRow, SwitchFormlyField, TextInputFormlyField} from '../../@theme/formly/models';
- import {ToastrService} from 'ngx-toastr';
- import {sum} from '../../utils/collection-extensions';
- @Component({
- selector: 'app-user-settings',
- templateUrl: './user-settings.component.html',
- styleUrls: ['./user-settings.component.scss']
- })
- export class UserSettingsComponent implements OnInit {
- public dataUser: UserDetailsDTO = {} as UserDetailsDTO;
- public printConstFnc: (id: number, options: IKeyValueModel[]) => string | Observable<string>;
- public translateEnumLogicalSum: (id: number, options: IKeyValueModel[]) => string | Observable<string>;
- public changePasswordParam: ExpandedChangePasswordParams = {};
- public notificationsList: IUserFormNotifications[];
- public model = { requestedWebsocketNotifications: {} };
- public form = new FormGroup({});
- public options: FormlyFormOptions = {};
- public changePasswordFields: FormlyFieldConfig[] = [
- new FormlyFieldsRow([
- new TextInputFormlyField('oldPassword', 'FORMLY.FORM.OLD_PASSWORD', {type: 'password', required: true}),
- new TextInputFormlyField('newPassword', 'FORMLY.FORM.NEW_PASSWORD', {type: 'password', required: true}),
- new TextInputFormlyField('confirmNewPassword', 'FORMLY.FORM.CONFIRM_NEW_PASSWORD',
- {type: 'password', required: true })])
- ];
- public requestedWebsocketNotificationsFormlyFields: FormlyFieldConfig[] = [];
- constructor(private accountClient: AccountClient,
- private loader: WholePageLoaderService,
- private constsService: ConstsService,
- private toastr: ToastrService) {
- this.printConstFnc = this.constsService.printConst.bind(this.constsService);
- this.translateEnumLogicalSum = this.constsService.translateEnumLogicalSum.bind(this.constsService);
- }
- setFF() {
- for (const notification of this.notificationsList) {
- const key = notification.Id;
- const singleFormlyForm = new FormlyFieldsRow([
- new SwitchFormlyField( `${{key}}`, notification.Name,
- 'FORMLY.FORM.OFF', 'FORMLY.FORM.ON')]);
- this.requestedWebsocketNotificationsFormlyFields.push(singleFormlyForm);
- }
- }
- ngOnInit() {
- this.notificationsList = this.constsService.NOTIFICATION_TYPE as IUserFormNotifications[];
- this.loader.on();
- this.accountClient.getMyAccountDetails().subscribe( res => {
- this.dataUser = res;
- this.setCheckedUserNotifications(this.dataUser.requestedWebsocketNotifications);
- this.loader.off();
- }, () => this.loader.off);
- this.setFF();
- }
- isPasswordCorrect(): boolean {
- return this.changePasswordParam.newPassword === this.changePasswordParam.confirmNewPassword;
- }
- onSubmit() {
- if (!this.form.valid) {
- return;
- }
- if (!this.isPasswordCorrect()) {
- this.toastr.error('TOASTR.CONFIRM_PASSWORD_INCORRECT');
- return;
- }
- this.loader.on();
- this.accountClient.changePassword(this.changePasswordParam).subscribe(() => {
- this.loader.off();
- }, () => this.loader.off());
- }
- getUserNotificationsSum(): NotificationType {
- return sum(this.notificationsList.filter(x => x.isChecked === true), x => x.Id as number);
- }
- setCheckedUserNotifications(notificationsSum: number) {
- for (const notification of this.notificationsList) {
- if (((notification.Id as number) & notificationsSum) !== 0) {
- notification.isChecked = true;
- }
- }
- }
- saveUserNotifications() {
- console.log(this.model, this.getUserNotificationsSum(), 'poczatek');
- this.accountClient.editMyAccountSettings({
- requestedWebsocketNotifications: this.getUserNotificationsSum(),
- requestedMailNotifications: 0}).subscribe( () => {
- console.log(this.model, this.getUserNotificationsSum(), 'e subscribie');
- });
- console.log(this.dataUser);
- }
- }
- export interface IUserFormNotifications extends IKeyValueModel {
- isChecked: boolean;
- }
- export interface ExpandedChangePasswordParams extends ChangePasswordParams {
- confirmNewPassword?: string | undefined;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement