fixed bug where a new user couldn't be created

This commit is contained in:
Dragos 2021-05-27 09:49:30 +03:00
parent 7fd17cecc9
commit 75005db046

View File

@ -15,8 +15,7 @@ import { ToastrService } from 'ngx-toastr';
templateUrl: './user-editor.component.html', templateUrl: './user-editor.component.html',
styleUrls: ['./user-editor.component.scss'] styleUrls: ['./user-editor.component.scss']
}) })
export class UserEditorComponent implements OnInit export class UserEditorComponent implements OnInit {
{
@Input() @Input()
user: User; user: User;
@ -35,8 +34,7 @@ export class UserEditorComponent implements OnInit
private readonly router: Router, private readonly router: Router,
private readonly fb: FormBuilder, private readonly fb: FormBuilder,
private readonly securityService: SecurityService, private readonly securityService: SecurityService,
private readonly toastr: ToastrService) private readonly toastr: ToastrService) { // When the user navigates away from this route, hide the modal
{ // When the user navigates away from this route, hide the modal
router.events router.events
.pipe( .pipe(
takeUntil(this.destroy$), takeUntil(this.destroy$),
@ -46,8 +44,7 @@ export class UserEditorComponent implements OnInit
} }
// ---------------------------------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------------------------------
private createForm() private createForm() {
{
this.editorForm = this.fb.group( this.editorForm = this.fb.group(
{ {
id: [this.user?.id], id: [this.user?.id],
@ -71,8 +68,7 @@ export class UserEditorComponent implements OnInit
} }
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
private passwordsValidator: ValidatorFn = (group: FormGroup): ValidationErrors | null => private passwordsValidator: ValidatorFn = (group: FormGroup): ValidationErrors | null => {
{
if (!this.changePassword && !!this.user) if (!this.changePassword && !!this.user)
return null; return null;
@ -96,47 +92,47 @@ export class UserEditorComponent implements OnInit
} }
// ---------------------------------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------------------------------
close() close() {
{
this.modalRef.hide(); this.modalRef.hide();
} }
// ---------------------------------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------------------------------
saveChanges() saveChanges() {
{
this.working = true; this.working = true;
let observable: Observable<UserResponse>; let observable: Observable<UserResponse>;
const changes = this.editorForm.getRawValue(); const changes = this.editorForm.getRawValue();
if (this.changePassword) if (this.changePassword) {
{
observable = this.securityService.changeUserPassword(this.user.id, changes.password, changes.passwordConfirmation); observable = this.securityService.changeUserPassword(this.user.id, changes.password, changes.passwordConfirmation);
} }
else else {
{ const user: any = {
const user = {
login: changes.username, login: changes.username,
email: changes.email, email: changes.email,
firstName: changes.firstName, password: changes.password.password
lastName: changes.lastName,
phone: changes.phone,
password: changes.password
}; };
if (changes.firstName)
user.firstName = changes.firstName;
if (changes.lastName)
user.lastName = changes.lastName;
if (changes.phone)
user.phone = changes.phone;
observable = this.user observable = this.user
? this.securityService.editUser(this.user.id, user) ? this.securityService.editUser(this.user.id, user)
: this.securityService.addUser(user); : this.securityService.addUser(user);
} }
observable.subscribe(x => observable.subscribe(x => {
{
this.save.next(x as User); this.save.next(x as User);
this.close(); this.close();
}, err => }, err => {
{
this.toastr.error(err.error.message); this.toastr.error(err.error.message);
this.working = false; this.working = false;
@ -144,8 +140,7 @@ export class UserEditorComponent implements OnInit
} }
// ---------------------------------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------------------------------
ngOnInit(): void ngOnInit(): void {
{
if (!this.user) if (!this.user)
this.changePassword = false; this.changePassword = false;