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
Prev Previous commit
Next Next commit
feat: TT-155 register userEfecct in AppModule
  • Loading branch information
thegreatyamori authored and Angeluz-07 committed Mar 19, 2021
commit 88ae09d343e5a38c3aa291317a3835de34763371
14 changes: 8 additions & 6 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ import { ProjectTypeListComponent } from './modules/customer-management/componen
// tslint:disable-next-line: max-line-length
import { CreateProjectTypeComponent } from './modules/customer-management/components/projects-type/components/create-project-type/create-project-type.component';
import { CustomerEffects } from './modules/customer-management/store/customer-management.effects';
import { UserEffects } from './modules/users/store/user.effects';
import { UserEffects as UsersEffects } from './modules/users/store/user.effects';
import { UserEffects } from './modules/login/store/user.effects';
import { EntryEffects } from './modules/time-clock/store/entry.effects';
import { InjectTokenInterceptor } from './modules/shared/interceptors/inject.token.interceptor';
import { SubstractDatePipe } from './modules/shared/pipes/substract-date/substract-date.pipe';
Expand All @@ -74,7 +75,7 @@ import { LoadingBarComponent } from './modules/shared/components/loading-bar/loa
import { UsersComponent } from './modules/users/pages/users.component';
import { UsersListComponent } from './modules/users/components/users-list/users-list.component';
import { UiSwitchModule } from 'ngx-ui-switch';
import {NgxMaterialTimepickerModule} from 'ngx-material-timepicker';
import { NgxMaterialTimepickerModule } from 'ngx-material-timepicker';
// tslint:disable-next-line: max-line-length
import { TechnologyReportTableComponent } from './modules/technology-report/components/technology-report-table/technology-report-table.component';
import { TechnologyReportComponent } from './modules/technology-report/pages/technology-report.component';
Expand Down Expand Up @@ -151,8 +152,8 @@ const maskConfig: Partial<IConfig> = {
}),
!environment.production
? StoreDevtoolsModule.instrument({
maxAge: 15, // Retains last 15 states
})
maxAge: 15, // Retains last 15 states
})
: [],
EffectsModule.forRoot([
ProjectEffects,
Expand All @@ -161,9 +162,10 @@ const maskConfig: Partial<IConfig> = {
TechnologyEffects,
ProjectTypeEffects,
EntryEffects,
UsersEffects,
UserEffects,
]),
ToastrModule.forRoot()
ToastrModule.forRoot(),
],
providers: [
{
Expand All @@ -176,4 +178,4 @@ const maskConfig: Partial<IConfig> = {
],
bootstrap: [AppComponent],
})
export class AppModule { }
export class AppModule {}
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,19 @@ export enum UserActionTypes {

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

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

constructor(readonly payload) {
}
constructor(readonly payload: any) {}
}

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

constructor(public error: string) {
}
constructor(public error: string) {}
}


export type UserActions =
| LoadUser
| LoadUserSuccess
| LoadUserFail
export type UserActions = LoadUser | LoadUserSuccess | LoadUserFail;
Empty file.
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
import { Injectable } from '@angular/core';
import { Actions, Effect, ofType } from '@ngrx/effects';
import { Actions, ofType, Effect } from '@ngrx/effects';
import { Action } from '@ngrx/store';
import { Observable, of } from 'rxjs';
import { catchError, map, mergeMap, switchMap } from 'rxjs/operators';
import { catchError, map, mergeMap } 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) {
}
constructor(private actions$: Actions, private userService: UserService) {}

@Effect()
loadUser$: Observable<Action> = this.actions$.pipe(
loadUserInfo$: 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));
})
map((response) => new actions.LoadUserSuccess(response)),
catchError((error) => of(new actions.LoadUserFail(error)))
)
)
);

}
Empty file.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { UserActions, UserActionTypes } from './user.actions';

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

export const userReducer = (state: any = initialState, action: UserActions): any => {
Expand All @@ -13,7 +13,7 @@ export const userReducer = (state: any = initialState, action: UserActions): any
return {
...state,
name: action.payload.name,
groups: action.payload.groups
groups: action.payload.groups,
};
case UserActionTypes.LOAD_USER_FAIL:
return state;
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createFeatureSelector, createSelector } from '@ngrx/store';

const getUserState = createFeatureSelector('user');
const getUserState = createFeatureSelector<any>('user');

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

20 changes: 8 additions & 12 deletions src/app/modules/shared/components/user/user.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ 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 { getUserInfo } from '../../../login/store/user.selectors';
import { Subscription } from 'rxjs';
import { LoadUser } from './store/user.actions';
import { LoadUser } from '../../../login/store/user.actions';

@Component({
selector: 'app-user',
Expand All @@ -15,21 +15,17 @@ export class UserComponent implements OnInit {
name: string;
userSubscription: Subscription;

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

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

this.store.dispatch(new LoadUser(userId));
this.userSubscription = this.store
.pipe(select(getUserInfo))
.subscribe((response) => {
console.log(response)
})
this.userSubscription = this.store.pipe(select(getUserInfo)).subscribe((response) => {
console.log('from Store: ', response);
});
this.azureAdB2CService.setTenantId();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/app/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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/shared/components/user/store/user.reducer';
import { userReducer } from '../modules/login/store/user.reducer';
import { userReducer as usersReducer } from '../modules/users/store/user.reducers';
export interface State {
projects;
Expand All @@ -16,7 +16,7 @@ export interface State {
projectType;
entries;
users;
user
user;
}

export const reducers: ActionReducerMap<State> = {
Expand Down