-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathprofile.effects.ts
More file actions
41 lines (39 loc) · 1.27 KB
/
profile.effects.ts
File metadata and controls
41 lines (39 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { catchError, combineLatest, map, of, switchMap } from 'rxjs';
import { UserService } from 'src/app/user/services/user.service';
import {
fetchProfile,
fetchProfileFailure,
fetchProfileSuccess,
} from './profile.actions';
import { profileApiToUserStateMapping } from './profile.mappings';
@Injectable()
export class ProfileEffects {
loadProfile$ = createEffect(() => {
return this.actions$.pipe(
ofType(fetchProfile),
switchMap(({ username }) =>
combineLatest([
this.userService.getUserInfo(username),
this.userService.getUserOrganizations(username),
this.userService.getUserRepos(username),
]).pipe(
map(([userData, orgsData, reposData]) => {
const data = profileApiToUserStateMapping(
userData,
orgsData,
reposData,
);
return fetchProfileSuccess({ data });
}),
catchError((error) => {
console.error(error);
return of(fetchProfileFailure({ error }));
}),
),
),
);
});
constructor(private actions$: Actions, private userService: UserService) {}
}