-1

I am trying to assing an object to another object with the same type.

So, I have this object 'Language'

export class Language {
  languageDescription: string;
  languageCode: string;
  isDefault: boolean;
}

And I declared two objects selectedLanguage and childSelectedLanguage with the Language type :

@State<LanguageSelectorStateModel>({
  name: 'languageSelector',
  defaults: {
    languages: [],
    selectedLanguage: null,
    childSelectedLanguage: null
  }
})*

After the selectedLanguage is populated( no more null),

  @Action(UpdateSelectedLanguage)
  @ImmutableContext()
  updateSelectedLanguage({ getState, patchState }: StateContext<LanguageSelectorStateModel>, { languageCode }: UpdateSelectedLanguage) {
    const state = getState();
    const selectedLanguage = state.languages.find(language => language.languageCode === languageCode);
    state.selectedLanguage = selectedLanguage;
    patchState(state);
  }

I tried to assign the same content of this value : to childSelectedLanguage.

childSelectedLanguage = selectedLanguage;

But childSlectedLanguage still null, despite that selectedLanguage is not.

I tried also

  @Action(InitializeChildrenSelectedLanguage)
  initializeChildrenSelectedLanguage(
    { getState, patchState }: StateContext<LanguageSelectorStateModel>,
    {  }: InitializeChildrenSelectedLanguage
  ) {
    const state = getState();
    Object.assign(state.childSelectedLanguage, state.selectedLanguage);
    patchState(state);
  }

But also i can't assign the object to the content.

First question: Why I cannot assign the object?

Second question: How can I resolve the problem?

Hamdy
  • 430
  • 1
  • 6
  • 19
  • 1
    You need to create a minimal but complete example of your code, as it is not necessarily clear what the problem is with the snippets you've shown. – crashmstr Nov 12 '20 at 16:35
  • I edited my post, but I don't think is relevant to the problem – Hamdy Nov 12 '20 at 16:46
  • Where are you trying to update the childSelectedLanguage and is this really the assignment you are using? childSelectedLanguage = selectedLanguage; – A Haworth Nov 12 '20 at 17:09
  • I tried to do it in stead of Object.assign(state.childSelectedLanguage, state.selectedLanguage);. yes it's real – Hamdy Nov 12 '20 at 17:19

1 Answers1

0

To create an object from an other, you can maybe use that kind of function

/**
 * Update attributes of Class or object.
 *
 * @param origin - Original Object
 * @param newValues - New Values
 */
export function updateFields<T>(origin: T, newValues: Partial<T>): T {
  if (typeof origin !== 'object' || typeof newValues !== 'object') {
    throw new Error('Arguments must be kind of Classe or Object');
  }

  const keys = (Object.keys(newValues) as unknown) as Array<keyof T>;
  const result = origin; // Reassignment of Function Parameters is prohibited so we do a copy.

  keys.forEach(function affectValues(key) {
    result[key] = newValues[key] as never;
  });
  return result;
}

In your code you can do this

state.selectedLanguage = updateFields<YourType>(state.selectedLanguage, selectedLanguage);
  • the function updateFields attent to have an object and not null value. I found a solution. I used @ImmutableContext() decorator – Hamdy Nov 13 '20 at 13:46