Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: consume user info
  • Loading branch information
Angeluz-07 committed Mar 19, 2021
commit 5950f27230b46d5f8ff67ae6d96a31592be2c6fc
8 changes: 8 additions & 0 deletions debug.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[1207/120956.471:ERROR:directory_reader_win.cc(43)] FindFirstFile: El sistema no puede encontrar la ruta especificada. (0x3)
[1209/102017.100:ERROR:directory_reader_win.cc(43)] FindFirstFile: El sistema no puede encontrar la ruta especificada. (0x3)
[1209/114251.202:ERROR:directory_reader_win.cc(43)] FindFirstFile: El sistema no puede encontrar la ruta especificada. (0x3)
[1211/010054.703:ERROR:directory_reader_win.cc(43)] FindFirstFile: El sistema no puede encontrar la ruta especificada. (0x3)
[1211/165324.600:ERROR:directory_reader_win.cc(43)] FindFirstFile: El sistema no puede encontrar la ruta especificada. (0x3)
[1214/163635.420:ERROR:directory_reader_win.cc(43)] FindFirstFile: El sistema no puede encontrar la ruta especificada. (0x3)
[1218/134332.967:ERROR:directory_reader_win.cc(43)] FindFirstFile: El sistema no puede encontrar la ruta especificada. (0x3)
[1218/155414.096:ERROR:directory_reader_win.cc(43)] FindFirstFile: El sistema no puede encontrar la ruta especificada. (0x3)
4 changes: 4 additions & 0 deletions src/app/modules/login/services/azure.ad.b2c.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,8 @@ export class AzureAdB2CService {
getUserGroup(): string {
return this.msal.getAccount().idToken?.extension_role;
}

getUserId(): string{
return this.msal.getAccount().accountIdentifier;
}
}
33 changes: 33 additions & 0 deletions src/app/modules/shared/components/user/store/user.actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Action } from '@ngrx/store';

export enum UserActionTypes {
LOAD_USER = '[User] LOAD_USER',
LOAD_USER_SUCCESS = '[User] LOAD_USER_SUCCESS',
LOAD_USER_FAIL = '[User] LOAD_USER_FAIL',
}

export class LoadUser implements Action {
public readonly type = UserActionTypes.LOAD_USER;
constructor(readonly userId) {
}
}

export class LoadUserSuccess implements Action {
readonly type = UserActionTypes.LOAD_USER_SUCCESS;

constructor(readonly payload) {
}
}

export class LoadUserFail implements Action {
public readonly type = UserActionTypes.LOAD_USER_FAIL;

constructor(public error: string) {
}
}


export type UserActions =
| LoadUser
| LoadUserSuccess
| LoadUserFail
30 changes: 30 additions & 0 deletions src/app/modules/shared/components/user/store/user.effects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Injectable } from '@angular/core';
import { Actions, Effect, ofType } from '@ngrx/effects';
import { Action } from '@ngrx/store';
import { Observable, of } from 'rxjs';
import { catchError, map, mergeMap, switchMap } from 'rxjs/operators';
import { UserService } from './user.service';
import * as actions from './user.actions';

@Injectable()
export class UserEffects {
constructor(private actions$: Actions, private userService: UserService) {
}

@Effect()
loadUser$: Observable<Action> = this.actions$.pipe(
ofType(actions.UserActionTypes.LOAD_USER),
map((action: actions.LoadUser) => action.userId),
mergeMap((userId) =>
this.userService.loadUser(userId).pipe(
map((response) => {
return new actions.LoadUserSuccess(response);
}),
catchError((error) => {
return of(new actions.LoadUserFail(error));
})
)
)
);

}
24 changes: 24 additions & 0 deletions src/app/modules/shared/components/user/store/user.reducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { UserActions, UserActionTypes } from './user.actions';

export const initialState = {
name:'',
groups:[]
};

export const userReducer = (state: any = initialState, action: UserActions): any => {
switch (action.type) {
case UserActionTypes.LOAD_USER:
return state;
case UserActionTypes.LOAD_USER_SUCCESS:
return {
...state,
name: action.payload.name,
groups: action.payload.groups
};
case UserActionTypes.LOAD_USER_FAIL:
return state;
default: {
return state;
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createFeatureSelector, createSelector } from '@ngrx/store';

const getUserState = createFeatureSelector('user');

export const getUserInfo = createSelector(getUserState, (state: any) => state);

21 changes: 21 additions & 0 deletions src/app/modules/shared/components/user/store/user.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import { Observable } from 'rxjs';
import { environment } from 'src/environments/environment';

@Injectable({
providedIn: 'root',
})
export class UserService {

constructor(private http: HttpClient) {
}

baseUrl = `${environment.timeTrackerApiUrl}/users`;

loadUser(userId): Observable<any> {
return this.http.get(`${this.baseUrl}/${userId}`);
}

}
18 changes: 17 additions & 1 deletion src/app/modules/shared/components/user/user.component.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
import { Component, OnInit } from '@angular/core';
import { AzureAdB2CService } from '../../../login/services/azure.ad.b2c.service';

import { select, Store } from '@ngrx/store';
import { getUserInfo } from './store/user.selectors';
import { Subscription } from 'rxjs';
import { LoadUser } from './store/user.actions';

@Component({
selector: 'app-user',
templateUrl: './user.component.html',
styleUrls: ['./user.component.scss'],
})
export class UserComponent implements OnInit {
name: string;
userSubscription: Subscription;

constructor(private azureAdB2CService: AzureAdB2CService) {}
constructor(
private azureAdB2CService: AzureAdB2CService,
private store: Store) {}

ngOnInit(): void {
if (this.azureAdB2CService.isLogin()) {
this.name = this.azureAdB2CService.getName();
let userId = this.azureAdB2CService.getUserId();

this.store.dispatch(new LoadUser(userId));
this.userSubscription = this.store
.pipe(select(getUserInfo))
.subscribe((response) => {
console.log(response)
})
this.azureAdB2CService.setTenantId();
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/app/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { customerManagementReducer } from '../modules/customer-management/store/
import { projectTypeReducer } from '../modules/customer-management/components/projects-type/store/project-type.reducers';
import { entryReducer } from '../modules/time-clock/store/entry.reducer';
import { environment } from '../../environments/environment';
import { userReducer } from '../modules/users/store/user.reducers';
import { userReducer } from '../modules/shared/components/user/store/user.reducer';
import { userReducer as usersReducer } from '../modules/users/store/user.reducers';
export interface State {
projects;
activities;
Expand All @@ -15,6 +16,7 @@ export interface State {
projectType;
entries;
users;
user
}

export const reducers: ActionReducerMap<State> = {
Expand All @@ -24,7 +26,8 @@ export const reducers: ActionReducerMap<State> = {
technologies: technologyReducer,
projectType: projectTypeReducer,
entries: entryReducer,
users: userReducer,
users: usersReducer,
user: userReducer,
};

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