Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ngrx/entity and metaReducers with localStorageSync #1464

Closed
csaszika opened this issue Dec 11, 2018 · 6 comments · Fixed by #1498 or TypescriptID/platform#177
Closed

Ngrx/entity and metaReducers with localStorageSync #1464

csaszika opened this issue Dec 11, 2018 · 6 comments · Fixed by #1498 or TypescriptID/platform#177
Labels
bug community watch Someone from the community is working this issue/PR Project: Store

Comments

@csaszika
Copy link

Hi Everyone,

I just would like to ask that it is possible to use ngrx/entity with localStorageSync?

My experience is that everything is working with normal states but with entity states no.

This is an example how I would like to register my entity state into local storage.

export function userPreferencesLocalStorageSyncReducer(reducer: ActionReducer<any>): ActionReducer<any> {
  return localStorageSync({
    keys: [USER_PREFERENCES_STORE_KEY],
    rehydrate: true,
  })(reducer);
}

@NgModule({
  imports: [StoreModule.forFeature(USER_PREFERENCES_STORE_KEY, reducer, { initialState, metaReducers })],
})
export class UserPreferencesStoreModule {}

Thanks for any advice

@timdeschryver
Copy link
Member

Are you sure this is a ngrx/entity problem, if so, what makes you think this?
A bit more info then a simple "no" could point us in the right direction (is it throwing exceptions, does the state not get saved, or hydrated) ?

I'm not using the ngrx-local-storage library myself but it seems like it has problems with feature states?
https:/btroncone/ngrx-store-localstorage/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+feature+module

@csaszika
Copy link
Author

Some more information:

  1. I get no errors.
  2. with redux devtools I see my feature state and I can update it with actions
  3. everything is working except the local storage integration

my initializing:

export interface UserPreferencesState extends EntityState<UserPreference> {}

export const adapter: EntityAdapter<UserPreference> = createEntityAdapter<UserPreference>({
  selectId: (element: UserPreference): string => element.key,
});

export const initialState: UserPreferencesState = adapter.getInitialState({});

const reducerFn = reducerWithInitialState(initialState)
  .case(storeUserPreference, (state: UserPreferencesState, userPreference: UserPreference) => adapter.addOne(userPreference, state))
  .case(removeUserPreference, (state: UserPreferencesState, key: string) => adapter.removeOne(key, state))
  .case(clearAllUserPreferences, (state: UserPreferencesState) => adapter.removeAll(state))
  .build();

// tslint:disable-next-line:only-arrow-functions
export function reducer(state: UserPreferencesState | undefined, action: Action): UserPreferencesState {
  return reducerFn(state, action);
}

export const {
  selectIds: selectUserPreferenceKeys,
  selectEntities: selectUserPreferenceEntities,
  selectAll: selectAllUserPreferences,
  selectTotal: selectUserPreferenceTotal,
}: EntitySelectors<UserPreference, UserPreferencesState> = adapter.getSelectors();

@timdeschryver
Copy link
Member

Since everything is working, I would say this is issue should be handled by the local storage library instead of NgRx? Or is it NgRx that is causing this issue?

@timdeschryver
Copy link
Member

Possibly because of #1445 (comment)
@itayod do you want to pick this one up?

@itayod
Copy link
Contributor

itayod commented Jan 2, 2019

sure, I would be happy to :)

@timdeschryver timdeschryver added the community watch Someone from the community is working this issue/PR label Jan 2, 2019
@itayod
Copy link
Contributor

itayod commented Jan 2, 2019

@timdeschryver from a brief look at the code, I think I might have found the bug...

I reproduced it made some changes and it looks like it is working fine :).

I will make some more tests and open a pr this weekend

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug community watch Someone from the community is working this issue/PR Project: Store
Projects
None yet
3 participants