import { Observable } from "rxjs" ; import { Store } from "@ngrx/store" ; import { map } from "rxjs/operators" ; import { Component } from "@angular/core" ; import { ENCRYPTION_DERIVATION_PRESETS , KEY_DERIVATION_PRESETS } from "src/shared/model/options" ; import { FormControl , FormGroup , Validators } from "@angular/forms" ; import { OPTIONS_ACTIONS } from "src/web/browser-window/app/store/actions" ; import { OptionsSelectors } from "src/web/browser-window/app/store/selectors" ; import { State } from "src/web/browser-window/app/store/reducers/options" ; @ Component ( { selector : "electron-mail-storage" , templateUrl : "./storage.component.html" , preserveWhitespaces : true , } ) export class StorageComponent { password = new FormControl ( null , Validators . required ) ; newPassword = new FormControl ( null , Validators . required ) ; newPasswordConfirm = new FormControl ( null , [ Validators . required , ( ) => { if ( this . newPassword && this . newPasswordConfirm && this . newPassword . value !== this . newPasswordConfirm . value ) { return { mismatch : true } ; } return null ; } , ] ) ; form = new FormGroup ( { password : this . password , newPassword : this . newPassword , newPasswordConfirm : this . newPasswordConfirm , } ) ; encryptionPresetForm = new FormGroup ( { password : new FormControl ( null , Validators . required ) , keyDerivation : new FormControl ( null , Validators . required ) , encryption : new FormControl ( null , Validators . required ) , } ) ; changingPassword$ : Observable < boolean > ; reEncryptingSettings$ : Observable < boolean > ; constructor ( private store ) { this . changingPassword$ = this . store . select ( OptionsSelectors . FEATURED . progress ) . pipe ( map ( ( progress ) => Boolean ( progress . changingPassword ) ) ) ; this . reEncryptingSettings$ = this . store . select ( OptionsSelectors . FEATURED . progress ) . pipe ( map ( ( progress ) => Boolean ( progress . reEncryptingSettings ) ) ) ; } submit ( ) { this . store . dispatch ( OPTIONS_ACTIONS . ChangeMasterPasswordRequest ( { password : this . password . value , newPassword : this . newPassword . value , } ) ) ; } submitPresets ( ) { const keyDerivation = KEY_DERIVATION_PRESETS [ this . encryptionPresetForm . controls . keyDerivation ?. value ] ; const encryption = ENCRYPTION_DERIVATION_PRESETS [ this . encryptionPresetForm . controls . encryption ?. value ] ; if ( ! keyDerivation || ! encryption ) { throw new Error ( "Invalid keyDerivation/encryption values detected" ) ; } this . store . dispatch ( OPTIONS_ACTIONS . ReEncryptSettings ( { password : this . encryptionPresetForm . controls . password ?. value , encryptionPreset : { keyDerivation , encryption } , } ) ) ; } }