2021-07-16 04:07:23 +00:00
|
|
|
import { Component, ContentChild, OnInit } from '@angular/core';
|
2021-07-14 02:27:28 +00:00
|
|
|
import { HttpErrorResponse, HttpResponse } from '@angular/common/http';
|
2021-07-03 21:48:27 +00:00
|
|
|
import { FormBuilder, Validators } from '@angular/forms';
|
2021-07-16 04:07:23 +00:00
|
|
|
import { Router, NavigationEnd, ActivatedRoute } from '@angular/router';
|
2021-07-09 00:33:54 +00:00
|
|
|
import { Observable } from 'rxjs';
|
|
|
|
import { finalize, map } from 'rxjs/operators';
|
|
|
|
import * as dayjs from 'dayjs';
|
2021-07-14 03:49:46 +00:00
|
|
|
import { DATE_FORMAT, DATE_TIME_FORMAT } from 'app/config/input.constants';
|
2021-07-09 00:33:54 +00:00
|
|
|
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';
|
2021-07-03 21:48:27 +00:00
|
|
|
import { AccountService } from 'app/core/auth/account.service';
|
2021-07-13 21:02:00 +00:00
|
|
|
import { LocalStorageService } from 'ngx-webstorage';
|
2021-07-14 02:27:28 +00:00
|
|
|
import { EMAIL_ALREADY_USED_TYPE, LOGIN_ALREADY_USED_TYPE } from '../../config/error.constants';
|
2021-07-15 02:04:35 +00:00
|
|
|
import { PasswordService } from '../password/password.service';
|
2021-07-03 21:48:27 +00:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'jhi-settings',
|
|
|
|
templateUrl: './settings.component.html',
|
|
|
|
})
|
|
|
|
export class SettingsComponent implements OnInit {
|
2021-07-16 04:07:23 +00:00
|
|
|
currentUrl = this.router.url;
|
2021-07-09 00:33:54 +00:00
|
|
|
isSaving = false;
|
2021-07-14 02:27:28 +00:00
|
|
|
success = false;
|
2021-07-15 21:08:53 +00:00
|
|
|
successPassword = false;
|
2021-07-17 02:38:06 +00:00
|
|
|
samePassword = false;
|
2021-07-14 02:27:28 +00:00
|
|
|
error = false;
|
2021-07-15 21:08:53 +00:00
|
|
|
errorPassword = false;
|
2021-07-15 02:04:35 +00:00
|
|
|
doNotMatch = false;
|
2021-07-09 00:33:54 +00:00
|
|
|
usersSharedCollection: IUser[] = [];
|
|
|
|
plantillasSharedCollection: IPlantilla[] = [];
|
2021-07-16 04:07:23 +00:00
|
|
|
showPassword = false;
|
2021-07-09 00:33:54 +00:00
|
|
|
|
2021-07-13 21:02:00 +00:00
|
|
|
isGoogle = this.localStorageService.retrieve('IsGoogle');
|
|
|
|
|
|
|
|
//Form info del usuario
|
2021-07-09 00:33:54 +00:00
|
|
|
editForm = this.fb.group({
|
|
|
|
email: [null, [Validators.required]],
|
|
|
|
id: [],
|
|
|
|
nombre: [null, [Validators.required]],
|
|
|
|
iconoPerfil: [],
|
|
|
|
fechaNacimiento: [],
|
|
|
|
estado: [null, [Validators.required]],
|
|
|
|
user: [],
|
|
|
|
plantillas: [],
|
2021-07-03 21:48:27 +00:00
|
|
|
});
|
|
|
|
|
2021-07-13 21:02:00 +00:00
|
|
|
//form de la contraseña
|
2021-07-09 02:48:27 +00:00
|
|
|
passwordForm = this.fb.group({
|
2021-07-18 05:47:01 +00:00
|
|
|
password: [null, [Validators.required, Validators.minLength(8), Validators.maxLength(50)]],
|
|
|
|
passwordNew: [null, [Validators.required, Validators.minLength(8), Validators.maxLength(50)]],
|
2021-07-15 02:04:35 +00:00
|
|
|
passwordNewConfirm: [null, [Validators.required, Validators.minLength(8), Validators.maxLength(50)]],
|
2021-07-09 02:48:27 +00:00
|
|
|
});
|
|
|
|
|
2021-07-09 00:33:54 +00:00
|
|
|
usuarioExtra: UsuarioExtra | null = null;
|
2021-07-09 02:48:27 +00:00
|
|
|
profileIcon: number = 1;
|
|
|
|
profileIcons: any[] = [
|
|
|
|
{ name: 'C1' },
|
|
|
|
{ name: 'C2' },
|
|
|
|
{ name: 'C3' },
|
|
|
|
{ name: 'C4' },
|
|
|
|
{ name: 'C5' },
|
|
|
|
{ name: 'C6' },
|
|
|
|
{ name: 'C7' },
|
|
|
|
{ name: 'C8' },
|
|
|
|
{ name: 'C9' },
|
|
|
|
{ name: 'C10' },
|
|
|
|
{ name: 'C11' },
|
|
|
|
{ name: 'C12' },
|
|
|
|
{ name: 'C13' },
|
|
|
|
{ name: 'C14' },
|
|
|
|
{ name: 'C15' },
|
|
|
|
{ name: 'C16' },
|
|
|
|
{ name: 'C17' },
|
|
|
|
{ name: 'C18' },
|
|
|
|
{ name: 'C19' },
|
|
|
|
{ name: 'C20' },
|
|
|
|
{ name: 'C21' },
|
|
|
|
{ name: 'C22' },
|
|
|
|
{ name: 'C23' },
|
|
|
|
{ name: 'C24' },
|
|
|
|
{ name: 'C25' },
|
|
|
|
{ name: 'C26' },
|
|
|
|
{ name: 'C27' },
|
|
|
|
{ name: 'C28' },
|
|
|
|
];
|
2021-07-09 00:33:54 +00:00
|
|
|
|
2021-07-16 04:07:23 +00:00
|
|
|
/* @ContentChild(IonInput) input: IonInput;*/
|
|
|
|
|
2021-07-09 00:33:54 +00:00
|
|
|
constructor(
|
|
|
|
protected usuarioExtraService: UsuarioExtraService,
|
|
|
|
protected userService: UserService,
|
|
|
|
protected plantillaService: PlantillaService,
|
|
|
|
protected activatedRoute: ActivatedRoute,
|
|
|
|
protected fb: FormBuilder,
|
2021-07-13 21:02:00 +00:00
|
|
|
protected accountService: AccountService,
|
2021-07-15 02:04:35 +00:00
|
|
|
private localStorageService: LocalStorageService,
|
2021-07-16 04:07:23 +00:00
|
|
|
protected passwordService: PasswordService,
|
|
|
|
private router: Router
|
2021-07-09 00:33:54 +00:00
|
|
|
) {}
|
2021-07-03 21:48:27 +00:00
|
|
|
|
|
|
|
ngOnInit(): void {
|
2021-07-09 00:33:54 +00:00
|
|
|
// 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);
|
|
|
|
}
|
2021-07-03 21:48:27 +00:00
|
|
|
|
2021-07-09 00:33:54 +00:00
|
|
|
// this.loadRelationshipsOptions();
|
|
|
|
});
|
2021-07-03 21:48:27 +00:00
|
|
|
}
|
|
|
|
});
|
2021-07-09 00:33:54 +00:00
|
|
|
|
2021-07-18 05:47:01 +00:00
|
|
|
//console.log(this.isGoogle);
|
2021-07-13 21:02:00 +00:00
|
|
|
|
2021-07-09 00:33:54 +00:00
|
|
|
// this.activatedRoute.data.subscribe(({ usuarioExtra }) => {
|
|
|
|
|
|
|
|
// });
|
2021-07-03 21:48:27 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 00:33:54 +00:00
|
|
|
previousState(): void {
|
|
|
|
window.history.back();
|
|
|
|
}
|
2021-07-03 21:48:27 +00:00
|
|
|
|
2021-07-14 02:27:28 +00:00
|
|
|
//Se manda la info a guardar
|
2021-07-09 00:33:54 +00:00
|
|
|
save(): void {
|
|
|
|
this.isSaving = true;
|
|
|
|
const usuarioExtra = this.createFromForm();
|
2021-07-14 02:27:28 +00:00
|
|
|
|
|
|
|
console.log(usuarioExtra.iconoPerfil);
|
|
|
|
console.log(usuarioExtra.fechaNacimiento);
|
|
|
|
|
|
|
|
this.subscribeToSaveResponse(this.usuarioExtraService.update(usuarioExtra));
|
2021-07-15 02:04:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
savePassword(): void {
|
2021-07-17 02:38:06 +00:00
|
|
|
this.successPassword = false;
|
|
|
|
this.doNotMatch = false;
|
|
|
|
this.samePassword = false;
|
|
|
|
this.errorPassword = false;
|
|
|
|
|
2021-07-15 21:08:53 +00:00
|
|
|
const passwordNew = this.passwordForm.get(['passwordNew'])!.value;
|
2021-07-17 02:38:06 +00:00
|
|
|
const passwordOld = this.passwordForm.get(['password'])!.value;
|
|
|
|
if (passwordOld == passwordNew) {
|
|
|
|
this.samePassword = true;
|
2021-07-15 02:04:35 +00:00
|
|
|
} else {
|
2021-07-17 02:38:06 +00:00
|
|
|
if (passwordNew !== this.passwordForm.get(['passwordNewConfirm'])!.value) {
|
|
|
|
(this.doNotMatch = true), (this.samePassword = false);
|
|
|
|
} else {
|
|
|
|
this.passwordService.save(passwordNew, passwordOld).subscribe(
|
|
|
|
() => (this.successPassword = true),
|
|
|
|
|
|
|
|
() => (this.errorPassword = true)
|
|
|
|
);
|
|
|
|
}
|
2021-07-15 02:04:35 +00:00
|
|
|
}
|
2021-07-09 00:33:54 +00:00
|
|
|
}
|
2021-07-03 21:48:27 +00:00
|
|
|
|
2021-07-09 00:33:54 +00:00
|
|
|
trackUserById(index: number, item: IUser): number {
|
|
|
|
return item.id!;
|
|
|
|
}
|
2021-07-03 21:48:27 +00:00
|
|
|
|
2021-07-09 00:33:54 +00:00
|
|
|
trackPlantillaById(index: number, item: IPlantilla): number {
|
|
|
|
return item.id!;
|
|
|
|
}
|
2021-07-03 21:48:27 +00:00
|
|
|
|
2021-07-09 00:33:54 +00:00
|
|
|
getSelectedPlantilla(option: IPlantilla, selectedVals?: IPlantilla[]): IPlantilla {
|
|
|
|
if (selectedVals) {
|
|
|
|
for (const selectedVal of selectedVals) {
|
|
|
|
if (option.id === selectedVal.id) {
|
|
|
|
return selectedVal;
|
|
|
|
}
|
2021-07-03 21:48:27 +00:00
|
|
|
}
|
2021-07-09 00:33:54 +00:00
|
|
|
}
|
|
|
|
return option;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected subscribeToSaveResponse(result: Observable<HttpResponse<IUsuarioExtra>>): void {
|
|
|
|
result.pipe(finalize(() => this.onSaveFinalize())).subscribe(
|
2021-07-17 02:38:06 +00:00
|
|
|
() => ((this.success = true), this.windowReload()),
|
2021-07-14 02:27:28 +00:00
|
|
|
response => this.processError(response)
|
2021-07-09 00:33:54 +00:00
|
|
|
);
|
|
|
|
}
|
2021-07-17 02:38:06 +00:00
|
|
|
windowReload() {
|
|
|
|
this.router.navigate(['account/settings']).then(() => {
|
|
|
|
window.location.reload();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-14 02:27:28 +00:00
|
|
|
processError(response: HttpErrorResponse): void {
|
|
|
|
if (response.status === 400) {
|
|
|
|
this.error = true;
|
|
|
|
}
|
|
|
|
}
|
2021-07-09 00:33:54 +00:00
|
|
|
|
|
|
|
protected onSaveSuccess(): void {
|
|
|
|
this.previousState();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected onSaveError(): void {
|
|
|
|
// Api for inheritance.
|
|
|
|
}
|
|
|
|
|
|
|
|
protected onSaveFinalize(): void {
|
|
|
|
this.isSaving = false;
|
|
|
|
}
|
|
|
|
|
2021-07-13 21:02:00 +00:00
|
|
|
//Llena el formulario para que se vea en pantalla
|
2021-07-09 00:33:54 +00:00
|
|
|
protected updateForm(usuarioExtra: IUsuarioExtra): void {
|
|
|
|
this.editForm.patchValue({
|
|
|
|
email: usuarioExtra.user?.login,
|
|
|
|
id: usuarioExtra.id,
|
|
|
|
nombre: usuarioExtra.nombre,
|
|
|
|
iconoPerfil: usuarioExtra.iconoPerfil,
|
2021-07-14 03:49:46 +00:00
|
|
|
fechaNacimiento: usuarioExtra.fechaNacimiento ? usuarioExtra.fechaNacimiento.format(DATE_FORMAT) : null,
|
2021-07-09 00:33:54 +00:00
|
|
|
estado: usuarioExtra.estado,
|
|
|
|
user: usuarioExtra.user,
|
|
|
|
plantillas: usuarioExtra.plantillas,
|
2021-07-03 21:48:27 +00:00
|
|
|
});
|
2021-07-09 00:33:54 +00:00
|
|
|
|
2021-07-09 02:48:27 +00:00
|
|
|
// Update swiper
|
2021-07-14 02:27:28 +00:00
|
|
|
this.profileIcon = usuarioExtra.iconoPerfil!;
|
2021-07-18 05:47:01 +00:00
|
|
|
|
|
|
|
console.log(this.profileIcon);
|
2021-07-09 02:48:27 +00:00
|
|
|
this.profileIcons.forEach(icon => {
|
2021-07-18 05:47:01 +00:00
|
|
|
if (icon.name.split('C')[1] === this.profileIcon) {
|
2021-07-09 02:48:27 +00:00
|
|
|
icon.class = 'active';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-07-09 00:33:54 +00:00
|
|
|
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<IUser[]>) => 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<IPlantilla[]>) => 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,
|
2021-07-14 02:27:28 +00:00
|
|
|
iconoPerfil: this.profileIcon,
|
2021-07-09 00:33:54 +00:00
|
|
|
fechaNacimiento: this.editForm.get(['fechaNacimiento'])!.value
|
2021-07-14 03:49:46 +00:00
|
|
|
? dayjs(this.editForm.get(['fechaNacimiento'])!.value, DATE_FORMAT)
|
2021-07-09 00:33:54 +00:00
|
|
|
: undefined,
|
|
|
|
estado: this.editForm.get(['estado'])!.value,
|
|
|
|
user: this.editForm.get(['user'])!.value,
|
|
|
|
plantillas: this.editForm.get(['plantillas'])!.value,
|
|
|
|
};
|
2021-07-03 21:48:27 +00:00
|
|
|
}
|
2021-07-09 02:48:27 +00:00
|
|
|
|
|
|
|
selectIcon(event: MouseEvent): void {
|
|
|
|
if (event.target instanceof Element) {
|
|
|
|
document.querySelectorAll('.active').forEach(e => e.classList.remove('active'));
|
|
|
|
event.target.classList.add('active');
|
|
|
|
this.profileIcon = +event.target.getAttribute('id')! + 1;
|
2021-07-14 02:27:28 +00:00
|
|
|
|
2021-07-18 05:47:01 +00:00
|
|
|
//console.log(this.profileIcon);
|
2021-07-09 02:48:27 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-03 21:48:27 +00:00
|
|
|
}
|