2021-07-24 04:50:40 +00:00
|
|
|
import { Component, OnInit } from '@angular/core';
|
|
|
|
import { IEncuesta } from '../encuesta.model';
|
|
|
|
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
|
|
|
|
import { EncuestaService } from '../service/encuesta.service';
|
|
|
|
import { EstadoEncuesta } from '../../enumerations/estado-encuesta.model';
|
2021-07-25 04:53:38 +00:00
|
|
|
import { AccesoEncuesta } from '../../enumerations/acceso-encuesta.model';
|
|
|
|
import { passwordResetFinishRoute } from '../../../account/password-reset/finish/password-reset-finish.route';
|
2021-07-26 02:20:37 +00:00
|
|
|
import { FormBuilder, Validators } from '@angular/forms';
|
|
|
|
import { IParametroAplicacion } from 'app/entities/parametro-aplicacion/parametro-aplicacion.model';
|
|
|
|
import { ParametroAplicacionService } from 'app/entities/parametro-aplicacion/service/parametro-aplicacion.service';
|
|
|
|
import { HttpResponse } from '@angular/common/http';
|
2021-07-26 04:51:53 +00:00
|
|
|
import { DATE_FORMAT, DATE_TIME_FORMAT } from '../../../config/input.constants';
|
|
|
|
import * as dayjs from 'dayjs';
|
2021-07-26 06:12:50 +00:00
|
|
|
import { finalize } from 'rxjs/operators';
|
2021-07-24 04:50:40 +00:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'jhi-encuesta-publish-dialog',
|
|
|
|
templateUrl: './encuesta-publish-dialog.component.html',
|
|
|
|
styleUrls: ['./encuesta-publish-dialog.component.scss'],
|
|
|
|
})
|
|
|
|
export class EncuestaPublishDialogComponent implements OnInit {
|
|
|
|
encuesta?: IEncuesta;
|
2021-07-26 04:51:53 +00:00
|
|
|
//fechaFinalizacion?: Date;
|
|
|
|
fechaFinalizarInvalid?: boolean = false;
|
|
|
|
fechaFinalizarInvalidMax?: boolean = false;
|
2021-07-26 02:20:37 +00:00
|
|
|
isLoading?: boolean;
|
|
|
|
parametroAplicacions?: IParametroAplicacion[];
|
|
|
|
isMin = false;
|
|
|
|
isMax = false;
|
2021-07-26 04:51:53 +00:00
|
|
|
datoMin?: number;
|
|
|
|
datoMax?: number;
|
|
|
|
now = new Date();
|
2021-07-26 02:20:37 +00:00
|
|
|
fechaForm = this.fb.group({
|
|
|
|
fechaFinalizacion: [null, [Validators.required]],
|
|
|
|
});
|
2021-07-24 04:50:40 +00:00
|
|
|
|
2021-07-26 02:20:37 +00:00
|
|
|
constructor(
|
|
|
|
protected parametroAplicacionService: ParametroAplicacionService,
|
|
|
|
protected encuestaService: EncuestaService,
|
|
|
|
protected fb: FormBuilder,
|
|
|
|
protected activeModal: NgbActiveModal
|
|
|
|
) {}
|
2021-07-24 04:50:40 +00:00
|
|
|
|
2021-07-26 04:51:53 +00:00
|
|
|
ngOnInit(): void {
|
|
|
|
this.loadAll();
|
|
|
|
}
|
|
|
|
|
2021-07-24 04:50:40 +00:00
|
|
|
cancel(): void {
|
|
|
|
this.activeModal.dismiss();
|
|
|
|
}
|
|
|
|
|
|
|
|
confirmPublish(encuesta: IEncuesta): void {
|
2021-07-26 06:12:50 +00:00
|
|
|
this.fechaFinalizarInvalid = false;
|
|
|
|
this.fechaFinalizarInvalidMax = false;
|
|
|
|
|
2021-07-26 04:51:53 +00:00
|
|
|
const now = dayjs();
|
2021-07-24 04:50:40 +00:00
|
|
|
|
2021-07-26 06:12:50 +00:00
|
|
|
/*this.loadAll()
|
|
|
|
|
|
|
|
this.parametroAplicacions?.forEach(datos => {
|
|
|
|
this.datoMin = datos.minDiasEncuesta;
|
|
|
|
this.datoMax = datos.maxDiasEncuesta;
|
|
|
|
});*/
|
2021-07-25 04:53:38 +00:00
|
|
|
|
2021-07-26 04:51:53 +00:00
|
|
|
encuesta.fechaFinalizar = dayjs(this.fechaForm.get(['fechaFinalizacion'])!.value);
|
|
|
|
encuesta.fechaPublicacion = dayjs(now, DATE_TIME_FORMAT);
|
|
|
|
|
|
|
|
if (this.fechaFinalizacionIsInvalid(encuesta.fechaFinalizar, encuesta.fechaPublicacion)) {
|
|
|
|
if (encuesta.estado === 'DRAFT') {
|
|
|
|
encuesta.estado = EstadoEncuesta.ACTIVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (encuesta.acceso === AccesoEncuesta.PRIVATE) {
|
|
|
|
encuesta.contrasenna = this.generatePassword();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.encuestaService.update(encuesta).subscribe(() => {
|
|
|
|
this.activeModal.close('published');
|
|
|
|
});
|
|
|
|
}
|
2021-07-24 04:50:40 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 02:20:37 +00:00
|
|
|
loadAll(): void {
|
|
|
|
this.isLoading = true;
|
2021-07-26 04:51:53 +00:00
|
|
|
|
2021-07-26 06:12:50 +00:00
|
|
|
this.parametroAplicacionService
|
|
|
|
.query()
|
|
|
|
.pipe(finalize(() => this.onLoadFinalize()))
|
|
|
|
.subscribe(
|
|
|
|
(res: HttpResponse<IParametroAplicacion[]>) => {
|
|
|
|
this.isLoading = false;
|
|
|
|
this.parametroAplicacions = res.body ?? [];
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
this.isLoading = false;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
onLoadFinalize() {
|
|
|
|
this.parametroAplicacions?.forEach(datos => {
|
|
|
|
this.datoMin = datos.minDiasEncuesta;
|
|
|
|
this.datoMax = datos.maxDiasEncuesta;
|
|
|
|
});
|
|
|
|
this.isLoading = false;
|
2021-07-26 02:20:37 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 04:51:53 +00:00
|
|
|
fechaFinalizacionIsInvalid(fechaFinalizar: dayjs.Dayjs, fechaPublicacion: dayjs.Dayjs): boolean {
|
|
|
|
let numberDays: number;
|
2021-07-26 01:20:07 +00:00
|
|
|
debugger;
|
2021-07-26 04:51:53 +00:00
|
|
|
|
|
|
|
numberDays = fechaFinalizar?.diff(fechaPublicacion, 'days');
|
|
|
|
|
2021-07-26 06:12:50 +00:00
|
|
|
if (numberDays <= this.datoMin!) {
|
2021-07-26 04:51:53 +00:00
|
|
|
this.fechaFinalizarInvalid = true;
|
|
|
|
return false;
|
2021-07-26 06:12:50 +00:00
|
|
|
} else if (numberDays >= this.datoMax!) {
|
2021-07-26 04:51:53 +00:00
|
|
|
this.fechaFinalizarInvalidMax = true;
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
2021-07-26 01:20:07 +00:00
|
|
|
}
|
|
|
|
|
2021-07-25 04:53:38 +00:00
|
|
|
generatePassword(): string {
|
|
|
|
const alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
|
|
|
|
|
|
|
let password = '';
|
|
|
|
for (let i = 0; i < 5; i++) {
|
|
|
|
password += alpha.charAt(Math.floor(Math.random() * alpha.length));
|
|
|
|
}
|
|
|
|
return password;
|
|
|
|
}
|
2021-07-24 04:50:40 +00:00
|
|
|
}
|