Skip to content

Commit

Permalink
refactor(example-app): use barrels for actions (#1317)
Browse files Browse the repository at this point in the history
Closes #1316
  • Loading branch information
peterbsmyth authored and brandonroberts committed Aug 31, 2018
1 parent 9706879 commit fdc6e93
Show file tree
Hide file tree
Showing 33 changed files with 198 additions and 195 deletions.
5 changes: 5 additions & 0 deletions projects/example-app/src/app/auth/actions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as AuthActions from './auth.actions';
import * as AuthApiActions from './auth-api.actions';
import * as LoginPageActions from './login-page.actions';

export { AuthActions, AuthApiActions, LoginPageActions };
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { StoreModule, Store, combineReducers } from '@ngrx/store';
import { LoginPageComponent } from '@example-app/auth/containers/login-page.component';
import { LoginFormComponent } from '@example-app/auth/components/login-form.component';
import * as fromAuth from '@example-app/auth/reducers';
import * as LoginPageActions from '@example-app/auth/actions/login-page.actions';
import { LoginPageActions } from '@example-app/auth/actions';

describe('Login Page', () => {
let fixture: ComponentFixture<LoginPageComponent>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Component, OnInit } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { Credentials } from '@example-app/auth/models/user';
import * as fromAuth from '@example-app/auth/reducers';
import * as LoginPageActions from '@example-app/auth/actions/login-page.actions';
import { LoginPageActions } from '@example-app/auth/actions';

@Component({
selector: 'bc-login-page',
Expand Down
22 changes: 10 additions & 12 deletions projects/example-app/src/app/auth/effects/auth.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import { provideMockActions } from '@ngrx/effects/testing';
import { cold, hot } from 'jasmine-marbles';
import { empty, Observable, of } from 'rxjs';
import {
LoginFailure,
LoginRedirect,
LoginSuccess,
} from '@example-app/auth/actions/auth-api.actions';
import * as loginPageActions from '@example-app/auth/actions/login-page.actions';
import * as AuthActions from '@example-app/auth/actions/auth.actions';
LoginPageActions,
AuthActions,
AuthApiActions,
} from '@example-app/auth/actions';

import { Credentials, User } from '@example-app/auth/models/user';
import { AuthService } from '@example-app/auth/services/auth.service';
Expand Down Expand Up @@ -59,8 +57,8 @@ describe('AuthEffects', () => {
it('should return an auth.LoginSuccess action, with user information if login succeeds', () => {
const credentials: Credentials = { username: 'test', password: '' };
const user = { name: 'User' } as User;
const action = new loginPageActions.Login({ credentials });
const completion = new LoginSuccess({ user });
const action = new LoginPageActions.Login({ credentials });
const completion = new AuthApiActions.LoginSuccess({ user });

actions$ = hot('-a---', { a: action });
const response = cold('-a|', { a: user });
Expand All @@ -72,8 +70,8 @@ describe('AuthEffects', () => {

it('should return a new auth.LoginFailure if the login service throws', () => {
const credentials: Credentials = { username: 'someOne', password: '' };
const action = new loginPageActions.Login({ credentials });
const completion = new LoginFailure({
const action = new LoginPageActions.Login({ credentials });
const completion = new AuthApiActions.LoginFailure({
error: 'Invalid username or password',
});
const error = 'Invalid username or password';
Expand All @@ -90,7 +88,7 @@ describe('AuthEffects', () => {
describe('loginSuccess$', () => {
it('should dispatch a RouterNavigation action', (done: any) => {
const user = { name: 'User' } as User;
const action = new LoginSuccess({ user });
const action = new AuthApiActions.LoginSuccess({ user });

actions$ = of(action);

Expand All @@ -103,7 +101,7 @@ describe('AuthEffects', () => {

describe('loginRedirect$', () => {
it('should dispatch a RouterNavigation action when auth.LoginRedirect is dispatched', (done: any) => {
const action = new LoginRedirect();
const action = new AuthApiActions.LoginRedirect();

actions$ = of(action);

Expand Down
20 changes: 8 additions & 12 deletions projects/example-app/src/app/auth/effects/auth.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ import { Router } from '@angular/router';
import { Actions, Effect, ofType } from '@ngrx/effects';
import { of } from 'rxjs';
import { catchError, exhaustMap, map, tap } from 'rxjs/operators';

import * as LoginPageActions from '@example-app/auth/actions/login-page.actions';
import * as AuthActions from '@example-app/auth/actions/auth.actions';

import {
AuthApiActionTypes,
LoginFailure,
LoginSuccess,
} from '@example-app/auth/actions/auth-api.actions';
LoginPageActions,
AuthActions,
AuthApiActions,
} from '@example-app/auth/actions';
import { Credentials } from '@example-app/auth/models/user';
import { AuthService } from '@example-app/auth/services/auth.service';
import { LogoutConfirmationDialogComponent } from '@example-app/auth/components/logout-confirmation-dialog.component';
Expand All @@ -25,22 +21,22 @@ export class AuthEffects {
map(action => action.payload.credentials),
exhaustMap((auth: Credentials) =>
this.authService.login(auth).pipe(
map(user => new LoginSuccess({ user })),
catchError(error => of(new LoginFailure({ error })))
map(user => new AuthApiActions.LoginSuccess({ user })),
catchError(error => of(new AuthApiActions.LoginFailure({ error })))
)
)
);

@Effect({ dispatch: false })
loginSuccess$ = this.actions$.pipe(
ofType(AuthApiActionTypes.LoginSuccess),
ofType(AuthApiActions.AuthApiActionTypes.LoginSuccess),
tap(() => this.router.navigate(['/']))
);

@Effect({ dispatch: false })
loginRedirect$ = this.actions$.pipe(
ofType(
AuthApiActionTypes.LoginRedirect,
AuthApiActions.AuthApiActionTypes.LoginRedirect,
AuthActions.AuthActionTypes.Logout
),
tap(authed => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { reducer } from '@example-app/auth/reducers/auth.reducer';
import * as fromAuth from '@example-app/auth/reducers/auth.reducer';
import { LoginSuccess } from '@example-app/auth/actions/auth-api.actions';
import * as AuthActions from '@example-app/auth/actions/auth.actions';
import { AuthApiActions, AuthActions } from '@example-app/auth/actions/';

import { User } from '@example-app/auth/models/user';

Expand All @@ -26,7 +25,7 @@ describe('AuthReducer', () => {
describe('LOGIN_SUCCESS', () => {
it('should add a user set loggedIn to true in auth state', () => {
const user = { name: 'test' } as User;
const createAction = new LoginSuccess({ user });
const createAction = new AuthApiActions.LoginSuccess({ user });

const expectedResult = {
user: { name: 'test' },
Expand Down
10 changes: 3 additions & 7 deletions projects/example-app/src/app/auth/reducers/auth.reducer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
AuthApiActionsUnion,
AuthApiActionTypes,
} from '@example-app/auth/actions/auth-api.actions';
import * as AuthActions from '@example-app/auth/actions/auth.actions';
import { AuthApiActions, AuthActions } from '@example-app/auth/actions';
import { User } from '@example-app/auth/models/user';

export interface State {
Expand All @@ -15,10 +11,10 @@ export const initialState: State = {

export function reducer(
state = initialState,
action: AuthApiActionsUnion | AuthActions.AuthActionsUnion
action: AuthApiActions.AuthApiActionsUnion | AuthActions.AuthActionsUnion
): State {
switch (action.type) {
case AuthApiActionTypes.LoginSuccess: {
case AuthApiActions.AuthApiActionTypes.LoginSuccess: {
return {
...state,
user: action.payload.user,
Expand Down
7 changes: 5 additions & 2 deletions projects/example-app/src/app/auth/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import * as fromRoot from '@example-app/reducers';
import * as fromAuth from '@example-app/auth/reducers/auth.reducer';
import * as fromLoginPage from '@example-app/auth/reducers/login-page.reducer';
import { AuthApiActionsUnion } from '@example-app/auth/actions/auth-api.actions';
import { AuthApiActions } from '@example-app/auth/actions';

export interface AuthState {
status: fromAuth.State;
Expand All @@ -17,7 +17,10 @@ export interface State extends fromRoot.State {
auth: AuthState;
}

export const reducers: ActionReducerMap<AuthState, AuthApiActionsUnion> = {
export const reducers: ActionReducerMap<
AuthState,
AuthApiActions.AuthApiActionsUnion
> = {
status: fromAuth.reducer,
loginPage: fromLoginPage.reducer,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { reducer } from '@example-app/auth/reducers/login-page.reducer';
import * as fromLoginPage from '@example-app/auth/reducers/login-page.reducer';

import * as authApiActions from '@example-app/auth/actions/auth-api.actions';
import { Login } from '@example-app/auth/actions/login-page.actions';
import { AuthApiActions, LoginPageActions } from '@example-app/auth/actions';

import { Credentials, User } from '@example-app/auth/models/user';

Expand All @@ -20,7 +19,7 @@ describe('LoginPageReducer', () => {
describe('LOGIN', () => {
it('should make pending to true', () => {
const user = { username: 'test' } as Credentials;
const createAction = new Login({ credentials: user });
const createAction = new LoginPageActions.Login({ credentials: user });

const expectedResult = {
error: null,
Expand All @@ -36,7 +35,7 @@ describe('LoginPageReducer', () => {
describe('LOGIN_SUCCESS', () => {
it('should have no error and no pending state', () => {
const user = { name: 'test' } as User;
const createAction = new authApiActions.LoginSuccess({ user });
const createAction = new AuthApiActions.LoginSuccess({ user });

const expectedResult = {
error: null,
Expand All @@ -52,7 +51,7 @@ describe('LoginPageReducer', () => {
describe('LOGIN_FAILURE', () => {
it('should have an error and no pending state', () => {
const error = 'login failed';
const createAction = new authApiActions.LoginFailure({ error });
const createAction = new AuthApiActions.LoginFailure({ error });

const expectedResult = {
error: error,
Expand Down
19 changes: 7 additions & 12 deletions projects/example-app/src/app/auth/reducers/login-page.reducer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
AuthApiActionTypes,
AuthApiActionsUnion,
} from '@example-app/auth/actions/auth-api.actions';
import {
LoginPageActionTypes,
LoginPageActionsUnion,
} from '@example-app/auth/actions/login-page.actions';
import { AuthApiActions, LoginPageActions } from '@example-app/auth/actions';

export interface State {
error: string | null;
Expand All @@ -19,26 +12,28 @@ export const initialState: State = {

export function reducer(
state = initialState,
action: AuthApiActionsUnion | LoginPageActionsUnion
action:
| AuthApiActions.AuthApiActionsUnion
| LoginPageActions.LoginPageActionsUnion
): State {
switch (action.type) {
case LoginPageActionTypes.Login: {
case LoginPageActions.LoginPageActionTypes.Login: {
return {
...state,
error: null,
pending: true,
};
}

case AuthApiActionTypes.LoginSuccess: {
case AuthApiActions.AuthApiActionTypes.LoginSuccess: {
return {
...state,
error: null,
pending: false,
};
}

case AuthApiActionTypes.LoginFailure: {
case AuthApiActions.AuthApiActionTypes.LoginFailure: {
return {
...state,
error: action.payload.error,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TestBed, inject } from '@angular/core/testing';
import { StoreModule, Store, combineReducers } from '@ngrx/store';
import { cold } from 'jasmine-marbles';
import { AuthGuard } from '@example-app/auth/services/auth-guard.service';
import * as AuthActions from '@example-app/auth/actions/auth-api.actions';
import { AuthApiActions } from '@example-app/auth/actions';
import * as fromRoot from '@example-app/reducers';
import * as fromAuth from '@example-app/auth/reducers';

Expand Down Expand Up @@ -33,7 +33,7 @@ describe('Auth Guard', () => {

it('should return true if the user state is logged in', () => {
const user: any = {};
const action = new AuthActions.LoginSuccess({ user });
const action = new AuthApiActions.LoginSuccess({ user });
store.dispatch(action);

const expected = cold('(a|)', { a: true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CanActivate } from '@angular/router';
import { Store, select } from '@ngrx/store';
import { Observable } from 'rxjs';
import { map, take } from 'rxjs/operators';
import * as AuthActions from '@example-app/auth/actions/auth-api.actions';
import { AuthApiActions } from '@example-app/auth/actions';
import * as fromAuth from '@example-app/auth/reducers';

@Injectable({
Expand All @@ -17,7 +17,7 @@ export class AuthGuard implements CanActivate {
select(fromAuth.getLoggedIn),
map(authed => {
if (!authed) {
this.store.dispatch(new AuthActions.LoginRedirect());
this.store.dispatch(new AuthApiActions.LoginRedirect());
return false;
}

Expand Down
17 changes: 17 additions & 0 deletions projects/example-app/src/app/books/actions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as BookActions from './book.actions';
import * as BooksApiActions from './books-api.actions';
import * as CollectionApiActions from './collection-api.actions';
import * as CollectionPageActions from './collection-page.actions';
import * as FindBookPageActions from './find-book-page.actions';
import * as SelectedBookPageActions from './selected-book-page.actions';
import * as ViewBookPageActions from './view-book-page.actions';

export {
BookActions,
BooksApiActions,
CollectionApiActions,
CollectionPageActions,
FindBookPageActions,
SelectedBookPageActions,
ViewBookPageActions,
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { RouterTestingModule } from '@angular/router/testing';
import { MatCardModule, MatInputModule } from '@angular/material';
import { BookPreviewListComponent } from '@example-app/books/components/book-preview-list.component';
import { BookPreviewComponent } from '@example-app/books/components/book-preview.component';
import * as CollectionActions from '@example-app/books/actions/collection-page.actions';
import { CollectionPageActions } from '@example-app/books/actions';
import * as fromBooks from '@example-app/books/reducers';
import { EllipsisPipe } from '@example-app/shared/pipes/ellipsis.pipe';
import { AddCommasPipe } from '@example-app/shared/pipes/add-commas.pipe';
Expand Down Expand Up @@ -52,7 +52,7 @@ describe('Collection Page', () => {
});

it('should dispatch a collection.Load on init', () => {
const action = new CollectionActions.LoadCollection();
const action = new CollectionPageActions.LoadCollection();

fixture.detectChanges();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
import { select, Store } from '@ngrx/store';
import { Observable } from 'rxjs';

import * as CollectionActions from '@example-app/books/actions/collection-page.actions';
import { CollectionPageActions } from '@example-app/books/actions';
import { Book } from '@example-app/books/models/book';
import * as fromBooks from '@example-app/books/reducers';

Expand Down Expand Up @@ -39,6 +39,6 @@ export class CollectionPageComponent implements OnInit {
}

ngOnInit() {
this.store.dispatch(new CollectionActions.LoadCollection());
this.store.dispatch(new CollectionPageActions.LoadCollection());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { EllipsisPipe } from '@example-app/shared/pipes/ellipsis.pipe';
import { BookAuthorsComponent } from '@example-app/books/components/book-authors.component';
import { AddCommasPipe } from '@example-app/shared/pipes/add-commas.pipe';
import { FindBookPageComponent } from '@example-app/books/containers/find-book-page.component';
import * as BookActions from '@example-app/books/actions/find-book-page.actions';
import { FindBookPageActions } from '@example-app/books/actions';
import * as fromBooks from '@example-app/books/reducers';

describe('Find Book Page', () => {
Expand Down Expand Up @@ -63,7 +63,7 @@ describe('Find Book Page', () => {

it('should dispatch a book.Search action on search', () => {
const $event = 'book name';
const action = new BookActions.SearchBooks($event);
const action = new FindBookPageActions.SearchBooks($event);

instance.search($event);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { select, Store } from '@ngrx/store';
import { Observable } from 'rxjs';
import { take } from 'rxjs/operators';

import * as BookActions from '@example-app/books/actions/find-book-page.actions';
import { FindBookPageActions } from '@example-app/books/actions';
import { Book } from '@example-app/books/models/book';
import * as fromBooks from '@example-app/books/reducers';

Expand Down Expand Up @@ -39,6 +39,6 @@ export class FindBookPageComponent {
}

search(query: string) {
this.store.dispatch(new BookActions.SearchBooks(query));
this.store.dispatch(new FindBookPageActions.SearchBooks(query));
}
}
Loading

0 comments on commit fdc6e93

Please sign in to comment.