3
votes

The problem here is mainly because of ofType(). I have simple action where user is dispatching a type then request is send to the backend, as response it will return an array of an object which will be passed as new dispatched action. The problem is that with new version of ngrx there is no way to specify ofType() of actions$ because first we need to use pipe() so maps are not processed in one type...

Here is my implementation:

@Effect()
    getGrades = this.actions$
    .pipe(
      ofType(StudentActions.TRY_SET_SUBJECTS),
      mergeMap((action: StudentActions.TrySetSubjects) => {
        const id = action.payload.id;
        return this.httpClient.get(`http://localhost:8080/api/v1/student/${id}/grades`)

      }),
      map((response: any[])  => {
        console.log('StudentEffects -> getGrades')
        const subjects: Subject[] = response

        return [
          new StudentActions.SetSubjects(subjects)
        ]
      })
    );

Here is how it would work with previous versions:

@Effect()
    getGrades = this.actions$
    .ofType(StudentActions.TRY_SET_SUBJECTS)
    .pipe(
      mergeMap((action: StudentActions.TrySetSubjects) => {
        const id = action.payload.id;
        return this.httpClient.get(`http://localhost:8080/api/v1/student/${id}/grades`)

      }),
      map((response: any[])  => {
        console.log('StudentEffects -> getGrades')

        const subjects: Subject[] = response

        return [
          new StudentActions.SetSubjects(subjects)
        ]
      })
    );

How can process response and dispatch new StudentActions.SetSubjects(subjects) with the new version?

Any hint would be helpful, thank you in advance.

EDIT

Actually there was problem with maps. At first I've used mergeMap and then just map. I've changed it to mergeMap and them concatMap. Not sure if concatMap is good choice but it waits until previous subscription ends so it sound like a good choice. Everything works fine.

1
your implementation is correct. ofType will still be able to detect the correct action type. what exactly is not working as expected?jahller
Everything is fine until return of new StudentActions.SetSubjects(subjects). It will throw dispatched an invalid action exception but if effect contains only one map it will be returned successfully.Mateusz Gebroski

1 Answers

2
votes

Is this what you're looking for?

enter image description here