From b0f9f39ed70299a208d1b388c72de8b7f3510cb7 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 4 Sep 2017 20:07:54 +0200 Subject: Begin user quota --- client/package.json | 4 ++-- client/src/app/+admin/users/shared/user.service.ts | 25 +++++++++++++++++++++- .../+admin/users/user-add/user-add.component.html | 19 +++++++++++++--- .../+admin/users/user-add/user-add.component.ts | 15 +++++++++---- .../+admin/users/user-list/user-list.component.ts | 5 ++++- .../src/app/shared/forms/form-validators/user.ts | 7 ++++++ client/src/app/shared/rest/rest-data-source.ts | 21 ++++++++++++++++-- client/src/app/shared/users/user.model.ts | 13 +++++++++-- client/tslint.json | 1 - client/yarn.lock | 20 ++++++++--------- 10 files changed, 104 insertions(+), 26 deletions(-) (limited to 'client') diff --git a/client/package.json b/client/package.json index 27246027b..f1c7e8799 100644 --- a/client/package.json +++ b/client/package.json @@ -80,9 +80,9 @@ "string-replace-loader": "^1.0.3", "style-loader": "^0.18.2", "tslib": "^1.5.0", - "tslint": "^5.4.3", + "tslint": "^5.7.0", "tslint-loader": "^3.3.0", - "typescript": "~2.4.0", + "typescript": "^2.5.2", "url-loader": "^0.5.7", "video.js": "^6.2.0", "videojs-dock": "^2.0.2", diff --git a/client/src/app/+admin/users/shared/user.service.ts b/client/src/app/+admin/users/shared/user.service.ts index 1c1cd575e..ffd7ba7da 100644 --- a/client/src/app/+admin/users/shared/user.service.ts +++ b/client/src/app/+admin/users/shared/user.service.ts @@ -2,12 +2,15 @@ import { Injectable } from '@angular/core' import 'rxjs/add/operator/catch' import 'rxjs/add/operator/map' +import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe' + import { AuthHttp, RestExtractor, RestDataSource, User } from '../../../shared' import { UserCreate } from '../../../../../../shared' @Injectable() export class UserService { private static BASE_USERS_URL = API_URL + '/api/v1/users/' + private bytesPipe = new BytesPipe() constructor ( private authHttp: AuthHttp, @@ -21,10 +24,30 @@ export class UserService { } getDataSource () { - return new RestDataSource(this.authHttp, UserService.BASE_USERS_URL) + return new RestDataSource(this.authHttp, UserService.BASE_USERS_URL, this.formatDataSource.bind(this)) } removeUser (user: User) { return this.authHttp.delete(UserService.BASE_USERS_URL + user.id) } + + private formatDataSource (users: User[]) { + const newUsers = [] + + users.forEach(user => { + let videoQuota + if (user.videoQuota === -1) { + videoQuota = 'Unlimited' + } else { + videoQuota = this.bytesPipe.transform(user.videoQuota) + } + + const newUser = Object.assign(user, { + videoQuota + }) + newUsers.push(newUser) + }) + + return newUsers + } } diff --git a/client/src/app/+admin/users/user-add/user-add.component.html b/client/src/app/+admin/users/user-add/user-add.component.html index 9b487aa75..f84d72c7c 100644 --- a/client/src/app/+admin/users/user-add/user-add.component.html +++ b/client/src/app/+admin/users/user-add/user-add.component.html @@ -9,7 +9,7 @@
@@ -20,7 +20,7 @@
@@ -31,7 +31,7 @@
@@ -39,6 +39,19 @@
+
+ + +
+
diff --git a/client/src/app/+admin/users/user-add/user-add.component.ts b/client/src/app/+admin/users/user-add/user-add.component.ts index 0dd99eccd..91377a933 100644 --- a/client/src/app/+admin/users/user-add/user-add.component.ts +++ b/client/src/app/+admin/users/user-add/user-add.component.ts @@ -9,7 +9,8 @@ import { FormReactive, USER_USERNAME, USER_EMAIL, - USER_PASSWORD + USER_PASSWORD, + USER_VIDEO_QUOTA } from '../../../shared' import { UserCreate } from '../../../../../../shared' @@ -24,12 +25,14 @@ export class UserAddComponent extends FormReactive implements OnInit { formErrors = { 'username': '', 'email': '', - 'password': '' + 'password': '', + 'videoQuota': '' } validationMessages = { 'username': USER_USERNAME.MESSAGES, 'email': USER_EMAIL.MESSAGES, - 'password': USER_PASSWORD.MESSAGES + 'password': USER_PASSWORD.MESSAGES, + 'videoQuota': USER_VIDEO_QUOTA.MESSAGES } constructor ( @@ -45,7 +48,8 @@ export class UserAddComponent extends FormReactive implements OnInit { this.form = this.formBuilder.group({ username: [ '', USER_USERNAME.VALIDATORS ], email: [ '', USER_EMAIL.VALIDATORS ], - password: [ '', USER_PASSWORD.VALIDATORS ] + password: [ '', USER_PASSWORD.VALIDATORS ], + videoQuota: [ '-1', USER_VIDEO_QUOTA.VALIDATORS ] }) this.form.valueChanges.subscribe(data => this.onValueChanged(data)) @@ -60,6 +64,9 @@ export class UserAddComponent extends FormReactive implements OnInit { const userCreate: UserCreate = this.form.value + // A select in HTML is always mapped as a string, we convert it to number + userCreate.videoQuota = parseInt(this.form.value['videoQuota'], 10) + this.userService.addUser(userCreate).subscribe( () => { this.notificationsService.success('Success', `User ${userCreate.username} created.`) diff --git a/client/src/app/+admin/users/user-list/user-list.component.ts b/client/src/app/+admin/users/user-list/user-list.component.ts index 12826741c..dbb85cedd 100644 --- a/client/src/app/+admin/users/user-list/user-list.component.ts +++ b/client/src/app/+admin/users/user-list/user-list.component.ts @@ -30,7 +30,7 @@ export class UserListComponent { }, pager: { display: true, - perPage: 10 + perPage: 1 }, columns: { id: { @@ -43,6 +43,9 @@ export class UserListComponent { email: { title: 'Email' }, + videoQuota: { + title: 'Video quota' + }, role: { title: 'Role', sort: false diff --git a/client/src/app/shared/forms/form-validators/user.ts b/client/src/app/shared/forms/form-validators/user.ts index fd316583e..087a99760 100644 --- a/client/src/app/shared/forms/form-validators/user.ts +++ b/client/src/app/shared/forms/form-validators/user.ts @@ -22,3 +22,10 @@ export const USER_PASSWORD = { 'minlength': 'Password must be at least 6 characters long.' } } +export const USER_VIDEO_QUOTA = { + VALIDATORS: [ Validators.required, Validators.min(-1) ], + MESSAGES: { + 'required': 'Video quota is required.', + 'min': 'Quota must be greater than -1.' + } +} \ No newline at end of file diff --git a/client/src/app/shared/rest/rest-data-source.ts b/client/src/app/shared/rest/rest-data-source.ts index 7956637e0..5c205d280 100644 --- a/client/src/app/shared/rest/rest-data-source.ts +++ b/client/src/app/shared/rest/rest-data-source.ts @@ -3,14 +3,31 @@ import { Http, RequestOptionsArgs, URLSearchParams, Response } from '@angular/ht import { ServerDataSource } from 'ng2-smart-table' export class RestDataSource extends ServerDataSource { - constructor (http: Http, endpoint: string) { + private updateResponse: (input: any[]) => any[] + + constructor (http: Http, endpoint: string, updateResponse?: (input: any[]) => any[]) { const options = { endPoint: endpoint, sortFieldKey: 'sort', dataKey: 'data' } - super(http, options) + + if (updateResponse) { + this.updateResponse = updateResponse + } + } + + protected extractDataFromResponse (res: Response) { + const json = res.json() + if (!json) return [] + let data = json.data + + if (this.updateResponse !== undefined) { + data = this.updateResponse(data) + } + + return data } protected extractTotalFromResponse (res: Response) { diff --git a/client/src/app/shared/users/user.model.ts b/client/src/app/shared/users/user.model.ts index 1c2b481e3..bf12876c7 100644 --- a/client/src/app/shared/users/user.model.ts +++ b/client/src/app/shared/users/user.model.ts @@ -6,6 +6,7 @@ export class User implements UserServerModel { email: string role: UserRole displayNSFW: boolean + videoQuota: number createdAt: Date constructor (hash: { @@ -13,6 +14,7 @@ export class User implements UserServerModel { username: string, email: string, role: UserRole, + videoQuota?: number, displayNSFW?: boolean, createdAt?: Date }) { @@ -20,9 +22,16 @@ export class User implements UserServerModel { this.username = hash.username this.email = hash.email this.role = hash.role - this.displayNSFW = hash.displayNSFW - if (hash.createdAt) { + if (hash.videoQuota !== undefined) { + this.videoQuota = hash.videoQuota + } + + if (hash.displayNSFW !== undefined) { + this.displayNSFW = hash.displayNSFW + } + + if (hash.createdAt !== undefined) { this.createdAt = hash.createdAt } } diff --git a/client/tslint.json b/client/tslint.json index cfad2a5d9..b1e211ee9 100644 --- a/client/tslint.json +++ b/client/tslint.json @@ -4,7 +4,6 @@ "rules": { "no-inferrable-types": true, "eofline": true, - "indent": ["spaces"], "max-line-length": [true, 140], "no-floating-promises": false, "no-unused-variable": false, // Bug, wait TypeScript 2.4 diff --git a/client/yarn.lock b/client/yarn.lock index 0fc5ec418..9478e23b2 100644 --- a/client/yarn.lock +++ b/client/yarn.lock @@ -6740,9 +6740,9 @@ tslint-loader@^3.3.0: rimraf "^2.4.4" semver "^5.3.0" -tslint@^5.4.3: - version "5.6.0" - resolved "https://registry.yarnpkg.com/tslint/-/tslint-5.6.0.tgz#088aa6c6026623338650b2900828ab3edf59f6cf" +tslint@^5.7.0: + version "5.7.0" + resolved "https://registry.yarnpkg.com/tslint/-/tslint-5.7.0.tgz#c25e0d0c92fa1201c2bc30e844e08e682b4f3552" dependencies: babel-code-frame "^6.22.0" colors "^1.1.2" @@ -6753,7 +6753,7 @@ tslint@^5.4.3: resolve "^1.3.2" semver "^5.3.0" tslib "^1.7.1" - tsutils "^2.7.1" + tsutils "^2.8.1" tsml@1.0.1: version "1.0.1" @@ -6763,9 +6763,9 @@ tsutils@^1.4.0: version "1.9.1" resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-1.9.1.tgz#b9f9ab44e55af9681831d5f28d0aeeaf5c750cb0" -tsutils@^2.7.1: - version "2.8.1" - resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-2.8.1.tgz#3771404e7ca9f0bedf5d919a47a4b1890a68efff" +tsutils@^2.8.1: + version "2.8.2" + resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-2.8.2.tgz#2c1486ba431260845b0ac6f902afd9d708a8ea6a" dependencies: tslib "^1.7.1" @@ -6806,9 +6806,9 @@ typedarray@^0.0.6: version "0.0.6" resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" -typescript@~2.4.0: - version "2.4.2" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.4.2.tgz#f8395f85d459276067c988aa41837a8f82870844" +typescript@^2.5.2: + version "2.5.2" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.5.2.tgz#038a95f7d9bbb420b1bf35ba31d4c5c1dd3ffe34" uglify-js@3.0.x, uglify-js@^3.0.6: version "3.0.28" -- cgit v1.2.3