Skip to content

Commit

Permalink
Added ability to add comics to a reading list to the state wiring [co…
Browse files Browse the repository at this point in the history
  • Loading branch information
mcpierce committed Jun 5, 2020
1 parent c6bab26 commit 223cbb7
Show file tree
Hide file tree
Showing 13 changed files with 533 additions and 27 deletions.
37 changes: 35 additions & 2 deletions comixed-frontend/src/app/library/actions/reading-list.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@

import { Action } from '@ngrx/store';
import { ReadingList } from 'app/comics/models/reading-list';
import { Comic } from 'app/comics';

export enum ReadingListActionTypes {
Create = '[READING LIST] Create a new reading list',
Edit = '[READING LIST] Edit a reading list',
CancelEdit = '[READING LIST] Cancel editing a reading list',
Save = '[READING LIST] Save a reading list',
Saved = '[READING LIST] Saved the reading list',
SaveFailed = '[READING LIST] Failed to save the reading list'
SaveFailed = '[READING LIST] Failed to save the reading list',
AddComics = '[READING LIST] Add comics to a reading list',
ComicsAdded = '[READING LIST] Comics added to a reading list',
AddComicsFailed = '[READING LIST] Failed to add comics to a reading list',
ToggleSelectDialog = '[READING LIST] Toggle showing the selection dialog'
}

export class ReadingListCreate implements Action {
Expand Down Expand Up @@ -64,10 +69,38 @@ export class ReadingListSaveFailed implements Action {
constructor() {}
}

export class ReadingListAddComics implements Action {
readonly type = ReadingListActionTypes.AddComics;

constructor(public payload: { readingList: ReadingList; comics: Comic[] }) {}
}

export class ReadingListComicsAdded implements Action {
readonly type = ReadingListActionTypes.ComicsAdded;

constructor() {}
}

export class ReadingListAddComicsFailed implements Action {
readonly type = ReadingListActionTypes.AddComicsFailed;

constructor() {}
}

export class ReadingListToggleSelectDialog implements Action {
readonly type = ReadingListActionTypes.ToggleSelectDialog;

constructor(public payload: { show: boolean }) {}
}

export type ReadingListActions =
| ReadingListCreate
| ReadingListEdit
| ReadingListCancelEdit
| ReadingListSave
| ReadingListSaved
| ReadingListSaveFailed;
| ReadingListSaveFailed
| ReadingListAddComics
| ReadingListComicsAdded
| ReadingListAddComicsFailed
| ReadingListToggleSelectDialog;
118 changes: 112 additions & 6 deletions comixed-frontend/src/app/library/adaptors/reading-list.adaptor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,29 @@ import { LoggerModule } from '@angular-ru/logger';
import { MessageService } from 'primeng/api';
import { ReadingListAdaptor } from './reading-list.adaptor';
import {
ReadingListAddComics,
ReadingListAddComicsFailed,
ReadingListCancelEdit,
ReadingListComicsAdded,
ReadingListCreate,
ReadingListEdit,
ReadingListSave,
ReadingListSaved,
ReadingListSaveFailed
ReadingListSaveFailed,
ReadingListToggleSelectDialog
} from 'app/library/actions/reading-list.actions';
import { AppState } from 'app/library';
import { NEW_READING_LIST } from 'app/library/library.constants';
import { READING_LIST_1 } from 'app/comics/models/reading-list.fixtures';
import { ReadingList } from 'app/comics/models/reading-list';
import { COMIC_1, COMIC_2, COMIC_3 } from 'app/comics/comics.fixtures';

describe('ReadingListAdaptor', () => {
const READING_LIST = READING_LIST_1;
const READING_LIST_ID = READING_LIST.id;
const READING_LIST_NAME = READING_LIST.name;
const READING_LIST_SUMMARY = READING_LIST.summary;
const COMICS = [COMIC_1, COMIC_2, COMIC_3];

let adaptor: ReadingListAdaptor;
let store: Store<AppState>;
Expand Down Expand Up @@ -127,7 +133,9 @@ describe('ReadingListAdaptor', () => {
});

it('provides updates on editing', () => {
adaptor.editingList$.subscribe(response => expect(response).toBeFalsy());
adaptor.editingList$.subscribe(response =>
expect(response).toBeFalsy()
);
});
});

Expand All @@ -147,7 +155,9 @@ describe('ReadingListAdaptor', () => {
});

it('provides updates on saving', () => {
adaptor.savingList$.subscribe(response => expect(response).toBeTruthy());
adaptor.savingList$.subscribe(response =>
expect(response).toBeTruthy()
);
});

describe('success', () => {
Expand All @@ -163,11 +173,15 @@ describe('ReadingListAdaptor', () => {
});

it('provides updates on saving', () => {
adaptor.savingList$.subscribe(response => expect(response).toBeFalsy());
adaptor.savingList$.subscribe(response =>
expect(response).toBeFalsy()
);
});

it('provides updates on editing', () => {
adaptor.editingList$.subscribe(response => expect(response).toBeFalsy());
adaptor.editingList$.subscribe(response =>
expect(response).toBeFalsy()
);
});

it('provides updates on the reading list', () => {
Expand All @@ -183,9 +197,101 @@ describe('ReadingListAdaptor', () => {
});

it('provides updates on saving', () => {
adaptor.savingList$.subscribe(response => expect(response).toBeFalsy());
adaptor.savingList$.subscribe(response =>
expect(response).toBeFalsy()
);
});
});
});
});

