Skip to content

Commit 50a043f

Browse files
committed
(fix)REC-216: Fecha de nacieminto
1 parent 3684916 commit 50a043f

2 files changed

Lines changed: 93 additions & 57 deletions

File tree

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,61 @@
1-
import { Injectable } from "@angular/core";
1+
import { Injectable } from '@angular/core';
22
import { Observable, BehaviorSubject } from 'rxjs';
33

44

55
@Injectable({
6-
providedIn: "root",
6+
providedIn: 'root',
77
})
88
export class AmbitoService {
9-
private readonly STORAGE_KEY = 'ambito_seleccionado';
10-
private ambitoSeleccionado: "publico" | "privado" | null = null;
11-
private ambitoSeleccionado$: BehaviorSubject<"publico" | "privado" | null>;
12-
13-
constructor() {
14-
// Cargar el ámbito desde localStorage al inicializar el servicio
15-
this.loadAmbitoFromStorage();
16-
this.ambitoSeleccionado$ = new BehaviorSubject<"publico" | "privado" | null>(this.ambitoSeleccionado);
17-
}
18-
19-
private loadAmbitoFromStorage(): void {
20-
try {
21-
const storedAmbito = localStorage.getItem(this.STORAGE_KEY);
22-
if (storedAmbito && (storedAmbito === 'publico' || storedAmbito === 'privado')) {
23-
this.ambitoSeleccionado = storedAmbito as "publico" | "privado";
24-
}
25-
} catch (error) {
26-
console.warn('Error al cargar ámbito desde localStorage:', error);
9+
private readonly STORAGE_KEY = 'ambito_seleccionado';
10+
private ambitoSeleccionado: 'publico' | 'privado' | null = null;
11+
private ambitoSeleccionado$: BehaviorSubject<'publico' | 'privado' | null>;
12+
13+
constructor() {
14+
// Cargar el ámbito desde localStorage al inicializar el servicio
15+
this.loadAmbitoFromStorage();
16+
this.ambitoSeleccionado$ = new BehaviorSubject<'publico' | 'privado' | null>(this.ambitoSeleccionado);
2717
}
28-
}
29-
30-
private saveAmbitoToStorage(ambito: "publico" | "privado" | null): void {
31-
try {
32-
if (ambito) {
33-
localStorage.setItem(this.STORAGE_KEY, ambito);
34-
} else {
35-
localStorage.removeItem(this.STORAGE_KEY);
36-
}
37-
} catch (error) {
38-
console.warn('Error al guardar ámbito en localStorage:', error);
18+
19+
private loadAmbitoFromStorage(): void {
20+
try {
21+
const storedAmbito = localStorage.getItem(this.STORAGE_KEY);
22+
if (storedAmbito && (storedAmbito === 'publico' || storedAmbito === 'privado')) {
23+
this.ambitoSeleccionado = storedAmbito as 'publico' | 'privado';
24+
}
25+
} catch (error) {
26+
console.warn('Error al cargar ámbito desde localStorage:', error);
27+
}
28+
}
29+
30+
private saveAmbitoToStorage(ambito: 'publico' | 'privado' | null): void {
31+
try {
32+
if (ambito) {
33+
localStorage.setItem(this.STORAGE_KEY, ambito);
34+
} else {
35+
localStorage.removeItem(this.STORAGE_KEY);
36+
}
37+
} catch (error) {
38+
console.warn('Error al guardar ámbito en localStorage:', error);
39+
}
40+
}
41+
42+
get getAmbitoSeleccionado(): Observable<'publico' | 'privado' | null> {
43+
return this.ambitoSeleccionado$.asObservable();
44+
}
45+
46+
setAmbito(ambito: 'publico' | 'privado'): void {
47+
this.ambitoSeleccionado = ambito;
48+
this.saveAmbitoToStorage(ambito);
49+
this.ambitoSeleccionado$.next(ambito);
50+
}
51+
52+
getAmbito(): 'publico' | 'privado' | null {
53+
return this.ambitoSeleccionado;
54+
}
55+
56+
clearAmbito(): void {
57+
this.ambitoSeleccionado = null;
58+
this.saveAmbitoToStorage(null);
59+
this.ambitoSeleccionado$.next(null);
3960
}
40-
}
41-
42-
get getAmbitoSeleccionado(): Observable<"publico" | "privado" | null> {
43-
return this.ambitoSeleccionado$.asObservable();
44-
}
45-
46-
setAmbito(ambito: "publico" | "privado"): void {
47-
this.ambitoSeleccionado = ambito;
48-
this.saveAmbitoToStorage(ambito);
49-
this.ambitoSeleccionado$.next(ambito);
50-
}
51-
52-
getAmbito(): "publico" | "privado" | null {
53-
return this.ambitoSeleccionado;
54-
}
55-
56-
clearAmbito(): void {
57-
this.ambitoSeleccionado = null;
58-
this.saveAmbitoToStorage(null);
59-
this.ambitoSeleccionado$.next(null);
60-
}
61-
}
61+
}

src/app/shared/components/patient-form/patient-form.component.ts

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component, Input, Output, EventEmitter, OnInit, ViewChild, forwardRef, OnDestroy } from '@angular/core';
2-
import { FormBuilder, FormGroup, AbstractControl, Validators, ControlValueAccessor, NG_VALUE_ACCESSOR, ValidatorFn } from '@angular/forms';
2+
import { FormBuilder, FormGroup, AbstractControl, Validators, ControlValueAccessor, NG_VALUE_ACCESSOR, ValidatorFn, NG_VALIDATORS, Validator, ValidationErrors } from '@angular/forms';
33
import { PatientsService } from '@services/patients.service';
44
import { Patient } from '@interfaces/patients';
55
import { ThemePalette } from '@angular/material/core';
@@ -47,10 +47,15 @@ function validDateValidator(): ValidatorFn {
4747
provide: NG_VALUE_ACCESSOR,
4848
useExisting: forwardRef(() => PatientFormComponent),
4949
multi: true
50+
},
51+
{
52+
provide: NG_VALIDATORS,
53+
useExisting: forwardRef(() => PatientFormComponent),
54+
multi: true
5055
}
5156
]
5257
})
53-
export class PatientFormComponent implements OnInit, OnDestroy, ControlValueAccessor {
58+
export class PatientFormComponent implements OnInit, OnDestroy, ControlValueAccessor, Validator {
5459
@Input() showObraSocial = false;
5560
@Input() appearance = 'fill';
5661
@Input() layout = 'row'; // 'row' or 'column'
@@ -79,6 +84,7 @@ export class PatientFormComponent implements OnInit, OnDestroy, ControlValueAcce
7984

8085
private onChange = (value: any) => { };
8186
private onTouched = () => { };
87+
private onValidatorChange = () => { };
8288
private subscriptions: Subscription = new Subscription();
8389
private osRequestSubscription: Subscription | null = null;
8490
private lastOsRequestKey = '';
@@ -135,6 +141,12 @@ export class PatientFormComponent implements OnInit, OnDestroy, ControlValueAcce
135141
}
136142
});
137143

