Reorganize client shared modules
[oweals/peertube.git] / client / src / app / +admin / users / user-edit / user-update.component.ts
1 import { Subscription } from 'rxjs'
2 import { Component, OnDestroy, OnInit } from '@angular/core'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { ConfigService } from '@app/+admin/config/shared/config.service'
5 import { AuthService, Notifier, ScreenService, ServerService, User, UserService } from '@app/core'
6 import { FormValidatorService, UserValidatorsService } from '@app/shared/shared-forms'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { User as UserType, UserAdminFlag, UserRole, UserUpdate } from '@shared/models'
9 import { UserEdit } from './user-edit'
10
11 @Component({
12   selector: 'my-user-update',
13   templateUrl: './user-edit.component.html',
14   styleUrls: [ './user-edit.component.scss' ]
15 })
16 export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
17   error: string
18
19   private paramsSub: Subscription
20
21   constructor (
22     protected formValidatorService: FormValidatorService,
23     protected serverService: ServerService,
24     protected configService: ConfigService,
25     protected screenService: ScreenService,
26     protected auth: AuthService,
27     private userValidatorsService: UserValidatorsService,
28     private route: ActivatedRoute,
29     private router: Router,
30     private notifier: Notifier,
31     private userService: UserService,
32     private i18n: I18n
33   ) {
34     super()
35
36     this.buildQuotaOptions()
37   }
38
39   ngOnInit () {
40     super.ngOnInit()
41
42     const defaultValues = {
43       role: UserRole.USER.toString(),
44       videoQuota: '-1',
45       videoQuotaDaily: '-1'
46     }
47
48     this.buildForm({
49       email: this.userValidatorsService.USER_EMAIL,
50       role: this.userValidatorsService.USER_ROLE,
51       videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
52       videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY,
53       byPassAutoBlock: null
54     }, defaultValues)
55
56     this.paramsSub = this.route.params.subscribe(routeParams => {
57       const userId = routeParams['id']
58       this.userService.getUser(userId, true).subscribe(
59         user => this.onUserFetched(user),
60
61         err => this.error = err.message
62       )
63     })
64   }
65
66   ngOnDestroy () {
67     this.paramsSub.unsubscribe()
68   }
69
70   formValidated () {
71     this.error = undefined
72
73     const userUpdate: UserUpdate = this.form.value
74     userUpdate.adminFlags = this.buildAdminFlags(this.form.value)
75
76     // A select in HTML is always mapped as a string, we convert it to number
77     userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
78     userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
79
80     this.userService.updateUser(this.user.id, userUpdate).subscribe(
81       () => {
82         this.notifier.success(this.i18n('User {{username}} updated.', { username: this.user.username }))
83         this.router.navigate([ '/admin/users/list' ])
84       },
85
86       err => this.error = err.message
87     )
88   }
89
90   isCreation () {
91     return false
92   }
93
94   isPasswordOptional () {
95     return false
96   }
97
98   getFormButtonTitle () {
99     return this.i18n('Update user')
100   }
101
102   resetPassword () {
103     this.userService.askResetPassword(this.user.email).subscribe(
104       () => {
105         this.notifier.success(
106           this.i18n('An email asking for password reset has been sent to {{username}}.', { username: this.user.username })
107         )
108       },
109
110       err => this.error = err.message
111     )
112   }
113
114   private onUserFetched (userJson: UserType) {
115     this.user = new User(userJson)
116
117     this.form.patchValue({
118       email: userJson.email,
119       role: userJson.role.toString(),
120       videoQuota: userJson.videoQuota,
121       videoQuotaDaily: userJson.videoQuotaDaily,
122       byPassAutoBlock: userJson.adminFlags & UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST
123     })
124   }
125 }