diff --git a/src/api/fn/strutture-pubbliche/strutture-pubbliche-controller-find-many-strutture.ts b/src/api/fn/strutture-pubbliche/strutture-pubbliche-controller-find-many-strutture.ts index fa3bbf7..a7de822 100644 --- a/src/api/fn/strutture-pubbliche/strutture-pubbliche-controller-find-many-strutture.ts +++ b/src/api/fn/strutture-pubbliche/strutture-pubbliche-controller-find-many-strutture.ts @@ -29,7 +29,7 @@ export interface StrutturePubblicheControllerFindManyStrutture$Params { /** * Regione */ - regione: string; + regione?: string; /** * Sigla Provincia diff --git a/src/app/modules/public/strutture-pubbliche/strutture-pubbliche.component.html b/src/app/modules/public/strutture-pubbliche/strutture-pubbliche.component.html index 5e73c84..71dc900 100644 --- a/src/app/modules/public/strutture-pubbliche/strutture-pubbliche.component.html +++ b/src/app/modules/public/strutture-pubbliche/strutture-pubbliche.component.html @@ -140,9 +140,6 @@ - -
Stati
-
this.state.set({ statiAreLoading: true })), map((stati) => ({ stati })), + tap((stati) => { + if (stati) + this.cercaStruttureForm.get('stato')?.setValue(stati.stati[0]); + }), catchError(() => { this.messageService.add({ severity: 'error', @@ -241,6 +245,34 @@ export class StrutturePubblicheComponent { const fetchRegioni$ = this.cercaStruttureForm.controls.stato.valueChanges.pipe( tap(() => this.state.set({ regioniAreLoading: true })), + tap((stato) => { + const isITA = stato?.codiceStato === 'ITA'; + + if (!isITA) { + // Reset completo + this.resetLocation('stato'); + + // Disable cascata + this.cercaStruttureForm.get('regione')?.disable(); + this.cercaStruttureForm.get('provincia')?.disable(); + this.cercaStruttureForm.get('citta')?.disable(); + + // Rimuovo il required da regione (non serve più) + this.cercaStruttureForm.get('regione')?.clearValidators(); + this.cercaStruttureForm.get('regione')?.updateValueAndValidity(); + return; + } + + // Se ITA → abilita tutto e rimetti validatori + this.cercaStruttureForm.get('regione')?.enable(); + this.cercaStruttureForm.get('provincia')?.enable(); + this.cercaStruttureForm.get('citta')?.enable(); + + this.cercaStruttureForm + .get('regione') + ?.setValidators([Validators.required]); + this.cercaStruttureForm.get('regione')?.updateValueAndValidity(); + }), filter((stato) => !!stato), switchMap((stato) => this.strutturePubblicheService.getRegioni(stato.codiceStato).pipe( @@ -255,6 +287,7 @@ export class StrutturePubblicheComponent { const fetchProvince$ = this.cercaStruttureForm.controls.regione.valueChanges.pipe( tap(() => this.state.set({ provinceAreLoading: true })), + tap(() => this.resetLocation('regione')), filter((regione) => !!regione), switchMap((regione) => this.strutturePubblicheService @@ -271,6 +304,7 @@ export class StrutturePubblicheComponent { const fetchCitta$ = this.cercaStruttureForm.controls.provincia.valueChanges.pipe( tap(() => this.state.set({ cittaAreLoading: true })), + tap(() => this.resetLocation('provincia')), filter((provincia) => !!provincia), switchMap((provincia) => this.strutturePubblicheService @@ -327,6 +361,23 @@ export class StrutturePubblicheComponent { this.state.connect(fetchCities$); } + private resetLocation(level: 'stato' | 'regione' | 'provincia') { + if (level === 'stato') { + this.cercaStruttureForm.get('regione')?.reset(); + this.cercaStruttureForm.get('provincia')?.reset(); + this.cercaStruttureForm.get('citta')?.reset(); + } + + if (level === 'regione') { + this.cercaStruttureForm.get('provincia')?.reset(); + this.cercaStruttureForm.get('citta')?.reset(); + } + + if (level === 'provincia') { + this.cercaStruttureForm.get('citta')?.reset(); + } + } + getStrutture() { this.state.set({ cercaStruttureFormSubmitted: true, @@ -342,11 +393,12 @@ export class StrutturePubblicheComponent { indirizzo: _form.indirizzo, tipoStruttura: _form.tipologiaStruttura, codiceStato: _form.stato!.codiceStato, - regione: _form.regione!.regione, + regione: _form.regione?.regione, siglaProvincia: _form.provincia?.siglaProvincia, comune: _form.citta?.comune, }; this.calcolaIndirizzoDa = _form.indirizzo; + console.log('params', params); const strutture$ = this.strutturePubblicheService.getStrutture(params).pipe( catchError((err) => {