2021-07-23 07:00:40 +00:00
|
|
|
import { EPreguntaCerrada } from './../../e-pregunta-cerrada/e-pregunta-cerrada.model';
|
|
|
|
import { EPreguntaCerradaOpcion, IEPreguntaCerradaOpcion } from './../../e-pregunta-cerrada-opcion/e-pregunta-cerrada-opcion.model';
|
2021-07-23 03:47:11 +00:00
|
|
|
import { EPreguntaAbiertaService } from './../../e-pregunta-abierta/service/e-pregunta-abierta.service';
|
2021-07-23 03:34:05 +00:00
|
|
|
import { EPreguntaCerradaOpcionService } from './../../e-pregunta-cerrada-opcion/service/e-pregunta-cerrada-opcion.service';
|
|
|
|
import { AfterViewChecked, Component, OnInit } from '@angular/core';
|
2021-07-03 21:48:27 +00:00
|
|
|
import { HttpResponse } from '@angular/common/http';
|
|
|
|
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 { IEncuesta, Encuesta } from '../encuesta.model';
|
|
|
|
import { EncuestaService } from '../service/encuesta.service';
|
|
|
|
import { ICategoria } from 'app/entities/categoria/categoria.model';
|
|
|
|
import { CategoriaService } from 'app/entities/categoria/service/categoria.service';
|
|
|
|
import { IUsuarioExtra } from 'app/entities/usuario-extra/usuario-extra.model';
|
|
|
|
import { UsuarioExtraService } from 'app/entities/usuario-extra/service/usuario-extra.service';
|
|
|
|
|
2021-07-18 20:21:45 +00:00
|
|
|
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
|
|
|
|
import { IEPreguntaCerrada } from 'app/entities/e-pregunta-cerrada/e-pregunta-cerrada.model';
|
|
|
|
import { EPreguntaCerradaService } from 'app/entities/e-pregunta-cerrada/service/e-pregunta-cerrada.service';
|
|
|
|
import { EPreguntaCerradaDeleteDialogComponent } from 'app/entities/e-pregunta-cerrada/delete/e-pregunta-cerrada-delete-dialog.component';
|
|
|
|
|
2021-07-23 07:00:40 +00:00
|
|
|
import { faTimes, faPlus } from '@fortawesome/free-solid-svg-icons';
|
2021-07-23 03:34:05 +00:00
|
|
|
|
2021-07-03 21:48:27 +00:00
|
|
|
@Component({
|
|
|
|
selector: 'jhi-encuesta-update',
|
|
|
|
templateUrl: './encuesta-update.component.html',
|
|
|
|
})
|
2021-07-23 03:34:05 +00:00
|
|
|
export class EncuestaUpdateComponent implements OnInit, AfterViewChecked {
|
|
|
|
faTimes = faTimes;
|
2021-07-23 07:00:40 +00:00
|
|
|
faPlus = faPlus;
|
2021-07-23 03:34:05 +00:00
|
|
|
|
2021-07-03 21:48:27 +00:00
|
|
|
isSaving = false;
|
|
|
|
|
|
|
|
categoriasSharedCollection: ICategoria[] = [];
|
|
|
|
usuarioExtrasSharedCollection: IUsuarioExtra[] = [];
|
|
|
|
|
2021-07-23 07:00:40 +00:00
|
|
|
// editForm = this.fb.group({
|
|
|
|
// id: [],
|
|
|
|
// nombre: [null, [Validators.required, Validators.minLength(1), Validators.maxLength(50)]],
|
|
|
|
// descripcion: [],
|
|
|
|
// fechaCreacion: [null, [Validators.required]],
|
|
|
|
// fechaPublicacion: [],
|
|
|
|
// fechaFinalizar: [],
|
|
|
|
// fechaFinalizada: [],
|
|
|
|
// calificacion: [null, [Validators.required]],
|
|
|
|
// acceso: [null, [Validators.required]],
|
|
|
|
// contrasenna: [],
|
|
|
|
// estado: [null, [Validators.required]],
|
|
|
|
// categoria: [],
|
|
|
|
// usuarioExtra: [],
|
|
|
|
// });
|
|
|
|
|
2021-07-03 21:48:27 +00:00
|
|
|
editForm = this.fb.group({
|
|
|
|
id: [],
|
2021-07-23 07:00:40 +00:00
|
|
|
nombre: [null, [Validators.required, Validators.minLength(1), Validators.maxLength(500)]],
|
|
|
|
// orden: [null, [Validators.required]],
|
|
|
|
// cantidad: [null, [Validators.required]],
|
|
|
|
// ePreguntaCerrada: [],
|
2021-07-03 21:48:27 +00:00
|
|
|
});
|
|
|
|
|
2021-07-18 20:21:45 +00:00
|
|
|
ePreguntas?: any[];
|
|
|
|
ePreguntasOpciones?: any[];
|
|
|
|
encuesta: Encuesta | null = null;
|
|
|
|
|
|
|
|
isLoading = false;
|
|
|
|
|
2021-07-23 07:00:40 +00:00
|
|
|
createAnother: Boolean = false;
|
|
|
|
selectedQuestionToCreateOption: IEPreguntaCerrada | null = null;
|
|
|
|
|
2021-07-03 21:48:27 +00:00
|
|
|
constructor(
|
|
|
|
protected encuestaService: EncuestaService,
|
|
|
|
protected categoriaService: CategoriaService,
|
|
|
|
protected usuarioExtraService: UsuarioExtraService,
|
|
|
|
protected activatedRoute: ActivatedRoute,
|
2021-07-18 20:21:45 +00:00
|
|
|
protected fb: FormBuilder,
|
2021-07-23 03:34:05 +00:00
|
|
|
protected modalService: NgbModal,
|
|
|
|
protected ePreguntaCerradaService: EPreguntaCerradaService,
|
2021-07-23 03:47:11 +00:00
|
|
|
protected ePreguntaCerradaOpcionService: EPreguntaCerradaOpcionService,
|
|
|
|
protected ePreguntaAbiertaService: EPreguntaAbiertaService
|
2021-07-03 21:48:27 +00:00
|
|
|
) {}
|
|
|
|
|
2021-07-18 20:21:45 +00:00
|
|
|
loadAll(): void {
|
|
|
|
this.isLoading = true;
|
|
|
|
|
|
|
|
this.encuestaService.findQuestions(this.encuesta?.id!).subscribe(
|
|
|
|
(res: any) => {
|
|
|
|
this.isLoading = false;
|
|
|
|
this.ePreguntas = res.body ?? [];
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
this.isLoading = false;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
this.encuestaService.findQuestionsOptions(this.encuesta?.id!).subscribe(
|
|
|
|
(res: any) => {
|
|
|
|
this.isLoading = false;
|
|
|
|
this.ePreguntasOpciones = res.body ?? [];
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
this.isLoading = false;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-03 21:48:27 +00:00
|
|
|
ngOnInit(): void {
|
|
|
|
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;
|
2021-07-18 20:21:45 +00:00
|
|
|
} else {
|
|
|
|
this.encuesta = encuesta;
|
|
|
|
this.loadAll();
|
2021-07-03 21:48:27 +00:00
|
|
|
}
|
|
|
|
|
2021-07-18 20:21:45 +00:00
|
|
|
// this.updateForm(encuesta);
|
2021-07-03 21:48:27 +00:00
|
|
|
|
2021-07-18 20:21:45 +00:00
|
|
|
// this.loadRelationshipsOptions();
|
2021-07-03 21:48:27 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-23 03:34:05 +00:00
|
|
|
ngAfterViewChecked(): void {
|
|
|
|
this.initListeners();
|
|
|
|
}
|
|
|
|
|
2021-07-18 20:21:45 +00:00
|
|
|
trackId(index: number, item: IEPreguntaCerrada): number {
|
2021-07-03 21:48:27 +00:00
|
|
|
return item.id!;
|
|
|
|
}
|
|
|
|
|
2021-07-18 20:21:45 +00:00
|
|
|
delete(ePreguntaCerrada: IEPreguntaCerrada): void {
|
|
|
|
const modalRef = this.modalService.open(EPreguntaCerradaDeleteDialogComponent, { size: 'lg', backdrop: 'static' });
|
|
|
|
modalRef.componentInstance.ePreguntaCerrada = ePreguntaCerrada;
|
|
|
|
// unsubscribe not needed because closed completes on modal close
|
|
|
|
modalRef.closed.subscribe(reason => {
|
|
|
|
if (reason === 'deleted') {
|
|
|
|
this.loadAll();
|
|
|
|
}
|
2021-07-03 21:48:27 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-23 03:34:05 +00:00
|
|
|
initListeners(): void {
|
|
|
|
const checkboxes = document.getElementsByClassName('ds-survey--checkbox');
|
|
|
|
for (let i = 0; i < checkboxes.length; i++) {
|
|
|
|
checkboxes[i].addEventListener('click', e => {
|
|
|
|
if ((e.target as HTMLInputElement).checked) {
|
|
|
|
(e.target as HTMLElement).offsetParent!.classList.add('ds-survey--closed-option--active');
|
|
|
|
} else {
|
|
|
|
(e.target as HTMLElement).offsetParent!.classList.remove('ds-survey--closed-option--active');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
previousState(): void {
|
|
|
|
window.history.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
publishSurvey(): void {}
|
|
|
|
|
|
|
|
finishSurvey(): void {}
|
|
|
|
|
2021-07-23 07:00:40 +00:00
|
|
|
addOption(event: any): void {}
|
|
|
|
|
|
|
|
resetForm(event: any): void {
|
|
|
|
this.editForm.reset();
|
|
|
|
if (event !== null) {
|
|
|
|
const id = event.target.dataset.id;
|
|
|
|
this.ePreguntaCerradaService.find(id).subscribe(e => {
|
|
|
|
this.selectedQuestionToCreateOption = e.body;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 03:34:05 +00:00
|
|
|
deleteQuestion(event: any) {
|
|
|
|
const id = event.target.dataset.id;
|
|
|
|
if (event.target.dataset.type) {
|
|
|
|
// Delete closed question
|
|
|
|
const questionElement = (event.target as HTMLElement).parentElement?.parentElement;
|
|
|
|
const optionIdsToDelete: number[] = [];
|
|
|
|
|
|
|
|
// Get options IDs
|
|
|
|
questionElement?.childNodes.forEach((e, i) => {
|
|
|
|
if (e.nodeName !== 'DIV') return;
|
|
|
|
if (i === 0) return;
|
|
|
|
if ((e as HTMLElement).dataset.id === undefined) return;
|
2021-07-24 05:58:25 +00:00
|
|
|
if (!(e as HTMLElement).classList.contains('can-delete')) return;
|
2021-07-23 03:34:05 +00:00
|
|
|
let optionId = (e as HTMLElement).dataset.id;
|
|
|
|
optionIdsToDelete.push(+optionId!);
|
|
|
|
});
|
|
|
|
|
2021-07-24 05:58:25 +00:00
|
|
|
if (optionIdsToDelete.length === 0) {
|
2021-07-23 03:34:05 +00:00
|
|
|
this.ePreguntaCerradaService.delete(id).subscribe(e => {
|
|
|
|
this.loadAll();
|
|
|
|
});
|
2021-07-24 05:58:25 +00:00
|
|
|
} else {
|
|
|
|
// Delete question options
|
|
|
|
this.ePreguntaCerradaOpcionService.deleteMany(optionIdsToDelete).subscribe(e => {
|
|
|
|
// Delete question
|
|
|
|
this.ePreguntaCerradaService.delete(id).subscribe(e => {
|
|
|
|
this.loadAll();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2021-07-23 03:34:05 +00:00
|
|
|
} else {
|
|
|
|
// Delete open question
|
2021-07-23 03:47:11 +00:00
|
|
|
this.ePreguntaAbiertaService.delete(id).subscribe(e => {
|
|
|
|
this.loadAll();
|
|
|
|
});
|
2021-07-23 03:34:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 07:20:43 +00:00
|
|
|
deleteOption(event: any): void {
|
|
|
|
const id = event.target.dataset.optionid;
|
|
|
|
this.ePreguntaCerradaOpcionService.delete(id).subscribe(e => {
|
|
|
|
this.ePreguntas = [];
|
|
|
|
this.ePreguntasOpciones = [];
|
|
|
|
this.loadAll();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-23 07:00:40 +00:00
|
|
|
save(): void {
|
|
|
|
this.isSaving = true;
|
|
|
|
const ePreguntaCerradaOpcion = this.createFromForm();
|
|
|
|
if (ePreguntaCerradaOpcion.id !== undefined) {
|
|
|
|
this.subscribeToSaveResponse(this.ePreguntaCerradaOpcionService.update(ePreguntaCerradaOpcion));
|
|
|
|
} else {
|
|
|
|
this.subscribeToSaveResponse(
|
|
|
|
this.ePreguntaCerradaOpcionService.create(ePreguntaCerradaOpcion, this.selectedQuestionToCreateOption?.id!)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trackEPreguntaCerradaById(index: number, item: IEPreguntaCerrada): number {
|
|
|
|
return item.id!;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected subscribeToSaveResponse(result: Observable<HttpResponse<IEPreguntaCerradaOpcion>>): void {
|
|
|
|
result.pipe(finalize(() => this.onSaveFinalize())).subscribe(
|
|
|
|
() => this.onSaveSuccess(),
|
|
|
|
() => this.onSaveError()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected onSaveSuccess(): void {
|
|
|
|
// this.previousState();
|
|
|
|
this.resetForm(null);
|
|
|
|
this.ePreguntas = [];
|
|
|
|
this.ePreguntasOpciones = [];
|
|
|
|
this.loadAll();
|
|
|
|
if (!this.createAnother) {
|
|
|
|
$('#cancelBtn').click();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected onSaveError(): void {
|
|
|
|
// Api for inheritance.
|
|
|
|
}
|
|
|
|
|
|
|
|
protected onSaveFinalize(): void {
|
|
|
|
this.isSaving = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected createFromForm(): IEPreguntaCerradaOpcion {
|
|
|
|
return {
|
|
|
|
// ...new EPreguntaCerradaOpcion(),
|
|
|
|
id: undefined,
|
|
|
|
nombre: this.editForm.get(['nombre'])!.value,
|
|
|
|
orden: 10,
|
|
|
|
cantidad: 0,
|
|
|
|
ePreguntaCerrada: this.selectedQuestionToCreateOption,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
createAnotherChange(event: any) {
|
|
|
|
this.createAnother = event.target.checked;
|
|
|
|
}
|
|
|
|
|
2021-07-18 20:21:45 +00:00
|
|
|
// previousState(): void {
|
|
|
|
// window.history.back();
|
|
|
|
// }
|
|
|
|
|
|
|
|
// save(): void {
|
|
|
|
// this.isSaving = true;
|
|
|
|
// const encuesta = this.createFromForm();
|
|
|
|
// if (encuesta.id !== undefined) {
|
|
|
|
// this.subscribeToSaveResponse(this.encuestaService.update(encuesta));
|
|
|
|
// } else {
|
|
|
|
// this.subscribeToSaveResponse(this.encuestaService.create(encuesta));
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// trackCategoriaById(index: number, item: ICategoria): number {
|
|
|
|
// return item.id!;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
// );
|
|
|
|
// }
|
|
|
|
|
|
|
|
// protected onSaveSuccess(): void {
|
|
|
|
// this.previousState();
|
|
|
|
// }
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
// return {
|
|
|
|
// ...new Encuesta(),
|
|
|
|
// id: this.editForm.get(['id'])!.value,
|
|
|
|
// nombre: this.editForm.get(['nombre'])!.value,
|
|
|
|
// descripcion: this.editForm.get(['descripcion'])!.value,
|
|
|
|
// fechaCreacion: this.editForm.get(['fechaCreacion'])!.value
|
|
|
|
// ? dayjs(this.editForm.get(['fechaCreacion'])!.value, DATE_TIME_FORMAT)
|
|
|
|
// : undefined,
|
|
|
|
// fechaPublicacion: this.editForm.get(['fechaPublicacion'])!.value
|
|
|
|
// ? dayjs(this.editForm.get(['fechaPublicacion'])!.value, DATE_TIME_FORMAT)
|
|
|
|
// : undefined,
|
|
|
|
// fechaFinalizar: this.editForm.get(['fechaFinalizar'])!.value
|
|
|
|
// ? dayjs(this.editForm.get(['fechaFinalizar'])!.value, DATE_TIME_FORMAT)
|
|
|
|
// : undefined,
|
|
|
|
// fechaFinalizada: this.editForm.get(['fechaFinalizada'])!.value
|
|
|
|
// ? dayjs(this.editForm.get(['fechaFinalizada'])!.value, DATE_TIME_FORMAT)
|
|
|
|
// : undefined,
|
|
|
|
// calificacion: this.editForm.get(['calificacion'])!.value,
|
|
|
|
// acceso: this.editForm.get(['acceso'])!.value,
|
|
|
|
// contrasenna: this.editForm.get(['contrasenna'])!.value,
|
|
|
|
// estado: this.editForm.get(['estado'])!.value,
|
|
|
|
// categoria: this.editForm.get(['categoria'])!.value,
|
|
|
|
// usuarioExtra: this.editForm.get(['usuarioExtra'])!.value,
|
|
|
|
// };
|
|
|
|
// }
|
2021-07-03 21:48:27 +00:00
|
|
|
}
|