Skip to content

Commit

Permalink
convert AuthActions
Browse files Browse the repository at this point in the history
  • Loading branch information
tja4472 committed Feb 28, 2019
1 parent 67a692f commit 2991f70
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 35 deletions.
24 changes: 8 additions & 16 deletions projects/example-app/src/app/auth/actions/auth.actions.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
import { Action } from '@ngrx/store';
import { Action, ActionsUnion, createAction } from '@ngrx/store';

export enum AuthActionTypes {
Logout = '[Auth] Logout',
LogoutConfirmation = '[Auth] Logout Confirmation',
LogoutConfirmationDismiss = '[Auth] Logout Confirmation Dismiss',
}

export class Logout implements Action {
readonly type = AuthActionTypes.Logout;
}

export class LogoutConfirmation implements Action {
readonly type = AuthActionTypes.LogoutConfirmation;
}

export class LogoutConfirmationDismiss implements Action {
readonly type = AuthActionTypes.LogoutConfirmationDismiss;
}
export const AuthActions = {
logout: () => createAction(AuthActionTypes.Logout),
logoutConfirmation: () => createAction(AuthActionTypes.LogoutConfirmation),
logoutConfirmationDismiss: () =>
createAction(AuthActionTypes.LogoutConfirmationDismiss),
};

export type AuthActionsUnion =
| Logout
| LogoutConfirmation
| LogoutConfirmationDismiss;
export type AuthActions = ActionsUnion<typeof AuthActions>;
6 changes: 5 additions & 1 deletion projects/example-app/src/app/auth/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import * as AuthActions from './auth.actions';
import {
AuthActions,
AuthActionTypes,
} from '@example-app/auth/actions/auth.actions';
import {
AuthApiActions,
AuthApiActionTypes,
Expand All @@ -10,6 +13,7 @@ import {

export {
AuthActions,
AuthActionTypes,
AuthApiActions,
AuthApiActionTypes,
LoginPageActions,
Expand Down
10 changes: 5 additions & 5 deletions projects/example-app/src/app/auth/effects/auth.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe('AuthEffects', () => {
});

it('should dispatch a RouterNavigation action when auth.Logout is dispatched', (done: any) => {
const action = new AuthActions.Logout();
const action = AuthActions.logout();

actions$ = of(action);

Expand All @@ -125,8 +125,8 @@ describe('AuthEffects', () => {

describe('logoutConfirmation$', () => {
it('should dispatch a Logout action if dialog closes with true result', () => {
const action = new AuthActions.LogoutConfirmation();
const completion = new AuthActions.Logout();
const action = AuthActions.logoutConfirmation();
const completion = AuthActions.logout();

actions$ = hot('-a', { a: action });
const expected = cold('-b', { b: completion });
Expand All @@ -139,8 +139,8 @@ describe('AuthEffects', () => {
});

it('should dispatch a LogoutConfirmationDismiss action if dialog closes with falsy result', () => {
const action = new AuthActions.LogoutConfirmation();
const completion = new AuthActions.LogoutConfirmationDismiss();
const action = AuthActions.logoutConfirmation();
const completion = AuthActions.logoutConfirmationDismiss();

actions$ = hot('-a', { a: action });
const expected = cold('-b', { b: completion });
Expand Down
12 changes: 4 additions & 8 deletions projects/example-app/src/app/auth/effects/auth.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { catchError, exhaustMap, map, tap } from 'rxjs/operators';
import {
LoginPageActions,
AuthActions,
AuthActionTypes,
AuthApiActions,
AuthApiActionTypes,
LoginPageActionTypes,
Expand Down Expand Up @@ -38,18 +39,15 @@ export class AuthEffects {

@Effect({ dispatch: false })
loginRedirect$ = this.actions$.pipe(
ofType(
AuthApiActionTypes.LoginRedirect,
AuthActions.AuthActionTypes.Logout
),
ofType(AuthApiActionTypes.LoginRedirect, AuthActionTypes.Logout),
tap(authed => {
this.router.navigate(['/login']);
})
);

@Effect()
logoutConfirmation$ = this.actions$.pipe(
ofType(AuthActions.AuthActionTypes.LogoutConfirmation),
ofType(AuthActionTypes.LogoutConfirmation),
exhaustMap(() => {
const dialogRef = this.dialog.open<
LogoutConfirmationDialogComponent,
Expand All @@ -61,9 +59,7 @@ export class AuthEffects {
}),
map(
result =>
result
? new AuthActions.Logout()
: new AuthActions.LogoutConfirmationDismiss()
result ? AuthActions.logout() : AuthActions.logoutConfirmationDismiss()
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('AuthReducer', () => {
const initialState = {
user: { name: 'test' },
} as fromAuth.State;
const createAction = new AuthActions.Logout();
const createAction = AuthActions.logout();

const expectedResult = fromAuth.initialState;

Expand Down
5 changes: 3 additions & 2 deletions projects/example-app/src/app/auth/reducers/auth.reducer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
AuthActions,
AuthActionTypes,
AuthApiActions,
AuthApiActionTypes,
} from '@example-app/auth/actions';
Expand All @@ -15,7 +16,7 @@ export const initialState: State = {

export function reducer(
state = initialState,
action: AuthApiActions | AuthActions.AuthActionsUnion
action: AuthApiActions | AuthActions
): State {
switch (action.type) {
case AuthApiActionTypes.LoginSuccess: {
Expand All @@ -25,7 +26,7 @@ export function reducer(
};
}

case AuthActions.AuthActionTypes.Logout: {
case AuthActionTypes.Logout: {
return initialState;
}

Expand Down
4 changes: 2 additions & 2 deletions projects/example-app/src/app/core/containers/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ChangeDetectionStrategy, Component } from '@angular/core';
import { select, Store } from '@ngrx/store';
import { Observable } from 'rxjs';

import * as AuthActions from '@example-app/auth/actions/auth.actions';
import { AuthActions } from '@example-app/auth/actions';
import * as fromAuth from '@example-app/auth/reducers';
import * as fromRoot from '@example-app/reducers';
import { LayoutActions } from '@example-app/core/actions';
Expand Down Expand Up @@ -64,6 +64,6 @@ export class AppComponent {
logout() {
this.closeSidenav();

this.store.dispatch(new AuthActions.LogoutConfirmation());
this.store.dispatch(AuthActions.logoutConfirmation());
}
}

0 comments on commit 2991f70

Please sign in to comment.