From f5c2f626df77ec39ce10dbed1c4e75c27dc9d663 Mon Sep 17 00:00:00 2001 From: Pablo Bonilla Date: Thu, 8 Jul 2021 18:33:54 -0600 Subject: [PATCH] Update retrieve data from usuario extra to display on profile --- .../account/settings/settings.component.html | 345 +++++++----------- .../account/settings/settings.component.ts | 182 +++++++-- .../usuario-extra-update.component.html | 119 ++++++ 3 files changed, 403 insertions(+), 243 deletions(-) diff --git a/src/main/webapp/app/account/settings/settings.component.html b/src/main/webapp/app/account/settings/settings.component.html index e906001..6fbf3d5 100644 --- a/src/main/webapp/app/account/settings/settings.component.html +++ b/src/main/webapp/app/account/settings/settings.component.html @@ -163,7 +163,9 @@ - --> + +------------------------------------------------------------------------------- +-->
@@ -178,263 +180,184 @@
-
+ +
- + - -
+
- Your email is required. - - - - Your email is invalid. - - - - Your email is required to be at least 5 characters. - - - - Your email cannot be longer than 100 characters. + This field is required.
- - - -
Nombre + +
+ + This field is required. + +
+
+
+
+
+ - - Your first name is required. - - - - Your first name is required to be at least 1 character. - - - - Your first name cannot be longer than 50 characters. - +
+
+
+
- + + - + --> diff --git a/src/main/webapp/app/account/settings/settings.component.ts b/src/main/webapp/app/account/settings/settings.component.ts index 4740dc8..dee036d 100644 --- a/src/main/webapp/app/account/settings/settings.component.ts +++ b/src/main/webapp/app/account/settings/settings.component.ts @@ -1,59 +1,177 @@ import { Component, OnInit } from '@angular/core'; +import { HttpResponse } from '@angular/common/http'; import { FormBuilder, Validators } from '@angular/forms'; -import { TranslateService } from '@ngx-translate/core'; +import { ActivatedRoute } from '@angular/router'; +import { Observable } from 'rxjs'; +import { finalize, map } from 'rxjs/operators'; +import * as dayjs from 'dayjs'; +import { DATE_TIME_FORMAT } from 'app/config/input.constants'; +import { IUser } from 'app/entities/user/user.model'; +import { UserService } from 'app/entities/user/user.service'; +import { IPlantilla } from 'app/entities/plantilla/plantilla.model'; +import { PlantillaService } from 'app/entities/plantilla/service/plantilla.service'; +import { IUsuarioExtra, UsuarioExtra } from 'app/entities/usuario-extra/usuario-extra.model'; +import { UsuarioExtraService } from 'app/entities/usuario-extra/service/usuario-extra.service'; import { AccountService } from 'app/core/auth/account.service'; -import { Account } from 'app/core/auth/account.model'; -import { LANGUAGES } from 'app/config/language.constants'; @Component({ selector: 'jhi-settings', templateUrl: './settings.component.html', }) export class SettingsComponent implements OnInit { - account!: Account; - success = false; - languages = LANGUAGES; - settingsForm = this.fb.group({ - firstName: [undefined, [Validators.required, Validators.minLength(1), Validators.maxLength(50)]], - lastName: [undefined, [Validators.required, Validators.minLength(1), Validators.maxLength(50)]], - email: [undefined, [Validators.required, Validators.minLength(5), Validators.maxLength(254), Validators.email]], - langKey: [undefined], + isSaving = false; + + usersSharedCollection: IUser[] = []; + plantillasSharedCollection: IPlantilla[] = []; + + editForm = this.fb.group({ + email: [null, [Validators.required]], + id: [], + nombre: [null, [Validators.required]], + iconoPerfil: [], + fechaNacimiento: [], + estado: [null, [Validators.required]], + user: [], + plantillas: [], }); - constructor(private accountService: AccountService, private fb: FormBuilder, private translateService: TranslateService) {} + usuarioExtra: UsuarioExtra | null = null; + + constructor( + protected usuarioExtraService: UsuarioExtraService, + protected userService: UserService, + protected plantillaService: PlantillaService, + protected activatedRoute: ActivatedRoute, + protected fb: FormBuilder, + protected accountService: AccountService + ) {} ngOnInit(): void { - this.accountService.identity().subscribe(account => { - if (account) { - this.settingsForm.patchValue({ - firstName: account.firstName, - lastName: account.lastName, - email: account.email, - langKey: account.langKey, - }); + // Get jhi_user and usuario_extra information + this.accountService.getAuthenticationState().subscribe(account => { + if (account !== null) { + this.usuarioExtraService.find(account.id).subscribe(usuarioExtra => { + this.usuarioExtra = usuarioExtra.body; + if (this.usuarioExtra !== null) { + if (this.usuarioExtra.id === undefined) { + const today = dayjs().startOf('day'); + this.usuarioExtra.fechaNacimiento = today; + } + this.updateForm(this.usuarioExtra); + } - this.account = account; + // this.loadRelationshipsOptions(); + }); } }); + + // this.activatedRoute.data.subscribe(({ usuarioExtra }) => { + + // }); + } + + previousState(): void { + window.history.back(); } save(): void { - this.success = false; + this.isSaving = true; + const usuarioExtra = this.createFromForm(); + if (usuarioExtra.id !== undefined) { + this.subscribeToSaveResponse(this.usuarioExtraService.update(usuarioExtra)); + } else { + this.subscribeToSaveResponse(this.usuarioExtraService.create(usuarioExtra)); + } + } - this.account.firstName = this.settingsForm.get('firstName')!.value; - this.account.lastName = this.settingsForm.get('lastName')!.value; - this.account.email = this.settingsForm.get('email')!.value; - this.account.langKey = this.settingsForm.get('langKey')!.value; + trackUserById(index: number, item: IUser): number { + return item.id!; + } - this.accountService.save(this.account).subscribe(() => { - this.success = true; + trackPlantillaById(index: number, item: IPlantilla): number { + return item.id!; + } - this.accountService.authenticate(this.account); - - if (this.account.langKey !== this.translateService.currentLang) { - this.translateService.use(this.account.langKey); + getSelectedPlantilla(option: IPlantilla, selectedVals?: IPlantilla[]): IPlantilla { + if (selectedVals) { + for (const selectedVal of selectedVals) { + if (option.id === selectedVal.id) { + return selectedVal; + } } + } + return option; + } + + protected subscribeToSaveResponse(result: Observable>): void { + result.pipe(finalize(() => this.onSaveFinalize())).subscribe( + () => this.onSaveSuccess(), + () => this.onSaveError() + ); + } + + protected onSaveSuccess(): void { + this.previousState(); + } + + protected onSaveError(): void { + // Api for inheritance. + } + + protected onSaveFinalize(): void { + this.isSaving = false; + } + + protected updateForm(usuarioExtra: IUsuarioExtra): void { + this.editForm.patchValue({ + email: usuarioExtra.user?.login, + id: usuarioExtra.id, + nombre: usuarioExtra.nombre, + iconoPerfil: usuarioExtra.iconoPerfil, + fechaNacimiento: usuarioExtra.fechaNacimiento ? usuarioExtra.fechaNacimiento.format(DATE_TIME_FORMAT) : null, + estado: usuarioExtra.estado, + user: usuarioExtra.user, + plantillas: usuarioExtra.plantillas, }); + + this.usersSharedCollection = this.userService.addUserToCollectionIfMissing(this.usersSharedCollection, usuarioExtra.user); + this.plantillasSharedCollection = this.plantillaService.addPlantillaToCollectionIfMissing( + this.plantillasSharedCollection, + ...(usuarioExtra.plantillas ?? []) + ); + } + + protected loadRelationshipsOptions(): void { + this.userService + .query() + .pipe(map((res: HttpResponse) => res.body ?? [])) + .pipe(map((users: IUser[]) => this.userService.addUserToCollectionIfMissing(users, this.editForm.get('user')!.value))) + .subscribe((users: IUser[]) => (this.usersSharedCollection = users)); + + this.plantillaService + .query() + .pipe(map((res: HttpResponse) => res.body ?? [])) + .pipe( + map((plantillas: IPlantilla[]) => + this.plantillaService.addPlantillaToCollectionIfMissing(plantillas, ...(this.editForm.get('plantillas')!.value ?? [])) + ) + ) + .subscribe((plantillas: IPlantilla[]) => (this.plantillasSharedCollection = plantillas)); + } + + protected createFromForm(): IUsuarioExtra { + return { + ...new UsuarioExtra(), + id: this.editForm.get(['id'])!.value, + nombre: this.editForm.get(['nombre'])!.value, + iconoPerfil: this.editForm.get(['iconoPerfil'])!.value, + fechaNacimiento: this.editForm.get(['fechaNacimiento'])!.value + ? dayjs(this.editForm.get(['fechaNacimiento'])!.value, DATE_TIME_FORMAT) + : undefined, + estado: this.editForm.get(['estado'])!.value, + user: this.editForm.get(['user'])!.value, + plantillas: this.editForm.get(['plantillas'])!.value, + }; } } diff --git a/src/main/webapp/app/entities/usuario-extra/update/usuario-extra-update.component.html b/src/main/webapp/app/entities/usuario-extra/update/usuario-extra-update.component.html index d75c700..1f2f81e 100644 --- a/src/main/webapp/app/entities/usuario-extra/update/usuario-extra-update.component.html +++ b/src/main/webapp/app/entities/usuario-extra/update/usuario-extra-update.component.html @@ -244,3 +244,122 @@ + +