144+
this.patientForm.statusChanges.subscribe(() => {
145+
if (this.onValidatorChange) {
146+
this.onValidatorChange();
147+
}
148+
});
149+
138150
// Suscribirse a cambios en otraOS si está habilitado
139151
if (this.showObraSocial) {
140152
this.patientForm.get('otraOS')?.valueChanges.subscribe((checked) => {
@@ -386,7 +398,6 @@ export class PatientFormComponent implements OnInit, OnDestroy, ControlValueAcce
386398
this.patientFirstName.disable({ onlySelf: true, emitEvent: false });
387399
this.patientNombreAutopercibido.disable({ onlySelf: true, emitEvent: false });
388400
this.patientSex.disable({ onlySelf: true, emitEvent: false });
389-
this.patientFechaNac.disable({ onlySelf: true, emitEvent: false });
390401
}
391402

392403
private enableFields(): void {
@@ -408,13 +419,18 @@ export class PatientFormComponent implements OnInit, OnDestroy, ControlValueAcce
408419
this.patientSex.setValue(patient.sex);
409420

410421
// Configurar fechaNac según el ámbito y si el paciente tiene idMPI
411-
this.showFechaNac = this.ambito === 'publico' && !patient.idMPI;
422+
this.showFechaNac = this.ambito === 'publico';
412423
this.updateFechaNacValidators();
413424
this.patientFechaNac.setValue(patient.fechaNac);
414425

415426
// Deshabilitar campos después de autocompletar
416427
this.disableFields();
417428

429+
// Desabilitar fecha de nacimiento para pacientes que ya tienen fecha registrada, independientemente del ámbito
430+
if (patient.fechaNac) {
431+
this.patientFechaNac.disable({ onlySelf: true, emitEvent: false });
432+
}
433+
418434
// Cargar la obra social cuando se selecciona un paciente
419435
if (this.showObraSocial && patient.dni) {
420436
// Habilitar el campo otraOS para permitir cambiar la obra social
@@ -509,6 +525,10 @@ export class PatientFormComponent implements OnInit, OnDestroy, ControlValueAcce
509525
} else {
510526
this.patientForm.reset();
511527
}
528+
529+
if (this.onValidatorChange) {
530+
this.onValidatorChange();
531+
}
512532
}
513533

514534
registerOnChange(fn: any): void {
@@ -519,12 +539,24 @@ export class PatientFormComponent implements OnInit, OnDestroy, ControlValueAcce
519539
this.onTouched = fn;
520540
}
521541

542+
validate(_: AbstractControl): ValidationErrors | null {
543+
return this.patientForm && this.patientForm.valid ? null : { patientFormInvalid: true };
544+
}
545+
546+
registerOnValidatorChange(fn: () => void): void {
547+
this.onValidatorChange = fn;
548+
}
549+
522550
setDisabledState(isDisabled: boolean): void {
523551
if (isDisabled) {
524552
this.patientForm.disable();
525553
} else {
526554
this.patientForm.enable();
527555
}
556+
557+
if (this.onValidatorChange) {
558+
this.onValidatorChange();
559+
}
528560
}
529561

530562
// Getters para acceso a controles
@@ -603,6 +635,10 @@ export class PatientFormComponent implements OnInit, OnDestroy, ControlValueAcce
603635
fechaNacControl.setValidators([validDateValidator()]);
604636
}
605637
fechaNacControl.updateValueAndValidity();
638+
639+
if (this.onValidatorChange) {
640+
this.onValidatorChange();
641+
}
606642
}
607643

608644
markAllFieldsTouched(): void {

0 commit comments

Comments
 (0)