0

I've just started to set up an NGRX/Store based sample project. My goal was to develop independent feature modules working on the same store. Later, these modules are to be outsourced to a library so that they can be integrated independently.


I have provided this really simple test project as a Stackblitz:

https://stackblitz.com/github/MarcManhart/ngrx-beispiel-projekt


Both modules load the data in their main components via an action, which in turn triggers an effect and which in turn obtains the data from a service. The classic way as far as I understand it.

However, it happens that these data overwrite each other if I first use the admin module to add data records and then navigate to the customer module.

So I have the feeling that I didn't integrate the store correctly. Can anybody help me further?

The components (each in its own module):

export class AdminHomeComponent implements OnInit {

  movies$: Observable<Movie[]> = this.store.pipe(select(selectAllMovies));

  constructor(private store: Store<fromReducer.State>) {
    this.store.dispatch(loadMovies());
  }

  //...
}
export class MoviesComponent implements OnInit {

  movies$: Observable<Movie[]> = this.store.pipe(select(selectAllMovies));

  constructor(private store: Store<fromReducer.State>) {
    this.store.dispatch(loadMovies());
  }

  //...
}

The action:

export const loadMovies = createAction('[Movies] Load movie');

The Effect:

@Injectable()
export class MovieEffects {

  loadMovies$ = createEffect(() => this.actions$.pipe(
      ofType(MovieActions.loadMovies),
      mergeMap(() => this.movieService.getAll() // service returns data from API
      .pipe(
        map(movies => {
          return MovieActions.loadMoviesSuccess({movies: movies});
        }),
        catchError(() => EMPTY)
      ))
    )
  );

  constructor(
    private actions$: Actions,
    private movieService: MovieService
  ) {
  }
}
EchtFettigerKeks
  • 1,692
  • 1
  • 18
  • 33
  • 1
    They are overwriting data since these module are using the same featureKey, 'movies-state'. You can see your application state and running actions if you install redux devtool – Sergio Rinaudo Jun 15 '22 at 14:57
  • Also, even though he has 2 feature modules, they are using the same action... the store in both modules looks the same, if so, they probably don't need to be separate modules... looks DRY to me – Andres2142 Jun 15 '22 at 15:00
  • Yes that's true, but my goal is to use the same data store. How else should I implement this? – EchtFettigerKeks Jun 15 '22 at 15:01
  • 1
    Since one module is for management and one module is for the "consumer" user ( is that right? ), maybe it is a better idea to use two distinct featureKey, ed. 'movies-management' and 'movies-customer'. These modules then can work individually or together, working on their state namespace ( the application state will be one in all cases ). – Sergio Rinaudo Jun 15 '22 at 15:07
  • As @SergioRinaudo says, don't worry about the global State, it will be declared once by the final App, and your feature Stores will simply add their properties to it. And I agree also, check the Redux DevTools, you will see how your Store gets scaffolded ! – Alain Boudard Jun 15 '22 at 15:10
  • Okay, maybe the problem here is that I don't understand how it can then be the same data. If my app-state consists of two feature-keys (since each module uses its own), redundant data storage is created, isn't it? (Appstate)-->(Management)-->(movies) then has e.g. three entries while (Appstate)-->(Customer)-->(movies) has e.g. only one or is outdated, or something like that. – EchtFettigerKeks Jun 15 '22 at 15:14
  • I forket the stackblitz with different feature-keys. It still seems to override the other when second module is loaded later. https://stackblitz.com/edit/github-pbrsbd – EchtFettigerKeks Jun 15 '22 at 15:24
  • @EchtFettigerKeks, let me ensure I understand your problem correctly - you want to have two separate lazy-loaded feature modules, but also want them to have the access to one shared data store, where both feature modules would use all the same actions and selectors? – JoannaFalkowska Jun 17 '22 at 15:07

0 Answers0