Skip to content

Commit 9b6a02a

Browse files
committed
feat: consume user info
1 parent 15cde86 commit 9b6a02a

File tree

9 files changed

+148
-3
lines changed

9 files changed

+148
-3
lines changed

debug.log

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[1207/120956.471:ERROR:directory_reader_win.cc(43)] FindFirstFile: El sistema no puede encontrar la ruta especificada. (0x3)
2+
[1209/102017.100:ERROR:directory_reader_win.cc(43)] FindFirstFile: El sistema no puede encontrar la ruta especificada. (0x3)
3+
[1209/114251.202:ERROR:directory_reader_win.cc(43)] FindFirstFile: El sistema no puede encontrar la ruta especificada. (0x3)
4+
[1211/010054.703:ERROR:directory_reader_win.cc(43)] FindFirstFile: El sistema no puede encontrar la ruta especificada. (0x3)
5+
[1211/165324.600:ERROR:directory_reader_win.cc(43)] FindFirstFile: El sistema no puede encontrar la ruta especificada. (0x3)
6+
[1214/163635.420:ERROR:directory_reader_win.cc(43)] FindFirstFile: El sistema no puede encontrar la ruta especificada. (0x3)
7+
[1218/134332.967:ERROR:directory_reader_win.cc(43)] FindFirstFile: El sistema no puede encontrar la ruta especificada. (0x3)
8+
[1218/155414.096:ERROR:directory_reader_win.cc(43)] FindFirstFile: El sistema no puede encontrar la ruta especificada. (0x3)

