datasurvey/src/main/webapp/app/entities/encuesta/list/encuesta.component.ts

578 lines
19 KiB
TypeScript
Raw Normal View History

2021-07-14 06:23:05 +00:00
import { Component, OnInit, AfterViewInit } from '@angular/core';
2021-07-03 21:48:27 +00:00
import { HttpResponse } from '@angular/common/http';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { IEncuesta, Encuesta } from '../encuesta.model';
2021-07-03 21:48:27 +00:00
import { EncuestaService } from '../service/encuesta.service';
import { EncuestaDeleteDialogComponent } from '../delete/encuesta-delete-dialog.component';
import { FormBuilder, Validators } from '@angular/forms';
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 { ICategoria } from 'app/entities/categoria/categoria.model';
import { CategoriaService } from 'app/entities/categoria/service/categoria.service';
2021-07-11 05:13:52 +00:00
import { IUsuarioExtra, UsuarioExtra } from 'app/entities/usuario-extra/usuario-extra.model';
import { UsuarioExtraService } from 'app/entities/usuario-extra/service/usuario-extra.service';
2021-07-11 05:13:52 +00:00
import { EstadoEncuesta } from 'app/entities/enumerations/estado-encuesta.model';
import { AccountService } from 'app/core/auth/account.service';
import { Account } from 'app/core/auth/account.model';
2021-07-14 06:23:05 +00:00
import { Router } from '@angular/router';
2021-07-25 03:03:23 +00:00
import { EncuestaPublishDialogComponent } from '../encuesta-publish-dialog/encuesta-publish-dialog.component';
2021-07-25 02:34:40 +00:00
import { IUser } from '../../user/user.model';
import {
faShareAlt,
faLock,
faUnlock,
faCalendarAlt,
faEdit,
faCopy,
faFile,
faTrashAlt,
faPlus,
faStar,
2021-07-24 04:50:40 +00:00
faUpload,
faPollH,
} from '@fortawesome/free-solid-svg-icons';
import * as $ from 'jquery';
import { EncuestaCompartirDialogComponent } from '../encuesta-compartir-dialog/encuesta-compartir-dialog.component';
2021-07-03 21:48:27 +00:00
@Component({
selector: 'jhi-encuesta',
templateUrl: './encuesta.component.html',
})
2021-07-14 06:23:05 +00:00
export class EncuestaComponent implements OnInit, AfterViewInit {
// Icons
faShareAlt = faShareAlt;
2021-07-16 04:26:34 +00:00
faLock = faLock;
faUnlock = faUnlock;
faCalendarAlt = faCalendarAlt;
faEdit = faEdit;
faCopy = faCopy;
faFile = faFile;
faTrashAlt = faTrashAlt;
faPlus = faPlus;
faStar = faStar;
2021-07-24 04:50:40 +00:00
faUpload = faUpload;
isPublished: Boolean = false;
2021-07-24 04:50:40 +00:00
successPublished = false;
faPollH = faPollH;
2021-07-11 05:13:52 +00:00
account: Account | null = null;
usuarioExtra: UsuarioExtra | null = null;
2021-07-24 02:55:45 +00:00
estadoDeleted = EstadoEncuesta.DELETED;
2021-07-11 05:13:52 +00:00
2021-07-03 21:48:27 +00:00
encuestas?: IEncuesta[];
isLoading = false;
2021-07-24 04:50:40 +00:00
selectedSurvey?: IEncuesta | null = null;
2021-07-26 06:00:03 +00:00
idEncuesta: number | null = null;
isSaving = false;
categoriasSharedCollection: ICategoria[] = [];
usuarioExtrasSharedCollection: IUsuarioExtra[] = [];
2021-07-25 02:34:40 +00:00
userSharedCollection: IUser[] = [];
2021-07-22 08:49:12 +00:00
encuestaencontrada: IEncuesta | null = null;
2021-07-24 22:52:36 +00:00
public searchString: string;
2021-07-22 04:30:54 +00:00
public accesoEncuesta: string;
//public categoriaEncuesta: string;
public estadoEncuesta: string;
editForm = this.fb.group({
id: [],
nombre: [null, [Validators.required, Validators.minLength(1), Validators.maxLength(50)]],
descripcion: [],
2021-07-11 05:13:52 +00:00
// fechaCreacion: [null, [Validators.required]],
// fechaPublicacion: [],
// fechaFinalizar: [],
// fechaFinalizada: [],
// calificacion: [null, [Validators.required]],
acceso: [null, [Validators.required]],
2021-07-11 05:13:52 +00:00
// contrasenna: [],
// estado: [null, [Validators.required]],
categoria: [null, [Validators.required]],
// usuarioExtra: [],
});
2021-07-30 04:15:36 +00:00
surveyEditForm = this.fb.group({
id: [],
nombre: [null, [Validators.required, Validators.minLength(1), Validators.maxLength(50)]],
descripcion: [],
acceso: [null, [Validators.required]],
categoria: [null, [Validators.required]],
});
2021-07-13 07:28:52 +00:00
createAnother: Boolean = false;
selectedSurveyId: number | null = null;
2021-07-13 07:28:52 +00:00
constructor(
protected encuestaService: EncuestaService,
protected modalService: NgbModal,
protected categoriaService: CategoriaService,
protected usuarioExtraService: UsuarioExtraService,
protected activatedRoute: ActivatedRoute,
2021-07-11 05:13:52 +00:00
protected fb: FormBuilder,
2021-07-14 06:23:05 +00:00
protected accountService: AccountService,
protected router: Router
2021-07-24 05:57:09 +00:00
) {
this.searchString = '';
2021-07-22 04:30:54 +00:00
this.accesoEncuesta = '';
this.estadoEncuesta = '';
2021-07-24 05:57:09 +00:00
}
2021-07-03 21:48:27 +00:00
2021-07-13 07:28:52 +00:00
resetForm(): void {
this.editForm.reset();
}
2021-07-03 21:48:27 +00:00
loadAll(): void {
this.isLoading = true;
if (this.isAdmin()) {
this.usuarioExtraService
.retrieveAllPublicUsers()
.pipe(finalize(() => this.loadPublicUser()))
.subscribe(res => {
this.userSharedCollection = res;
});
} else {
this.loadEncuestas();
}
2021-07-25 02:34:40 +00:00
}
loadPublicUser(): void {
this.usuarioExtraService
.retrieveAllPublicUsers()
.pipe(finalize(() => this.loadUserExtras()))
.subscribe(res => {
this.userSharedCollection = res;
});
}
loadUserExtras() {
this.usuarioExtraService
.query()
.pipe(finalize(() => this.loadEncuestas()))
2021-07-25 02:34:40 +00:00
.subscribe(
(res: HttpResponse<IUsuarioExtra[]>) => {
this.isLoading = false;
this.usuarioExtrasSharedCollection = res.body ?? [];
this.usuarioExtrasSharedCollection.forEach(uE => {
uE.user = this.userSharedCollection?.find(pU => pU.id == uE.user?.id);
});
},
() => {
this.isLoading = false;
2021-07-24 01:25:38 +00:00
}
2021-07-25 02:34:40 +00:00
);
2021-07-03 21:48:27 +00:00
}
loadEncuestas() {
this.encuestaService.query().subscribe(
(res: HttpResponse<IEncuesta[]>) => {
this.isLoading = false;
const tmpEncuestas = res.body ?? [];
2021-08-12 02:45:11 +00:00
// Fix calificacion
tmpEncuestas.forEach(encuesta => {
const _calificacion = encuesta.calificacion;
encuesta.calificacion = Number(_calificacion?.toString().split('.')[0]);
});
if (this.isAdmin()) {
this.encuestas = tmpEncuestas.filter(e => e.estado !== EstadoEncuesta.DELETED);
this.encuestas.forEach(e => {
e.usuarioExtra = this.usuarioExtrasSharedCollection?.find(pU => pU.id == e.usuarioExtra?.id);
});
} else {
this.encuestas = tmpEncuestas
.filter(e => e.usuarioExtra?.id === this.usuarioExtra?.id)
.filter(e => e.estado !== EstadoEncuesta.DELETED);
}
},
() => {
this.isLoading = false;
}
);
}
2021-07-03 21:48:27 +00:00
ngOnInit(): void {
2021-07-24 22:30:54 +00:00
this.searchString = '';
2021-07-22 04:30:54 +00:00
this.accesoEncuesta = '';
//this.categoriaEncuesta = '';
this.estadoEncuesta = '';
document.body.addEventListener('click', e => {
document.getElementById('contextmenu')!.classList.add('ds-contextmenu--closed');
document.getElementById('contextmenu')!.classList.remove('ds-contextmenu--open');
document.getElementById('contextmenu')!.style.maxHeight = '0%';
if (e.target) {
if (!(e.target as HTMLElement).classList.contains('ds-list--entity')) {
document.querySelectorAll('.ds-list--entity').forEach(e => {
e.classList.remove('active');
});
}
}
});
// this.activatedRoute.data.subscribe(({ encuesta }) => {
// if (encuesta.id === undefined) {
// const today = dayjs().startOf('day');
// encuesta.fechaCreacion = today;
// encuesta.fechaPublicacion = today;
// encuesta.fechaFinalizar = today;
// encuesta.fechaFinalizada = today;
// }
// this.updateForm(encuesta);
// });
2021-07-11 05:13:52 +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;
this.loadAll();
2021-07-25 02:34:40 +00:00
this.loadRelationshipsOptions();
2021-07-11 05:13:52 +00:00
if (this.usuarioExtra !== null) {
if (this.usuarioExtra.id === undefined) {
const today = dayjs().startOf('day');
this.usuarioExtra.fechaNacimiento = today;
}
}
// this.loadRelationshipsOptions();
});
}
});
2021-07-03 21:48:27 +00:00
}
ngAfterViewInit(): void {}
2021-07-14 06:23:05 +00:00
2021-07-24 03:03:21 +00:00
trackId(_index: number, item: IEncuesta): number {
2021-07-03 21:48:27 +00:00
return item.id!;
}
delete(encuesta: IEncuesta): void {
const modalRef = this.modalService.open(EncuestaDeleteDialogComponent, { size: 'lg', backdrop: 'static' });
modalRef.componentInstance.encuesta = encuesta;
// unsubscribe not needed because closed completes on modal close
modalRef.closed.subscribe(reason => {
if (reason === 'deleted') {
this.loadAll();
}
});
}
2021-07-22 08:49:12 +00:00
deleteSurvey(): void {
if (this.selectedSurveyId != null) {
this.getEncuesta(this.selectedSurveyId)
2021-07-22 08:49:12 +00:00
.pipe(
finalize(() => {
const modalRef = this.modalService.open(EncuestaDeleteDialogComponent, { size: 'lg', backdrop: 'static' });
modalRef.componentInstance.encuesta = this.encuestaencontrada;
modalRef.closed.subscribe(reason => {
if (reason === 'deleted') {
this.loadAll();
}
});
})
)
.subscribe(data => {
this.encuestaencontrada = data;
});
/*const modalRef = this.modalService.open(EncuestaDeleteDialogComponent, { size: 'lg', backdrop: 'static' });
modalRef.componentInstance.encuesta = this.getEncuesta(this.selectedSurvey)
.pipe(finalize(() =>
modalRef.closed.subscribe(reason => {
if (reason === 'deleted') {
this.loadAll();
}
})
))
.subscribe(data=> {
console.log(data);
//this.encuestaencontrada = data;
});
*/
// unsubscribe not needed because closed completes on modal close
}
}
getEncuesta(id: number) {
return this.encuestaService.findEncuesta(id);
}
previousState(): void {
window.history.back();
}
save(): void {
this.isSaving = true;
const encuesta = this.createFromForm();
2021-07-11 05:13:52 +00:00
if (encuesta.id !== undefined) {
this.subscribeToSaveResponse(this.encuestaService.update(encuesta));
} else {
this.subscribeToSaveResponse(this.encuestaService.create(encuesta));
}
}
2021-07-24 03:03:21 +00:00
trackCategoriaById(_index: number, item: ICategoria): number {
return item.id!;
}
2021-07-24 03:03:21 +00:00
trackUsuarioExtraById(_index: number, item: IUsuarioExtra): number {
return item.id!;
}
protected subscribeToSaveResponse(result: Observable<HttpResponse<IEncuesta>>): void {
result.pipe(finalize(() => this.onSaveFinalize())).subscribe(
() => this.onSaveSuccess(),
() => this.onSaveError()
);
}
2021-07-13 07:28:52 +00:00
createAnotherChange(event: any) {
this.createAnother = event.target.checked;
}
protected onSaveSuccess(): void {
2021-07-11 05:13:52 +00:00
// this.previousState();
// ($('#crearEncuesta') as any).modal('hide');
2021-07-13 07:28:52 +00:00
this.resetForm();
this.encuestas = [];
this.loadAll();
2021-07-13 07:28:52 +00:00
if (!this.createAnother) {
$('#cancelBtn').click();
}
}
protected onSaveError(): void {
// Api for inheritance.
}
protected onSaveFinalize(): void {
this.isSaving = false;
}
protected updateForm(encuesta: IEncuesta): void {
this.editForm.patchValue({
id: encuesta.id,
nombre: encuesta.nombre,
descripcion: encuesta.descripcion,
fechaCreacion: encuesta.fechaCreacion ? encuesta.fechaCreacion.format(DATE_TIME_FORMAT) : null,
fechaPublicacion: encuesta.fechaPublicacion ? encuesta.fechaPublicacion.format(DATE_TIME_FORMAT) : null,
fechaFinalizar: encuesta.fechaFinalizar ? encuesta.fechaFinalizar.format(DATE_TIME_FORMAT) : null,
fechaFinalizada: encuesta.fechaFinalizada ? encuesta.fechaFinalizada.format(DATE_TIME_FORMAT) : null,
calificacion: encuesta.calificacion,
acceso: encuesta.acceso,
contrasenna: encuesta.contrasenna,
estado: encuesta.estado,
categoria: encuesta.categoria,
usuarioExtra: encuesta.usuarioExtra,
});
this.categoriasSharedCollection = this.categoriaService.addCategoriaToCollectionIfMissing(
this.categoriasSharedCollection,
encuesta.categoria
);
this.usuarioExtrasSharedCollection = this.usuarioExtraService.addUsuarioExtraToCollectionIfMissing(
this.usuarioExtrasSharedCollection,
encuesta.usuarioExtra
);
}
protected loadRelationshipsOptions(): void {
this.categoriaService
.query()
.pipe(map((res: HttpResponse<ICategoria[]>) => res.body ?? []))
.pipe(
map((categorias: ICategoria[]) =>
this.categoriaService.addCategoriaToCollectionIfMissing(categorias, this.editForm.get('categoria')!.value)
)
)
.subscribe((categorias: ICategoria[]) => (this.categoriasSharedCollection = categorias));
this.usuarioExtraService
.query()
.pipe(map((res: HttpResponse<IUsuarioExtra[]>) => res.body ?? []))
.pipe(
map((usuarioExtras: IUsuarioExtra[]) =>
this.usuarioExtraService.addUsuarioExtraToCollectionIfMissing(usuarioExtras, this.editForm.get('usuarioExtra')!.value)
)
)
.subscribe((usuarioExtras: IUsuarioExtra[]) => (this.usuarioExtrasSharedCollection = usuarioExtras));
}
protected createFromForm(): IEncuesta {
2021-07-11 05:13:52 +00:00
const now = dayjs();
return {
...new Encuesta(),
2021-07-11 05:13:52 +00:00
id: undefined,
nombre: this.editForm.get(['nombre'])!.value,
descripcion: this.editForm.get(['descripcion'])!.value,
2021-07-11 05:13:52 +00:00
fechaCreacion: dayjs(now, DATE_TIME_FORMAT),
2021-08-12 02:45:11 +00:00
calificacion: 5.1,
acceso: this.editForm.get(['acceso'])!.value,
2021-07-11 05:13:52 +00:00
contrasenna: undefined,
estado: EstadoEncuesta.DRAFT,
categoria: this.editForm.get(['categoria'])!.value,
2021-07-11 05:13:52 +00:00
usuarioExtra: this.usuarioExtra,
};
}
2021-07-14 06:23:05 +00:00
isAdmin(): boolean {
return this.accountService.hasAnyAuthority('ROLE_ADMIN');
}
isAuthenticated(): boolean {
return this.accountService.isAuthenticated();
}
openSurvey(event: any): void {
if (event === null) {
const surveyId = this.selectedSurveyId;
this.router.navigate(['/encuesta', surveyId, 'edit']);
} else {
const surveyId = event.target.dataset.id;
this.router.navigate(['/encuesta', surveyId, 'edit']);
}
}
2021-07-14 06:23:05 +00:00
selectSurvey(event: any): void {
2021-07-26 06:00:03 +00:00
this.idEncuesta = event.target.getAttribute('data-id');
document.querySelectorAll('.ds-list--entity').forEach(e => {
e.classList.remove('active');
});
if (event.target.classList.contains('ds-list--entity')) {
event.target.classList.add('active');
}
}
openPreview() {
const surveyId = this.selectedSurveyId;
this.router.navigate(['/encuesta', surveyId, 'preview']);
}
2021-07-24 04:50:40 +00:00
async openContextMenu(event: any): Promise<void> {
if (event.type === 'contextmenu') {
event.preventDefault();
if (event.target === null) return;
document.querySelectorAll('.ds-list--entity').forEach(e => {
e.classList.remove('active');
});
if ((event.target as HTMLElement).classList.contains('ds-list')) {
document.getElementById('contextmenu-create--separator')!.style.display = 'block';
document.getElementById('contextmenu-edit--separator')!.style.display = 'none';
document.getElementById('contextmenu-delete--separator')!.style.display = 'none';
} else if ((event.target as HTMLElement).classList.contains('ds-list--entity')) {
this.selectedSurveyId = Number(event.target.dataset.id);
event.target.classList.add('active');
2021-07-23 06:56:59 +00:00
let res = await this.encuestaService.find(this.selectedSurveyId).toPromise();
this.selectedSurvey = res.body;
2021-07-30 04:15:36 +00:00
// Fill in the edit survey
this.fillSurveyEditForm();
this.isPublished = this.selectedSurvey!.estado === 'ACTIVE' || this.selectedSurvey!.estado === 'FINISHED'; // QUE SE LE MUESTRE CUANDO ESTE EN DRAFT
document.getElementById('contextmenu-create--separator')!.style.display = 'none';
document.getElementById('contextmenu-delete--separator')!.style.display = 'block';
2021-07-30 04:15:36 +00:00
document.getElementById('contextmenu-edit--separator')!.style.display = 'block';
2021-07-26 06:00:03 +00:00
document.getElementById('contextmenu-preview')!.style.display = 'block';
if (!this.isPublished) {
2021-07-30 04:15:36 +00:00
document.getElementById('contextmenu-edit--separator')!.style.display = 'block';
document.getElementById('contextmenu-edit')!.style.display = 'block';
document.getElementById('contextmenu-publish')!.style.display = 'block';
document.getElementById('contextmenu-duplicate')!.style.display = 'block';
document.getElementById('contextmenu-share')!.style.display = 'none';
} else {
2021-07-30 04:15:36 +00:00
document.getElementById('contextmenu-edit')!.style.display = 'none';
document.getElementById('contextmenu-publish')!.style.display = 'none';
document.getElementById('contextmenu-duplicate')!.style.display = 'none';
document.getElementById('contextmenu-share')!.style.display = 'block';
}
2021-08-03 21:56:05 +00:00
if (this.selectedSurvey!.estado === 'FINISHED') {
document.getElementById('contextmenu-share')!.style.display = 'none';
}
document.getElementById('contextmenu-create--separator')!.style.display = 'none';
}
document.getElementById('contextmenu')!.style.top = event.layerY + 'px';
document.getElementById('contextmenu')!.style.left = event.layerX + 'px';
document.getElementById('contextmenu')!.classList.remove('ds-contextmenu--closed');
document.getElementById('contextmenu')!.classList.add('ds-contextmenu--open');
document.getElementById('contextmenu')!.style.maxHeight = '100%';
}
2021-07-14 06:23:05 +00:00
}
2021-07-24 04:50:40 +00:00
2021-07-25 04:12:50 +00:00
publish(): void {
2021-07-24 04:50:40 +00:00
const modalRef = this.modalService.open(EncuestaPublishDialogComponent, { size: 'lg', backdrop: 'static' });
modalRef.componentInstance.encuesta = this.selectedSurvey;
// unsubscribe not needed because closed completes on modal close
modalRef.closed.subscribe(reason => {
if (reason === 'published') {
this.successPublished = true;
this.loadAll();
}
});
}
2021-07-26 20:40:21 +00:00
async duplicateSurvey(): Promise<void> {
const res = await this.encuestaService.duplicate(this.selectedSurveyId!).toPromise();
this.loadAll();
}
2021-07-30 04:15:36 +00:00
editSurvey(): void {
const survey = { ...this.selectedSurvey };
survey.nombre = this.surveyEditForm.get(['nombre'])!.value;
survey.descripcion = this.surveyEditForm.get(['descripcion'])!.value;
survey.acceso = this.surveyEditForm.get(['acceso'])!.value;
survey.categoria = this.surveyEditForm.get(['categoria'])!.value;
// Prevent user update by setting to null
survey.usuarioExtra!.user = null;
this.encuestaService.updateSurvey(survey).subscribe(res => {
this.loadAll();
$('#cancelEditSurveyBtn').click();
});
}
fillSurveyEditForm(): void {
this.surveyEditForm.patchValue({
nombre: this.selectedSurvey!.nombre,
descripcion: this.selectedSurvey!.descripcion,
acceso: this.selectedSurvey!.acceso,
categoria: this.selectedSurvey!.categoria,
});
}
compartir(): void {
const modalRef = this.modalService.open(EncuestaCompartirDialogComponent, { size: 'lg', backdrop: 'static' });
modalRef.componentInstance.encuesta = this.selectedSurvey;
// unsubscribe not needed because closed completes on modal close
modalRef.closed.subscribe(reason => {
if (reason === 'published') {
this.successPublished = true;
this.loadAll();
}
});
}
2021-07-03 21:48:27 +00:00
}