describe('adding comics to a readinglist', () => {
beforeEach(() => {
adaptor.addComicsToList(READING_LIST, COMICS);
});

it('fires an action', () => {
expect(store.dispatch).toHaveBeenCalledWith(
new ReadingListAddComics({ readingList: READING_LIST, comics: COMICS })
);
});

it('provides updates on adding', () => {
adaptor.addingComics$.subscribe(response =>
expect(response).toBeTruthy()
);
});

it('provides updates on the added status', () => {
adaptor.comicsAdded$.subscribe(response => expect(response).toBeFalsy());
});

describe('success', () => {
beforeEach(() => {
store.dispatch(new ReadingListComicsAdded());
});

it('provides updates on adding', () => {
adaptor.addingComics$.subscribe(response =>
expect(response).toBeFalsy()
);
});

it('provides updates on the added status', () => {
adaptor.comicsAdded$.subscribe(response =>
expect(response).toBeTruthy()
);
});
});

describe('failure', () => {
beforeEach(() => {
store.dispatch(new ReadingListAddComicsFailed());
});

it('provides updates on adding', () => {
adaptor.addingComics$.subscribe(response =>
expect(response).toBeFalsy()
);
});
});
});

describe('toggling the selection list dialog', () => {
describe('toggling it on', () => {
beforeEach(() => {
adaptor.showSelectDialog();
});

it('fires an action', () => {
expect(store.dispatch).toHaveBeenCalledWith(
new ReadingListToggleSelectDialog({ show: true })
);
});

it('provides updates on showing the selection dialog', () => {
adaptor.showSelectionDialog$.subscribe(response =>
expect(response).toBeTruthy()
);
});
});

describe('toggling it off', () => {
beforeEach(() => {
adaptor.hideSelectDialog();
});

it('fires an action', () => {
expect(store.dispatch).toHaveBeenCalledWith(
new ReadingListToggleSelectDialog({ show: false })
);
});

it('provides updates on showing the selection dialog', () => {
adaptor.showSelectionDialog$.subscribe(response =>
expect(response).toBeFalsy()
);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import { Injectable } from '@angular/core';
import { Store } from '@ngrx/store';
import { AppState } from 'app/library';
import {
ReadingListAddComics,
ReadingListCancelEdit,
ReadingListCreate,
ReadingListEdit,
ReadingListSave
ReadingListSave,
ReadingListToggleSelectDialog
} from 'app/library/actions/reading-list.actions';
import {
READING_LIST_FEATURE_KEY,
Expand All @@ -34,13 +36,17 @@ import { LoggerService } from '@angular-ru/logger';
import { BehaviorSubject, Observable } from 'rxjs';
import { filter } from 'rxjs/operators';
import { ReadingList } from 'app/comics/models/reading-list';
import { Comic } from 'app/comics';

@Injectable()
export class ReadingListAdaptor {
private _updated$ = new BehaviorSubject<Date>(null);
private _current$ = new BehaviorSubject<ReadingList>(null);
private _editingList$ = new BehaviorSubject<boolean>(false);
private _savingList$ = new BehaviorSubject<boolean>(false);
private _addingComics$ = new BehaviorSubject<boolean>(false);
private _comicsAdded$ = new BehaviorSubject<boolean>(false);
private _showSelectionDialog$ = new BehaviorSubject<boolean>(false);

constructor(private store: Store<AppState>, private logger: LoggerService) {
this.store
Expand All @@ -57,6 +63,17 @@ export class ReadingListAdaptor {
if (this._savingList$.getValue() !== state.savingList) {
this._savingList$.next(state.savingList);
}
if (this._addingComics$.getValue() !== state.addingComics) {
this._addingComics$.next(state.addingComics);
}
if (this._comicsAdded$.getValue() !== state.comicsAdded) {
this._comicsAdded$.next(state.comicsAdded);
}
if (
this._showSelectionDialog$.getValue() !== state.showSelectionDialog
) {
this._showSelectionDialog$.next(state.showSelectionDialog);
}
this._updated$.next(new Date());
});
}
Expand Down Expand Up @@ -100,4 +117,38 @@ export class ReadingListAdaptor {
get savingList$(): Observable<boolean> {
return this._savingList$.asObservable();
}

addComicsToList(readingList: ReadingList, comics: Comic[]): void {
this.logger.debug(
'firing action to add comics to a reading list: readingList:',
readingList,
' comics:',
comics
);
this.store.dispatch(
new ReadingListAddComics({ readingList: readingList, comics: comics })
);
}

get addingComics$(): Observable<boolean> {
return this._addingComics$.asObservable();
}

get comicsAdded$(): Observable<boolean> {
return this._comicsAdded$.asObservable();
}

showSelectDialog() {
this.logger.debug('firing action to show the reading list select dialog');
this.store.dispatch(new ReadingListToggleSelectDialog({ show: true }));
}

hideSelectDialog() {
this.logger.debug('firing action to hide the reading list select dialog');
this.store.dispatch(new ReadingListToggleSelectDialog({ show: false }));
}

get showSelectionDialog$(): Observable<boolean> {
return this._showSelectionDialog$.asObservable();
}
}

0 comments on commit 223cbb7

Please sign in to comment.