fix(): fix reset in cascata delle select

This commit is contained in:
Flavio Bontà 2025-11-26 10:14:57 +01:00
parent 1ff630a3be
commit 15aea3755c
3 changed files with 58 additions and 6 deletions

View File

@ -29,7 +29,7 @@ export interface StrutturePubblicheControllerFindManyStrutture$Params {
/** /**
* Regione * Regione
*/ */
regione: string; regione?: string;
/** /**
* Sigla Provincia * Sigla Provincia

View File

@ -140,9 +140,6 @@
<ng-template #dropdownicon> <ng-template #dropdownicon>
<i class="pi pi-map"></i> <i class="pi pi-map"></i>
</ng-template> </ng-template>
<ng-template #header>
<div class="font-medium p-3">Stati</div>
</ng-template>
</p-select> </p-select>
<!-- <p-autoComplete #acSt <!-- <p-autoComplete #acSt
formControlName="luogo" formControlName="luogo"
@ -235,7 +232,10 @@
</p-autoComplete> --> </p-autoComplete> -->
</div> </div>
<div <div
*ngIf="cercaStruttureForm.controls.stato.value" *ngIf="
cercaStruttureForm.controls.stato.value &&
cercaStruttureForm.controls.stato.value.codiceStato === 'ITA'
"
class="flex-1 flex flex-col justify-start items-start gap-2 w-full" class="flex-1 flex flex-col justify-start items-start gap-2 w-full"
> >
<div <div

View File

@ -224,6 +224,10 @@ export class StrutturePubblicheComponent {
const fetchStati$ = this.strutturePubblicheService.getStati().pipe( const fetchStati$ = this.strutturePubblicheService.getStati().pipe(
tap(() => this.state.set({ statiAreLoading: true })), tap(() => this.state.set({ statiAreLoading: true })),
map((stati) => ({ stati })), map((stati) => ({ stati })),
tap((stati) => {
if (stati)
this.cercaStruttureForm.get('stato')?.setValue(stati.stati[0]);
}),
catchError(() => { catchError(() => {
this.messageService.add({ this.messageService.add({
severity: 'error', severity: 'error',
@ -241,6 +245,34 @@ export class StrutturePubblicheComponent {
const fetchRegioni$ = const fetchRegioni$ =
this.cercaStruttureForm.controls.stato.valueChanges.pipe( this.cercaStruttureForm.controls.stato.valueChanges.pipe(
tap(() => this.state.set({ regioniAreLoading: true })), 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), filter((stato) => !!stato),
switchMap((stato) => switchMap((stato) =>
this.strutturePubblicheService.getRegioni(stato.codiceStato).pipe( this.strutturePubblicheService.getRegioni(stato.codiceStato).pipe(
@ -255,6 +287,7 @@ export class StrutturePubblicheComponent {
const fetchProvince$ = const fetchProvince$ =
this.cercaStruttureForm.controls.regione.valueChanges.pipe( this.cercaStruttureForm.controls.regione.valueChanges.pipe(
tap(() => this.state.set({ provinceAreLoading: true })), tap(() => this.state.set({ provinceAreLoading: true })),
tap(() => this.resetLocation('regione')),
filter((regione) => !!regione), filter((regione) => !!regione),
switchMap((regione) => switchMap((regione) =>
this.strutturePubblicheService this.strutturePubblicheService
@ -271,6 +304,7 @@ export class StrutturePubblicheComponent {
const fetchCitta$ = const fetchCitta$ =
this.cercaStruttureForm.controls.provincia.valueChanges.pipe( this.cercaStruttureForm.controls.provincia.valueChanges.pipe(
tap(() => this.state.set({ cittaAreLoading: true })), tap(() => this.state.set({ cittaAreLoading: true })),
tap(() => this.resetLocation('provincia')),
filter((provincia) => !!provincia), filter((provincia) => !!provincia),
switchMap((provincia) => switchMap((provincia) =>
this.strutturePubblicheService this.strutturePubblicheService
@ -327,6 +361,23 @@ export class StrutturePubblicheComponent {
this.state.connect(fetchCities$); 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() { getStrutture() {
this.state.set({ this.state.set({
cercaStruttureFormSubmitted: true, cercaStruttureFormSubmitted: true,
@ -342,11 +393,12 @@ export class StrutturePubblicheComponent {
indirizzo: _form.indirizzo, indirizzo: _form.indirizzo,
tipoStruttura: _form.tipologiaStruttura, tipoStruttura: _form.tipologiaStruttura,
codiceStato: _form.stato!.codiceStato, codiceStato: _form.stato!.codiceStato,
regione: _form.regione!.regione, regione: _form.regione?.regione,
siglaProvincia: _form.provincia?.siglaProvincia, siglaProvincia: _form.provincia?.siglaProvincia,
comune: _form.citta?.comune, comune: _form.citta?.comune,
}; };
this.calcolaIndirizzoDa = _form.indirizzo; this.calcolaIndirizzoDa = _form.indirizzo;
console.log('params', params);
const strutture$ = this.strutturePubblicheService.getStrutture(params).pipe( const strutture$ = this.strutturePubblicheService.getStrutture(params).pipe(
catchError((err) => { catchError((err) => {