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 c5e92927b5
3 changed files with 58 additions and 6 deletions

View File

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

View File

@ -140,9 +140,6 @@
<ng-template #dropdownicon>
<i class="pi pi-map"></i>
</ng-template>
<ng-template #header>
<div class="font-medium p-3">Stati</div>
</ng-template>
</p-select>
<!-- <p-autoComplete #acSt
formControlName="luogo"
@ -235,7 +232,10 @@
</p-autoComplete> -->
</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"
>
<div

View File

@ -224,6 +224,10 @@ export class StrutturePubblicheComponent {
const fetchStati$ = this.strutturePubblicheService.getStati().pipe(
tap(() => 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) => {