angular-akita

how to make a partial update of an akita store state?


Very basic question, google didnt help.

I have an akita store for my main session. It has this state

    export interface SessionState {
      language: Language;
      activeRepo: number;
      auth_token: string;
      user : AppUser
      userRepos : UserRepo[]
    }

How can I, in the services, update just a single of these properties? For instance,"userRepos" only

i have this service call

 getUserRepos() {
      return httpclient.post("User","GetRepos", {}).pipe(tap<UserRepo[]>(repos  => {
           // THIS IS THE LINE THAT I CANNOT GET TO WORK--->
             this.sessionStore.update({...userRepos : repos});
      }));
 }

I've tried many constructions and I just cant get it. Is it possible at all to update just a piece of the state?


Solution

  • You don't need to make a POST request to update Akita state. You can use javascript destructuring to update just that one attribute of your store. Like this:

        this._sessionStore.update(state => ({
          ...state,
          userRepos: repos,
        }));
    

    This takes the state object and assigns the same previous state value (destructured) changing the userRepos attribute with the one passed.