Search code examples
angularngrx-store

How to add an effect with id on ngrx Store


It's my first time with Ngrx Store and I want to use it to get some publications from my database. I can do it for a simple publication but right now I want to do it but for a special id

This is my effect

  @Effect()
  getPiinsByProfilesEffect$: Observable<Action> = this.actions$
    .pipe(
      ofType<featureActions.GetPiinsByProfile>(featureActions.ActionTypes.GET_PIINS_BY_PROFILE),
      startWith(new featureActions.GetPiinsByProfile()),
      switchMap(action => this.dataService.GetPiinsByProfile(action.id)
        .pipe(
          map(items => new featureActions.GetPiinsByProfileSuccess(items.results)),
          catchError(error =>
            observableOf(new featureActions.GetPiinsByProfileFail(error))
          )
        )
      )
    );

This is my service

  GetPiinsByProfile(id: string): Observable<ListPiinsResponse> {
    const limit = '7';
    const page = '1';

    return this.http.get<ListPiinsResponse>(`${this.baseUrl}/piins/profile/${id}`, {
      params: {
        limit: limit, page
      }
    });
  }

And this is my action

export class GetPiinsByProfile implements Action {
  readonly type = ActionTypes.GET_PIINS_BY_PROFILE;
  constructor(public id: String) { }
}

export class GetPiinsByProfileStart implements Action {
  readonly type = ActionTypes.GET_PIINS_BY_PROFILE_START;
}

export class GetPiinsByProfileFail implements Action {
  readonly type = ActionTypes.GET_PIINS_BY_PROFILE_FAIL;

  constructor(public payload: any) { }
}

export class GetPiinsByProfileSuccess implements Action {
  readonly type = ActionTypes.GET_PIINS_BY_PROFILE_SUCCESS;

  constructor(public payload: Piins[]) { }
}

Thanks to everyone if you can help me :)


Solution

  • Thank's everyone and I find the solution.

    I use the same constructor like @vishnu but the real problem was in my effect. That's my new version :

      @Effect()
      getPiinsByProfilesEffect$: Observable<ActionsPiins> = this.actions$
        .pipe(
          ofType<featureActions.GetPiinsByProfile>(featureActions.ActionTypes.GET_PIINS_BY_PROFILE),
          startWith(new featureActions.GetPiinsByProfileLoad),
          switchMap(action => {
            console.log(action);
            if (action.type === featureActions.ActionTypes.GET_PIINS_BY_PROFILE) {
              const getPiinsByProfileAction = action as featureActions.GetPiinsByProfile;
              return this.dataService.GetPiinsByProfile(getPiinsByProfileAction.id)
                .pipe(
                  tap((list) => console.log(list)),
                  map(items => new featureActions.GetPiinsByProfileSuccess(items.results)),
                  catchError(error =>
                    observableOf(new featureActions.GetPiinsByProfileFail(error))
                  )
                );
            } else {
              return observableOf(action);
            }
    
          }
          )
        );
    

    And this is my new action :

    export class GetPiinsByProfileLoad implements Action {
      readonly type = ActionTypes.GET_PIINS_BY_PROFILE_LOAD;
    }