]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/+admin/users/user-edit/user-update.component.ts
We don't need services anymore for validators
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / users / user-edit / user-update.component.ts
index dca555706511e34fb6dabe016f56212e6c7a2a20..e16f66a2baecace4875ee9f71d5dd0edbdb19886 100644 (file)
@@ -1,14 +1,17 @@
+import { Subscription } from 'rxjs'
 import { Component, OnDestroy, OnInit } from '@angular/core'
-import { FormBuilder, FormGroup } from '@angular/forms'
 import { ActivatedRoute, Router } from '@angular/router'
-import { Subscription } from 'rxjs'
-import { NotificationsService } from 'angular2-notifications'
-import { UserService } from '../shared'
-import { User, USER_EMAIL, USER_ROLE, USER_VIDEO_QUOTA } from '../../../shared'
-import { ServerService } from '../../../core'
+import { ConfigService } from '@app/+admin/config/shared/config.service'
+import { AuthService, Notifier, ScreenService, ServerService, User, UserService } from '@app/core'
+import {
+  USER_EMAIL_VALIDATOR,
+  USER_ROLE_VALIDATOR,
+  USER_VIDEO_QUOTA_DAILY_VALIDATOR,
+  USER_VIDEO_QUOTA_VALIDATOR
+} from '@app/shared/form-validators/user-validators'
+import { FormValidatorService } from '@app/shared/shared-forms'
+import { User as UserType, UserAdminFlag, UserRole, UserUpdate } from '@shared/models'
 import { UserEdit } from './user-edit'
-import { UserUpdate } from '../../../../../../shared'
-import { I18n } from '@ngx-translate/i18n-polyfill'
 
 @Component({
   selector: 'my-user-update',
@@ -17,51 +20,45 @@ import { I18n } from '@ngx-translate/i18n-polyfill'
 })
 export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
   error: string
-  userId: number
-  username: string
-
-  form: FormGroup
-  formErrors = {
-    'email': '',
-    'role': '',
-    'videoQuota': ''
-  }
-  validationMessages = {
-    'email': USER_EMAIL.MESSAGES,
-    'role': USER_ROLE.MESSAGES,
-    'videoQuota': USER_VIDEO_QUOTA.MESSAGES
-  }
 
   private paramsSub: Subscription
 
   constructor (
+    protected formValidatorService: FormValidatorService,
     protected serverService: ServerService,
+    protected configService: ConfigService,
+    protected screenService: ScreenService,
+    protected auth: AuthService,
     private route: ActivatedRoute,
     private router: Router,
-    private notificationsService: NotificationsService,
-    private formBuilder: FormBuilder,
-    private userService: UserService,
-    private i18n: I18n
-  ) {
+    private notifier: Notifier,
+    private userService: UserService
+    ) {
     super()
-  }
 
-  buildForm () {
-    this.form = this.formBuilder.group({
-      email:    [ '', USER_EMAIL.VALIDATORS ],
-      role: [ '', USER_ROLE.VALIDATORS ],
-      videoQuota: [ '-1', USER_VIDEO_QUOTA.VALIDATORS ]
-    })
-
-    this.form.valueChanges.subscribe(data => this.onValueChanged(data))
+    this.buildQuotaOptions()
   }
 
   ngOnInit () {
-    this.buildForm()
+    super.ngOnInit()
+
+    const defaultValues = {
+      role: UserRole.USER.toString(),
+      videoQuota: '-1',
+      videoQuotaDaily: '-1'
+    }
+
+    this.buildForm({
+      email: USER_EMAIL_VALIDATOR,
+      role: USER_ROLE_VALIDATOR,
+      videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
+      videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR,
+      byPassAutoBlock: null
+    }, defaultValues)
 
     this.paramsSub = this.route.params.subscribe(routeParams => {
       const userId = routeParams['id']
-      this.userService.getUser(userId).subscribe(
+      this.userService.getUser(userId, true).subscribe(
         user => this.onUserFetched(user),
 
         err => this.error = err.message
@@ -77,16 +74,15 @@ export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
     this.error = undefined
 
     const userUpdate: UserUpdate = this.form.value
+    userUpdate.adminFlags = this.buildAdminFlags(this.form.value)
 
     // A select in HTML is always mapped as a string, we convert it to number
     userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
+    userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
 
-    this.userService.updateUser(this.userId, userUpdate).subscribe(
+    this.userService.updateUser(this.user.id, userUpdate).subscribe(
       () => {
-        this.notificationsService.success(
-          this.i18n('Success'),
-          this.i18n('User {{ username }} updated.', { username: this.username })
-        )
+        this.notifier.success($localize`User ${this.user.username} updated.`)
         this.router.navigate([ '/admin/users/list' ])
       },
 
@@ -98,18 +94,33 @@ export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
     return false
   }
 
+  isPasswordOptional () {
+    return false
+  }
+
   getFormButtonTitle () {
-    return this.i18n('Update user')
+    return $localize`Update user`
+  }
+
+  resetPassword () {
+    this.userService.askResetPassword(this.user.email).subscribe(
+      () => {
+        this.notifier.success($localize`An email asking for password reset has been sent to ${this.user.username}.`)
+      },
+
+      err => this.error = err.message
+    )
   }
 
-  private onUserFetched (userJson: User) {
-    this.userId = userJson.id
-    this.username = userJson.username
+  private onUserFetched (userJson: UserType) {
+    this.user = new User(userJson)
 
     this.form.patchValue({
       email: userJson.email,
-      role: userJson.role,
-      videoQuota: userJson.videoQuota
+      role: userJson.role.toString(),
+      videoQuota: userJson.videoQuota,
+      videoQuotaDaily: userJson.videoQuotaDaily,
+      byPassAutoBlock: userJson.adminFlags & UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST
     })
   }
 }