src/app/modules/login/services/azure.ad.b2c.service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,8 @@ export class AzureAdB2CService {
8383
getUserGroup(): string {
8484
return this.msal.getAccount().idToken?.extension_role;
8585
}
86+
87+
getUserId(): string{
88+
return this.msal.getAccount().accountIdentifier;
89+
}
8690
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Action } from '@ngrx/store';
2+
3+
export enum UserActionTypes {
4+
LOAD_USER = '[User] LOAD_USER',
5+
LOAD_USER_SUCCESS = '[User] LOAD_USER_SUCCESS',
6+
LOAD_USER_FAIL = '[User] LOAD_USER_FAIL',
7+
}
8+
9+
export class LoadUser implements Action {
10+
public readonly type = UserActionTypes.LOAD_USER;
11+
constructor(readonly userId) {
12+
}
13+
}
14+
15+
export class LoadUserSuccess implements Action {
16+
readonly type = UserActionTypes.LOAD_USER_SUCCESS;
17+
18+
constructor(readonly payload) {
19+
}
20+
}
21+
22+
export class LoadUserFail implements Action {
23+
public readonly type = UserActionTypes.LOAD_USER_FAIL;
24+
25+
constructor(public error: string) {
26+
}
27+
}
28+
29+
30+
export type UserActions =
31+
| LoadUser
32+
| LoadUserSuccess
33+
| LoadUserFail
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Injectable } from '@angular/core';
2+
import { Actions, Effect, ofType } from '@ngrx/effects';
3+
import { Action } from '@ngrx/store';
4+
import { Observable, of } from 'rxjs';
5+
import { catchError, map, mergeMap, switchMap } from 'rxjs/operators';
6+
import { UserService } from './user.service';
7+
import * as actions from './user.actions';
8+
9+
@Injectable()
10+
export class UserEffects {
11+
constructor(private actions$: Actions, private userService: UserService) {
12+
}
13+
14+
@Effect()
15+
loadUser$: Observable<Action> = this.actions$.pipe(
16+
ofType(actions.UserActionTypes.LOAD_USER),
17+
map((action: actions.LoadUser) => action.userId),
18+
mergeMap((userId) =>
19+
this.userService.loadUser(userId).pipe(
20+
map((response) => {
21+
return new actions.LoadUserSuccess(response);
22+
}),
23+
catchError((error) => {
24+
return of(new actions.LoadUserFail(error));
25+
})
26+
)
27+
)
28+
);
29+
30+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { UserActions, UserActionTypes } from './user.actions';
2+
3+
export const initialState = {
4+
name:'',
5+
groups:[]
6+
};
7+
8+
export const userReducer = (state: any = initialState, action: UserActions): any => {
9+
switch (action.type) {
10+
case UserActionTypes.LOAD_USER:
11+
return state;
12+
case UserActionTypes.LOAD_USER_SUCCESS:
13+
return {
14+
...state,
15+
name: action.payload.name,
16+
groups: action.payload.groups
17+
};
18+
case UserActionTypes.LOAD_USER_FAIL:
19+
return state;
20+
default: {
21+
return state;
22+
}
23+
}
24+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { createFeatureSelector, createSelector } from '@ngrx/store';
2+
3+
const getUserState = createFeatureSelector('user');
4+
5+
export const getUserInfo = createSelector(getUserState, (state: any) => state);
6+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Injectable } from '@angular/core';
2+
import { HttpClient } from '@angular/common/http';
3+
4+
import { Observable } from 'rxjs';
5+
import { environment } from 'src/environments/environment';
6+
7+
@Injectable({
8+
providedIn: 'root',
9+
})
10+
export class UserService {
11+
12+
constructor(private http: HttpClient) {
13+
}
14+
15+
baseUrl = `${environment.timeTrackerApiUrl}/users`;
16+
17+
loadUser(userId): Observable<any> {
18+
return this.http.get(`${this.baseUrl}/${userId}`);
19+
}
20+
21+
}

src/app/modules/shared/components/user/user.component.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,35 @@
11
import { Component, OnInit } from '@angular/core';
22
import { AzureAdB2CService } from '../../../login/services/azure.ad.b2c.service';
33

4+
import { select, Store } from '@ngrx/store';
5+
import { getUserInfo } from './store/user.selectors';
6+
import { Subscription } from 'rxjs';
7+
import { LoadUser } from './store/user.actions';
8+
49
@Component({
510
selector: 'app-user',
611
templateUrl: './user.component.html',
712
styleUrls: ['./user.component.scss'],
813
})
914
export class UserComponent implements OnInit {
1015
name: string;
16+
userSubscription: Subscription;
1117

12-
constructor(private azureAdB2CService: AzureAdB2CService) {}
18+
constructor(
19+
private azureAdB2CService: AzureAdB2CService,
20+
private store: Store) {}
1321

1422
ngOnInit(): void {
1523
if (this.azureAdB2CService.isLogin()) {
1624
this.name = this.azureAdB2CService.getName();
25+
let userId = this.azureAdB2CService.getUserId();
26+
27+
this.store.dispatch(new LoadUser(userId));
28+
this.userSubscription = this.store
29+
.pipe(select(getUserInfo))
30+
.subscribe((response) => {
31+
console.log(response)
32+
})
1733
this.azureAdB2CService.setTenantId();
1834
}
1935
}

src/app/reducers/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { customerManagementReducer } from '../modules/customer-management/store/
66
import { projectTypeReducer } from '../modules/customer-management/components/projects-type/store/project-type.reducers';
77
import { entryReducer } from '../modules/time-clock/store/entry.reducer';
88
import { environment } from '../../environments/environment';
9-
import { userReducer } from '../modules/users/store/user.reducers';
9+
import { userReducer } from '../modules/shared/components/user/store/user.reducer';
10+
import { userReducer as usersReducer } from '../modules/users/store/user.reducers';
1011
export interface State {
1112
projects;
1213
activities;
@@ -15,6 +16,7 @@ export interface State {
1516
projectType;
1617
entries;
1718
users;
19+
user
1820
}
1921

2022
export const reducers: ActionReducerMap<State> = {
@@ -24,7 +26,8 @@ export const reducers: ActionReducerMap<State> = {
2426
technologies: technologyReducer,
2527
projectType: projectTypeReducer,
2628
entries: entryReducer,
27-
users: userReducer,
29+
users: usersReducer,
30+
user: userReducer,
2831
};
2932

3033
export const metaReducers: MetaReducer<State>[] = !environment.production ? [] : [];

0 commit comments

Comments
 (0)