Clean up change password validation
authorChocobozzz <me@florianbigard.com>
Wed, 5 Sep 2018 12:59:15 +0000 (14:59 +0200)
committerChocobozzz <me@florianbigard.com>
Wed, 5 Sep 2018 13:00:25 +0000 (15:00 +0200)
client/src/app/+my-account/my-account-settings/my-account-change-password/my-account-change-password.component.html
client/src/app/+my-account/my-account-settings/my-account-change-password/my-account-change-password.component.ts
client/src/app/+my-account/my-account.component.ts
client/src/app/shared/forms/form-validators/user-validators.service.ts

index 913b570cb697def13726f0994c238c1d64e39caf..ab6df52be475008fa9dbc663697ac31fdb1a6486 100644 (file)
@@ -6,7 +6,6 @@
   <input
     type="password" id="new-password" i18n-placeholder placeholder="New password"
     formControlName="new-password" [ngClass]="{ 'input-error': formErrors['new-password'] }"
-    (change)="validateNewPassword()"  (blur)="printAnError()"
   >
   <div *ngIf="formErrors['new-password']" class="form-error">
     {{ formErrors['new-password'] }}
 
   <input
     type="password" id="new-confirmed-password" i18n-placeholder placeholder="Confirm new password"
-    formControlName="new-confirmed-password" (change)="validateNewPassword()" (blur)="printAnError()"
+    formControlName="new-confirmed-password"
   >
+  <div *ngIf="formErrors['new-confirmed-password']" class="form-error">
+    {{ formErrors['new-confirmed-password'] }}
+  </div>
 
-  <input type="submit" i18n-value value="Change password" [disabled]="!form.valid || unsendable">
+  <input type="submit" i18n-value value="Change password" [disabled]="!form.valid">
 </form>
index 0707d8f9ae62d15bcdca64c3969f0ff7732417c4..57a706b0f090c4dc25f7a7c5022b624d000a632b 100644 (file)
@@ -4,6 +4,7 @@ import { FormReactive, UserService } from '../../../shared'
 import { I18n } from '@ngx-translate/i18n-polyfill'
 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
 import { UserValidatorsService } from '@app/shared/forms/form-validators/user-validators.service'
+import { filter } from 'rxjs/operators'
 
 @Component({
   selector: 'my-account-change-password',
@@ -12,7 +13,6 @@ import { UserValidatorsService } from '@app/shared/forms/form-validators/user-va
 })
 export class MyAccountChangePasswordComponent extends FormReactive implements OnInit {
   error: string = null
-  unsendable = true // default to true to not have to not the if in change password
 
   constructor (
     protected formValidatorService: FormValidatorService,
@@ -27,36 +27,23 @@ export class MyAccountChangePasswordComponent extends FormReactive implements On
   ngOnInit () {
     this.buildForm({
       'new-password': this.userValidatorsService.USER_PASSWORD,
-      'new-confirmed-password': this.userValidatorsService.USER_PASSWORD
+      'new-confirmed-password': this.userValidatorsService.USER_CONFIRM_PASSWORD
     })
-  }
 
-  validateNewPassword () {
-    if (this.form.value['new-password'] && this.form.value['new-confirmed-password']) {
-      if (this.form.value['new-password'] === this.form.value['new-confirmed-password']) {
-        this.error = null
-        this.unsendable = false
-        return
-      }
-    }
-    this.unsendable = true
-  }
+    const confirmPasswordControl = this.form.get('new-confirmed-password')
 
-  printAnError () {
-    console.log(this.unsendable)
-    this.validateNewPassword()
-    if (this.unsendable) {
-      this.error = this.i18n('The new password and the confirmed password do not correspond.')
-    }
+    confirmPasswordControl.valueChanges
+                          .pipe(filter(v => v !== this.form.value[ 'new-password' ]))
+                          .subscribe(() => confirmPasswordControl.setErrors({ matchPassword: true }))
   }
 
   changePassword () {
-    if (this.unsendable) {
-      return
-    }
+    this.userService.changePassword(this.form.value[ 'new-password' ]).subscribe(
+      () => {
+        this.notificationsService.success(this.i18n('Success'), this.i18n('Password updated.'))
 
-    this.userService.changePassword(this.form.value['new-password']).subscribe(
-      () => this.notificationsService.success(this.i18n('Success'), this.i18n('Password updated.')),
+        this.form.reset()
+      },
 
       err => this.error = err.message
     )
index 548f6a1c0b5ef183064d0f5ec2d14ce9443d937f..7100638c8deeb522e600d22215fb050f68cbd93d 100644 (file)
@@ -20,7 +20,6 @@ export class MyAccountComponent implements OnInit {
   ) {}
 
   ngOnInit () {
-    console.log(this.router.url)
     this.updateLibraryLabel(this.router.url)
 
     this.router.events
@@ -29,7 +28,9 @@ export class MyAccountComponent implements OnInit {
   }
 
   isVideoImportEnabled () {
-    return this.serverService.getConfig().import.videos.http.enabled
+    const importConfig = this.serverService.getConfig().import.videos
+
+    return importConfig.http.enabled || importConfig.torrent.enabled
   }
 
   private updateLibraryLabel (url: string) {
index 424553d7478398a481161cd125547e8a9d8e3e41..1fd1cdf68ed3220510f8c1b3a2014e0667cbb4bf 100644 (file)
@@ -8,6 +8,7 @@ export class UserValidatorsService {
   readonly USER_USERNAME: BuildFormValidator
   readonly USER_EMAIL: BuildFormValidator
   readonly USER_PASSWORD: BuildFormValidator
+  readonly USER_CONFIRM_PASSWORD: BuildFormValidator
   readonly USER_VIDEO_QUOTA: BuildFormValidator
   readonly USER_VIDEO_QUOTA_DAILY: BuildFormValidator
   readonly USER_ROLE: BuildFormValidator
@@ -55,6 +56,13 @@ export class UserValidatorsService {
       }
     }
 
+    this.USER_CONFIRM_PASSWORD = {
+      VALIDATORS: [],
+      MESSAGES: {
+        'matchPassword': this.i18n('The new password and the confirmed password do not correspond.')
+      }
+    }
+
     this.USER_VIDEO_QUOTA = {
       VALIDATORS: [ Validators.required, Validators.min(-1) ],
       MESSAGES: {