diff --git a/frontend/src/app/operations/users.operations.ts b/frontend/src/app/operations/users.operations.ts index a315217..a265c72 100644 --- a/frontend/src/app/operations/users.operations.ts +++ b/frontend/src/app/operations/users.operations.ts @@ -62,8 +62,12 @@ export class UsersOperation { return true; } catch (e) { - console.error(e); - return false; + if (e.response.status || e.response.status === 409) { + throw new Error('User with that name already exists!'); + } else { + console.error(e); + return false; + } } } diff --git a/frontend/src/app/pages/create-user/create-user.component.html b/frontend/src/app/pages/create-user/create-user.component.html index 9d85d84..0c00703 100644 --- a/frontend/src/app/pages/create-user/create-user.component.html +++ b/frontend/src/app/pages/create-user/create-user.component.html @@ -6,7 +6,7 @@
- +
Name can not be empty.
@@ -14,7 +14,7 @@
- +
Password is required.
diff --git a/frontend/src/app/pages/create-user/create-user.component.ts b/frontend/src/app/pages/create-user/create-user.component.ts index 4f25fd3..02d2d27 100644 --- a/frontend/src/app/pages/create-user/create-user.component.ts +++ b/frontend/src/app/pages/create-user/create-user.component.ts @@ -25,7 +25,7 @@ export class CreateUserComponent implements OnInit { private createForm() { this.userForm = this.fb.group({ name: ['', Validators.required], - type: ['user', Validators.required], + type: ['user'], password: ['', Validators.required], password2: [''] }, { validator: PasswordValidationUtil.matchPassword }); diff --git a/frontend/src/app/pages/edit-user/edit-user.component.html b/frontend/src/app/pages/edit-user/edit-user.component.html index ae99287..d840264 100644 --- a/frontend/src/app/pages/edit-user/edit-user.component.html +++ b/frontend/src/app/pages/edit-user/edit-user.component.html @@ -1,3 +1,38 @@ -

- Edit user works! -

\ No newline at end of file +
+
+

Edit user {{ username }}

+ +
+ +
+ + +
+ Name can not be empty. +
+
+ +
+ + +
+ +
+ + +
+ Passwords do not match. +
+
+ +
+ + +
+ + +
+
+
\ No newline at end of file diff --git a/frontend/src/app/pages/edit-user/edit-user.component.ts b/frontend/src/app/pages/edit-user/edit-user.component.ts index f1f5102..398d9b2 100644 --- a/frontend/src/app/pages/edit-user/edit-user.component.ts +++ b/frontend/src/app/pages/edit-user/edit-user.component.ts @@ -1,19 +1,82 @@ -import { ActivatedRoute } from '@angular/router'; +import { UsersOperation } from './../../operations/users.operations'; +import { ModalService } from './../../services/modal.service'; +import { ActivatedRoute, ParamMap, Router } from '@angular/router'; +import { FormGroup, Validators, FormBuilder } from '@angular/forms'; import { Component, OnInit } from '@angular/core'; +import { ModalOptionsDatatype } from '../../datatypes/modal-options.datatype'; +import { PasswordValidationUtil } from '../../utils/password-validation.util'; @Component({ - selector: 'app-edit-user', + selector: 'app-create-user', templateUrl: './edit-user.component.html', styleUrls: ['./edit-user.component.scss'] }) export class EditUserComponent implements OnInit { - public type: string; + public userForm: FormGroup; - constructor(private route: ActivatedRoute) { } + public isNative = false; + public username = ''; + public userId = 0; + + constructor(private fb: FormBuilder, private route: ActivatedRoute, private users: UsersOperation, + private router: Router, private modal: ModalService) { } ngOnInit() { + this.createForm(); + this.route.paramMap.subscribe((params) => this.initControl(params)); } + private async initControl(params: ParamMap) { + this.userId = +params.get('userId'); + + const user = await this.users.getSingle(this.userId); + + this.username = user.name; + this.isNative = user.native; + + this.userForm.reset({ + name: user.name, + type: user.type + }); + } + + private createForm() { + this.userForm = this.fb.group({ + name: ['', Validators.required], + type: ['user'], + password: [''], + password2: [''] + }, { validator: PasswordValidationUtil.matchPassword }); + } + + public async onSubmit() { + try { + const v = this.userForm.value; + + if (this.isNative) { + const name = this.userForm.controls['name'].dirty ? v.name : null; + const password = v.password !== '' ? v.password : null; + await this.users.updateUser(this.userId, name, v.type, password); + } else { + await this.users.updateUser(this.userId, null, v.type); + } + + this.userForm.reset({ + name: v.name, + type: v.type, + password: '', + password2: '' + }); + } catch (e) { + await this.modal.showMessage(new ModalOptionsDatatype({ + heading: 'Error', + body: e.message, + acceptText: 'OK', + dismisText: '', + acceptClass: 'warning' + })); + } + } } diff --git a/frontend/src/app/partials/modal-container/modal-container.component.html b/frontend/src/app/partials/modal-container/modal-container.component.html index 739fafc..31b5963 100644 --- a/frontend/src/app/partials/modal-container/modal-container.component.html +++ b/frontend/src/app/partials/modal-container/modal-container.component.html @@ -9,7 +9,8 @@
diff --git a/frontend/src/app/partials/modal-container/modal-container.component.ts b/frontend/src/app/partials/modal-container/modal-container.component.ts index dfde14a..79decd1 100644 --- a/frontend/src/app/partials/modal-container/modal-container.component.ts +++ b/frontend/src/app/partials/modal-container/modal-container.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, ElementRef, ViewChild } from '@angular/core'; import { ModalService } from '../../services/modal.service'; import { ModalOptionsDatatype } from '../../datatypes/modal-options.datatype'; @@ -17,6 +17,8 @@ export class ModalContainerComponent implements OnInit { acceptClass: 'primary' }); + @ViewChild('acceptButton') private acceptButton: ElementRef; + public show = false; public animate = false; @@ -42,6 +44,8 @@ export class ModalContainerComponent implements OnInit { this.show = true; window.setTimeout(() => this.animate = true, 50); + window.setTimeout(() => this.acceptButton.nativeElement.focus(), 50); + return new Promise((resolve, reject) => { this.currentResolve = resolve; this.currentReject = reject; @@ -61,6 +65,10 @@ export class ModalContainerComponent implements OnInit { */ onDismis() { this.hideModal(); + if (this.options.dismisText.length === 0) { + this.onAccept(); + return; + } if (this.currentReject) { this.currentReject(); this.currentResolve = null;