Skip to content

Commit

Permalink
chore(Example): rename Actions to be consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver committed Aug 28, 2017
1 parent ebf9cf4 commit 63b7b3d
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 90 deletions.
14 changes: 5 additions & 9 deletions example-app/app/books/actions/book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ export const SELECT = '[Book] Select';
*
* See Discriminated Unions: https://www.typescriptlang.org/docs/handbook/advanced-types.html#discriminated-unions
*/
export class SearchAction implements Action {
export class Search implements Action {
readonly type = SEARCH;

constructor(public payload: string) {}
}

export class SearchCompleteAction implements Action {
export class SearchComplete implements Action {
readonly type = SEARCH_COMPLETE;

constructor(public payload: Book[]) {}
}

export class LoadAction implements Action {
export class Load implements Action {
readonly type = LOAD;

constructor(public payload: Book) {}
}

export class SelectAction implements Action {
export class Select implements Action {
readonly type = SELECT;

constructor(public payload: string) {}
Expand All @@ -41,8 +41,4 @@ export class SelectAction implements Action {
* Export a type alias of all actions in this action group
* so that reducers can easily compose action types
*/
export type Actions =
| SearchAction
| SearchCompleteAction
| LoadAction
| SelectAction;
export type Actions = Search | SearchComplete | Load | Select;
36 changes: 18 additions & 18 deletions example-app/app/books/actions/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ export const LOAD_FAIL = '[Collection] Load Fail';
/**
* Add Book to Collection Actions
*/
export class AddBookAction implements Action {
export class AddBook implements Action {
readonly type = ADD_BOOK;

constructor(public payload: Book) {}
}

export class AddBookSuccessAction implements Action {
export class AddBookSuccess implements Action {
readonly type = ADD_BOOK_SUCCESS;

constructor(public payload: Book) {}
}

export class AddBookFailAction implements Action {
export class AddBookFail implements Action {
readonly type = ADD_BOOK_FAIL;

constructor(public payload: Book) {}
Expand All @@ -35,19 +35,19 @@ export class AddBookFailAction implements Action {
/**
* Remove Book from Collection Actions
*/
export class RemoveBookAction implements Action {
export class RemoveBook implements Action {
readonly type = REMOVE_BOOK;

constructor(public payload: Book) {}
}

export class RemoveBookSuccessAction implements Action {
export class RemoveBookSuccess implements Action {
readonly type = REMOVE_BOOK_SUCCESS;

constructor(public payload: Book) {}
}

export class RemoveBookFailAction implements Action {
export class RemoveBookFail implements Action {
readonly type = REMOVE_BOOK_FAIL;

constructor(public payload: Book) {}
Expand All @@ -56,29 +56,29 @@ export class RemoveBookFailAction implements Action {
/**
* Load Collection Actions
*/
export class LoadAction implements Action {
export class Load implements Action {
readonly type = LOAD;
}

export class LoadSuccessAction implements Action {
export class LoadSuccess implements Action {
readonly type = LOAD_SUCCESS;

constructor(public payload: Book[]) {}
}

export class LoadFailAction implements Action {
export class LoadFail implements Action {
readonly type = LOAD_FAIL;

constructor(public payload: any) {}
}

export type Actions =
| AddBookAction
| AddBookSuccessAction
| AddBookFailAction
| RemoveBookAction
| RemoveBookSuccessAction
| RemoveBookFailAction
| LoadAction
| LoadSuccessAction
| LoadFailAction;
| AddBook
| AddBookSuccess
| AddBookFail
| RemoveBook
| RemoveBookSuccess
| RemoveBookFail
| Load
| LoadSuccess
| LoadFail;
2 changes: 1 addition & 1 deletion example-app/app/books/containers/collection-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ export class CollectionPageComponent implements OnInit {
}

ngOnInit() {
this.store.dispatch(new collection.LoadAction());
this.store.dispatch(new collection.Load());
}
}
2 changes: 1 addition & 1 deletion example-app/app/books/containers/find-book-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ export class FindBookPageComponent {
}

search(query: string) {
this.store.dispatch(new book.SearchAction(query));
this.store.dispatch(new book.Search(query));
}
}
4 changes: 2 additions & 2 deletions example-app/app/books/containers/selected-book-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ export class SelectedBookPageComponent {
}

addToCollection(book: Book) {
this.store.dispatch(new collection.AddBookAction(book));
this.store.dispatch(new collection.AddBook(book));
}

removeFromCollection(book: Book) {
this.store.dispatch(new collection.RemoveBookAction(book));
this.store.dispatch(new collection.RemoveBook(book));
}
}
2 changes: 1 addition & 1 deletion example-app/app/books/containers/view-book-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class ViewBookPageComponent implements OnDestroy {

constructor(store: Store<fromBooks.State>, route: ActivatedRoute) {
this.actionsSubscription = route.params
.map(params => new book.SelectAction(params.id))
.map(params => new book.Select(params.id))
.subscribe(store);
}

Expand Down
16 changes: 8 additions & 8 deletions example-app/app/books/effects/book.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { empty } from 'rxjs/observable/empty';
import { BookEffects, SEARCH_SCHEDULER, SEARCH_DEBOUNCE } from './book';
import { GoogleBooksService } from '../../core/services/google-books';
import { Observable } from 'rxjs/Observable';
import { SearchAction, SearchCompleteAction } from '../actions/book';
import { Search, SearchComplete } from '../actions/book';
import { Book } from '../models/book';

export class TestActions extends Actions {
Expand Down Expand Up @@ -47,12 +47,12 @@ describe('BookEffects', () => {
});

describe('search$', () => {
it('should return a new book.SearchCompleteAction, with the books, on success, after the de-bounce', () => {
it('should return a new book.SearchComplete, with the books, on success, after the de-bounce', () => {
const book1 = { id: '111', volumeInfo: {} } as Book;
const book2 = { id: '222', volumeInfo: {} } as Book;
const books = [book1, book2];
const action = new SearchAction('query');
const completion = new SearchCompleteAction(books);
const action = new Search('query');
const completion = new SearchComplete(books);

actions$.stream = hot('-a---', { a: action });
const response = cold('-a|', { a: books });
Expand All @@ -62,9 +62,9 @@ describe('BookEffects', () => {
expect(effects.search$).toBeObservable(expected);
});

it('should return a new book.SearchCompleteAction, with an empty array, if the books service throws', () => {
const action = new SearchAction('query');
const completion = new SearchCompleteAction([]);
it('should return a new book.SearchComplete, with an empty array, if the books service throws', () => {
const action = new Search('query');
const completion = new SearchComplete([]);
const error = 'Error!';

actions$.stream = hot('-a---', { a: action });
Expand All @@ -76,7 +76,7 @@ describe('BookEffects', () => {
});

it(`should not do anything if the query is an empty string`, () => {
const action = new SearchAction('');
const action = new Search('');

actions$.stream = hot('-a---', { a: action });
const expected = cold('---');
Expand Down
6 changes: 3 additions & 3 deletions example-app/app/books/effects/book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const SEARCH_SCHEDULER = new InjectionToken<Scheduler>(
export class BookEffects {
@Effect()
search$: Observable<Action> = this.actions$
.ofType<book.SearchAction>(book.SEARCH)
.ofType<book.Search>(book.SEARCH)
.debounceTime(this.debounce, this.scheduler || async)
.map(action => action.payload)
.switchMap(query => {
Expand All @@ -50,8 +50,8 @@ export class BookEffects {
return this.googleBooks
.searchBooks(query)
.takeUntil(nextSearch$)
.map((books: Book[]) => new book.SearchCompleteAction(books))
.catch(() => of(new book.SearchCompleteAction([])));
.map((books: Book[]) => new book.SearchComplete(books))
.catch(() => of(new book.SearchComplete([])));
});

constructor(
Expand Down
36 changes: 18 additions & 18 deletions example-app/app/books/effects/collection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ describe('CollectionEffects', () => {
});

describe('loadCollection$', () => {
it('should return a collection.LoadSuccessAction, with the books, on success', () => {
const action = new collection.LoadAction();
const completion = new collection.LoadSuccessAction([book1, book2]);
it('should return a collection.LoadSuccess, with the books, on success', () => {
const action = new collection.Load();
const completion = new collection.LoadSuccess([book1, book2]);

actions$.stream = hot('-a', { a: action });
const response = cold('-a-b|', { a: book1, b: book2 });
Expand All @@ -72,10 +72,10 @@ describe('CollectionEffects', () => {
expect(effects.loadCollection$).toBeObservable(expected);
});

it('should return a collection.LoadFailAction, if the query throws', () => {
const action = new collection.LoadAction();
it('should return a collection.LoadFail, if the query throws', () => {
const action = new collection.Load();
const error = 'Error!';
const completion = new collection.LoadFailAction(error);
const completion = new collection.LoadFail(error);

actions$.stream = hot('-a', { a: action });
const response = cold('-#', {}, error);
Expand All @@ -87,9 +87,9 @@ describe('CollectionEffects', () => {
});

describe('addBookToCollection$', () => {
it('should return a collection.AddBookSuccessAction, with the book, on success', () => {
const action = new collection.AddBookAction(book1);
const completion = new collection.AddBookSuccessAction(book1);
it('should return a collection.AddBookSuccess, with the book, on success', () => {
const action = new collection.AddBook(book1);
const completion = new collection.AddBookSuccess(book1);

actions$.stream = hot('-a', { a: action });
const response = cold('-b', { b: true });
Expand All @@ -100,9 +100,9 @@ describe('CollectionEffects', () => {
expect(db.insert).toHaveBeenCalledWith('books', [book1]);
});

it('should return a collection.AddBookFailAction, with the book, when the db insert throws', () => {
const action = new collection.AddBookAction(book1);
const completion = new collection.AddBookFailAction(book1);
it('should return a collection.AddBookFail, with the book, when the db insert throws', () => {
const action = new collection.AddBook(book1);
const completion = new collection.AddBookFail(book1);
const error = 'Error!';

actions$.stream = hot('-a', { a: action });
Expand All @@ -114,9 +114,9 @@ describe('CollectionEffects', () => {
});

describe('removeBookFromCollection$', () => {
it('should return a collection.RemoveBookSuccessAction, with the book, on success', () => {
const action = new collection.RemoveBookAction(book1);
const completion = new collection.RemoveBookSuccessAction(book1);
it('should return a collection.RemoveBookSuccess, with the book, on success', () => {
const action = new collection.RemoveBook(book1);
const completion = new collection.RemoveBookSuccess(book1);

actions$.stream = hot('-a', { a: action });
const response = cold('-b', { b: true });
Expand All @@ -129,9 +129,9 @@ describe('CollectionEffects', () => {
]);
});

it('should return a collection.RemoveBookFailAction, with the book, when the db insert throws', () => {
const action = new collection.RemoveBookAction(book1);
const completion = new collection.RemoveBookFailAction(book1);
it('should return a collection.RemoveBookFail, with the book, when the db insert throws', () => {
const action = new collection.RemoveBook(book1);
const completion = new collection.RemoveBookFail(book1);
const error = 'Error!';

actions$.stream = hot('-a', { a: action });
Expand Down
16 changes: 8 additions & 8 deletions example-app/app/books/effects/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,30 @@ export class CollectionEffects {
this.db
.query('books')
.toArray()
.map((books: Book[]) => new collection.LoadSuccessAction(books))
.catch(error => of(new collection.LoadFailAction(error)))
.map((books: Book[]) => new collection.LoadSuccess(books))
.catch(error => of(new collection.LoadFail(error)))
);

@Effect()
addBookToCollection$: Observable<Action> = this.actions$
.ofType(collection.ADD_BOOK)
.map((action: collection.AddBookAction) => action.payload)
.map((action: collection.AddBook) => action.payload)
.mergeMap(book =>
this.db
.insert('books', [book])
.map(() => new collection.AddBookSuccessAction(book))
.catch(() => of(new collection.AddBookFailAction(book)))
.map(() => new collection.AddBookSuccess(book))
.catch(() => of(new collection.AddBookFail(book)))
);

@Effect()
removeBookFromCollection$: Observable<Action> = this.actions$
.ofType(collection.REMOVE_BOOK)
.map((action: collection.RemoveBookAction) => action.payload)
.map((action: collection.RemoveBook) => action.payload)
.mergeMap(book =>
this.db
.executeWrite('books', 'delete', [book.id])
.map(() => new collection.RemoveBookSuccessAction(book))
.catch(() => of(new collection.RemoveBookFailAction(book)))
.map(() => new collection.RemoveBookSuccess(book))
.catch(() => of(new collection.RemoveBookFail(book)))
);

constructor(private actions$: Actions, private db: Database) {}
Expand Down
4 changes: 2 additions & 2 deletions example-app/app/books/guards/book-exists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ export class BookExistsGuard implements CanActivate {
hasBookInApi(id: string): Observable<boolean> {
return this.googleBooks
.retrieveBook(id)
.map(bookEntity => new book.LoadAction(bookEntity))
.do((action: book.LoadAction) => this.store.dispatch(action))
.map(bookEntity => new book.Load(bookEntity))
.do((action: book.Load) => this.store.dispatch(action))
.map(book => !!book)
.catch(() => {
this.router.navigate(['/404']);
Expand Down
Loading

0 comments on commit 63b7b3d

Please sign in to comment.