]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/+admin/users/user-edit/user-update.component.ts
Refractor notification service
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / users / user-edit / user-update.component.ts
index 23e44ac1f60f9f32eb5864bca78415cdaafe098b..61e64182391173e4674329c66b9157175860e7c3 100644 (file)
@@ -1,20 +1,15 @@
 import { Component, OnDestroy, OnInit } from '@angular/core'
-import { FormBuilder, FormGroup } from '@angular/forms'
 import { ActivatedRoute, Router } from '@angular/router'
-import { Subscription } from 'rxjs/Subscription'
-
-import { NotificationsService } from 'angular2-notifications'
-
-import { UserService } from '../shared'
-import {
-  USER_EMAIL,
-  USER_VIDEO_QUOTA,
-  USER_ROLE,
-  User
-} from '../../../shared'
+import { Subscription } from 'rxjs'
+import { Notifier } from '@app/core'
 import { ServerService } from '../../../core'
 import { UserEdit } from './user-edit'
-import { UserUpdate, UserRole } from '../../../../../../shared'
+import { User, UserUpdate } 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 { ConfigService } from '@app/+admin/config/shared/config.service'
+import { UserService } from '@app/shared'
 
 @Component({
   selector: 'my-user-update',
@@ -26,43 +21,32 @@ export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
   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,
+    private userValidatorsService: UserValidatorsService,
     private route: ActivatedRoute,
     private router: Router,
-    private notificationsService: NotificationsService,
-    private formBuilder: FormBuilder,
-    private userService: UserService
+    private notifier: Notifier,
+    private userService: UserService,
+    private i18n: I18n
   ) {
     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()
+    const defaultValues = { videoQuota: '-1', videoQuotaDaily: '-1' }
+    this.buildForm({
+      email: this.userValidatorsService.USER_EMAIL,
+      role: this.userValidatorsService.USER_ROLE,
+      videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
+      videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY
+    }, defaultValues)
 
     this.paramsSub = this.route.params.subscribe(routeParams => {
       const userId = routeParams['id']
@@ -85,10 +69,11 @@ export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
 
     // 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.notificationsService.success('Success', `User ${this.username} updated.`)
+        this.notifier.success(this.i18n('User {{username}} updated.', { username: this.username }))
         this.router.navigate([ '/admin/users/list' ])
       },
 
@@ -101,7 +86,7 @@ export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
   }
 
   getFormButtonTitle () {
-    return 'Update user'
+    return this.i18n('Update user')
   }
 
   private onUserFetched (userJson: User) {
@@ -111,7 +96,8 @@ export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
     this.form.patchValue({
       email: userJson.email,
       role: userJson.role,
-      videoQuota: userJson.videoQuota
+      videoQuota: userJson.videoQuota,
+      videoQuotaDaily: userJson.videoQuotaDaily
     })
   }
 }