0

I need your help on NgRx forms, I need to add to the pregk table a value on an alias [index], without it touching me other alias [other_index]

my entities

export interface CreationPersonnePhysiqueLieeForm {
  type: TypePersonne;
  nomMarital?: string;
  noms?: string[];
  alias?: IAlias[];
}


export interface IAlias {
  nom: string;
  prenoms: string[];
  dateNaissance?: string;
  paysNaissance?: Boxed<CodificationSrj>;
  villeNaissance?: string;
}

//-->Definition of the initial state of the form

export const initialPersonnePhysique = <CreationPersonnePhysiqueLieeForm>{
  type: TypePersonne.PHYSIQUE,
  noms: [''],
  nomMarital: null,
  alias: [],
};

export const initialState: FormGroupState<CreationPersonnePhysiqueLieeForm> = createFormGroupState<CreationPersonnePhysiqueLieeForm>(
  FORMULAIRE_CREATION_PERSONNE_PHYSIQUE_LIEE_ID,
  initialPersonnePhysique,
);

//-->Définition du reducer de feature

export function creationPersonnePhysiqueFormReducer(
  state: FormGroupState<CreationPersonnePhysiqueLieeForm> = initialState,
  action: CreationPersonnePhysiqueFormActions,
): FormGroupState<CreationPersonnePhysiqueLieeForm> {
  // Règles de validation du formulaire
  const validateForm = updateGroup<CreationPersonnePhysiqueLieeForm>({
    type: validate(required),
    noms: compose(validate<Array<string>>(maxLength(environment.FORM_MAX_NOMBRE_NOMS))),
    nationalites: compose(validate<Array<string>>(maxLength(environment.FORM_MAX_NOMBRE_NATIONALITES))),
    alias: updateArray(
      updateGroup<IAlias>({
        nom: validate(required),
        prenoms: compose(validate<Array<string>>(maxLength(environment.FORM_MAX_NOMBRE_PRENOMS))),
      }),
    ),
  });

//Gestions des actions

 switch (action.type) {
    case CreationPersonnePhysiqueFormActionTypes.INIT_FORM:
      const personne = action.initialValue || initialPersonnePhysique;
      return createFormGroupState<CreationPersonnePhysiqueLieeForm>(FORMULAIRE_CREATION_PERSONNE_PHYSIQUE_LIEE_ID, personne);

    case CreationPersonnePhysiqueFormActionTypes.ENREGISTRER_FORM:
      return createFormGroupState(FORMULAIRE_CREATION_PERSONNE_PHYSIQUE_LIEE_ID, action.creationPersonnePhysiqueLieeFormValue);

    case CreationPersonnePhysiqueFormActionTypes.AJOUTER_NOM:
      return updateGroup<CreationPersonnePhysiqueLieeForm>(state, {
        noms: addArrayControl<string>(''),
      });

    case CreationPersonnePhysiqueFormActionTypes.SUPPRIMER_NOM:
      return updateGroup<CreationPersonnePhysiqueLieeForm>(state, {
        noms: removeArrayControl(action.nomIndex),
      });


    case CreationPersonnePhysiqueFormActionTypes.AJOUTER_ALIAS:
      return updateGroup<CreationPersonnePhysiqueLieeForm>(state, {
        alias: addArrayControl<IAlias>({
          nom: '',
          prenoms: [''],
          dateNaissance: null,
          villeNaissance: null,
          paysNaissance: null,
        }),
      });

    case CreationPersonnePhysiqueFormActionTypes.SUPPRIMER_ALIAS:
      return updateGroup<CreationPersonnePhysiqueLieeForm>(state, {
        alias: removeArrayControl(action.aliasIndex),
      });

** I tried that but when I create a second alias (Alias2) and I add a second name ==> it updates the first alias (Alias1) with the new value I gave to Alias2. while I don't want it to touch Alias1**

 case CreationPersonnePhysiqueFormActionTypes.AJOUTER_PRENOM_ALIAS:
      console.log('aliaaaaaaaass===============>', action.aliasIndex);

      console.log('state=======9++========>', state.controls.alias.controls[action.aliasIndex].controls);

      return updateGroup<CreationPersonnePhysiqueLieeForm>(state, {
        alias: updateArray((lineItem, lineItemsState) =>
          updateGroup<IAlias>({
            prenoms: addArrayControl<string>(''),
          })(lineItemsState.controls[action.aliasIndex]),
        ),
      });


    case CreationPersonnePhysiqueFormActionTypes.SUPPRIMER_PRENOM_ALIAS:
      return updateGroup<CreationPersonnePhysiqueLieeForm>(state, {
        alias: updateArray(
          updateGroup<IAlias>({
            prenoms: removeArrayControl(action.prenomAliasIndex),
          }),
        ),
      });

    default:
      return validateForm(formGroupReducer(state, action));
  }
}

I have a lead with

const updatedArrayUncurried = updateArrayWithFilter(array, (s, idx) => s.value === '0' && idx === 0, setValue('1'));

but I can't write it correctly with my case

0 Answers0