68 lines
3.7 KiB
TypeScript
68 lines
3.7 KiB
TypeScript
import { Component, DestroyRef, inject } from '@angular/core';
|
|
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
|
import { FormControl, FormGroup, ReactiveFormsModule, ValidatorFn, Validators } from '@angular/forms';
|
|
import { MatButtonModule } from '@angular/material/button';
|
|
import { MAT_DIALOG_DATA, MatDialogModule, MatDialogRef } from '@angular/material/dialog';
|
|
import { MatFormFieldModule } from '@angular/material/form-field';
|
|
import { MatSelectModule } from '@angular/material/select';
|
|
import { FechaCompuesta } from './fecha-compuesta';
|
|
import { CONDICIONES_DEMO, PROCEDIMIENTOS_DEMO, ESTUDIOS_DEMO, RESULTADOS_DEMO, esOpcion, opciones } from './paciente-registro.catalogos';
|
|
import { NuevoRegistroTemporal, TipoRegistroTemporal } from './paciente-registro.models';
|
|
import { crearRegistroTemporal, esFechaReal } from './paciente-registro.validation';
|
|
|
|
export interface RegistroDialogData { tipo: TipoRegistroTemporal; id_seudonimo: string; }
|
|
|
|
const opcionValida = (catalogo: Readonly<Record<string, string>>): ValidatorFn =>
|
|
control => !control.value || esOpcion(catalogo, control.value) ? null : { opcion: true };
|
|
const fechaValida: ValidatorFn = control => !control.value || esFechaReal(control.value) ? null : { fecha: true };
|
|
|
|
@Component({
|
|
selector: 'app-paciente-registro-dialog',
|
|
imports: [ReactiveFormsModule, MatDialogModule, MatButtonModule, MatFormFieldModule, MatSelectModule, FechaCompuesta],
|
|
templateUrl: './paciente-registro-dialog.html',
|
|
styleUrl: './paciente-registro-dialog.scss',
|
|
})
|
|
export class PacienteRegistroDialog {
|
|
readonly data = inject<RegistroDialogData>(MAT_DIALOG_DATA);
|
|
private readonly dialogRef = inject<MatDialogRef<PacienteRegistroDialog, NuevoRegistroTemporal>>(MatDialogRef);
|
|
readonly titulo = this.data.tipo === 'condicion' ? 'Agregar condición'
|
|
: this.data.tipo === 'procedimiento' ? 'Agregar procedimiento' : 'Agregar resultado';
|
|
readonly etiquetaTipo = this.data.tipo === 'condicion' ? 'Tipo de condición'
|
|
: this.data.tipo === 'procedimiento' ? 'Tipo de procedimiento' : 'Tipo de estudio';
|
|
readonly etiquetaFecha = this.data.tipo === 'condicion' ? 'Fecha de inicio'
|
|
: this.data.tipo === 'procedimiento' ? 'Fecha del procedimiento' : 'Fecha del estudio';
|
|
private readonly catalogo = this.data.tipo === 'condicion' ? CONDICIONES_DEMO
|
|
: this.data.tipo === 'procedimiento' ? PROCEDIMIENTOS_DEMO : ESTUDIOS_DEMO;
|
|
readonly tipos = opciones(this.catalogo);
|
|
resultados: ReturnType<typeof opciones> = [];
|
|
readonly form = new FormGroup({
|
|
seleccion: new FormControl('', { nonNullable: true, validators: [Validators.required, opcionValida(this.catalogo)] }),
|
|
fecha: new FormControl('', { nonNullable: true, validators: [Validators.required, fechaValida] }),
|
|
resultado: new FormControl({ value: '', disabled: true }, { nonNullable: true }),
|
|
});
|
|
private enviado = false;
|
|
|
|
constructor() {
|
|
if (this.data.tipo === 'resultado') {
|
|
this.form.controls.seleccion.valueChanges.pipe(takeUntilDestroyed(inject(DestroyRef))).subscribe(estudio => {
|
|
const catalogo = esOpcion(ESTUDIOS_DEMO, estudio) ? RESULTADOS_DEMO[estudio] : null;
|
|
this.resultados = catalogo ? opciones(catalogo) : [];
|
|
const resultado = this.form.controls.resultado;
|
|
resultado.reset('');
|
|
resultado.setValidators([Validators.required, opcionValida(catalogo ?? {})]);
|
|
if (catalogo) resultado.enable(); else resultado.disable();
|
|
});
|
|
}
|
|
}
|
|
|
|
agregar() {
|
|
this.form.markAllAsTouched();
|
|
if (this.enviado || this.form.invalid) return;
|
|
const { seleccion, fecha, resultado } = this.form.getRawValue();
|
|
const registro = crearRegistroTemporal(this.data.tipo, seleccion, fecha, resultado);
|
|
if (!registro) return;
|
|
this.enviado = true;
|
|
this.dialogRef.close(registro);
|
|
}
|
|
}
|