Skip to content

Commit

Permalink
[Issue #233] Update the reading list state wiring.
Browse files Browse the repository at this point in the history
 * Remove holding the list of reading lists.
 * Refactor the calls for creating and saving a reading list.
  • Loading branch information
mcpierce committed May 24, 2020
1 parent f5ee62a commit 4efb972
Show file tree
Hide file tree
Showing 21 changed files with 384 additions and 637 deletions.
5 changes: 0 additions & 5 deletions comixed-frontend/src/app/app.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,4 @@

export const COMIXED_API_ROOT = '/api';

export const GET_READING_LISTS_URL = `${COMIXED_API_ROOT}/lists`;
export const GET_READING_LIST_URL = `${COMIXED_API_ROOT}/lists/\${id}`;
export const CREATE_READING_LIST_URL = `${COMIXED_API_ROOT}/lists`;
export const SAVE_READING_LIST_URL = `${COMIXED_API_ROOT}/lists/\${id}`;

export const GET_BUILD_DETAILS_URL = `${COMIXED_API_ROOT}/core/build-details`;
60 changes: 14 additions & 46 deletions comixed-frontend/src/app/library/actions/reading-list.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,73 +17,45 @@
*/

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

export enum ReadingListActionTypes {
GetAll = '[READING LIST] Load reading lists',
AllReceived = '[READING LIST] All reading lists loaded',
GetAllFailed = '[READING LIST] Failed to load reading lists',
Get = '[READING LIST] Get a single reading list',
Received = '[READING LIST] Received a single reading list',
GetFailed = '[READING LIST] Failed get a single reading list',
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'
}

export class ReadingListsLoad implements Action {
readonly type = ReadingListActionTypes.GetAll;

constructor() {}
}

export class ReadingListsLoaded implements Action {
readonly type = ReadingListActionTypes.AllReceived;

constructor(public payload: { reading_lists: ReadingList[] }) {}
}

export class ReadingListLoadFailed implements Action {
readonly type = ReadingListActionTypes.GetAllFailed;
export class ReadingListCreate implements Action {
readonly type = ReadingListActionTypes.Create;

constructor() {}
}

export class ReadingListGet implements Action {
readonly type = ReadingListActionTypes.Get;
export class ReadingListEdit implements Action {
readonly type = ReadingListActionTypes.Edit;

constructor(public payload: { id: number }) {}
constructor(public payload: { readingList: ReadingList }) {}
}

export class ReadingListReceived implements Action {
readonly type = ReadingListActionTypes.Received;

constructor(public payload: { reading_list: ReadingList }) {}
}

export class ReadingListGetFailed implements Action {
readonly type = ReadingListActionTypes.GetFailed;

constructor() {}
}

export class ReadingListCreate implements Action {
readonly type = ReadingListActionTypes.Create;
export class ReadingListCancelEdit implements Action {
readonly type = ReadingListActionTypes.CancelEdit;

constructor() {}
}

export class ReadingListSave implements Action {
readonly type = ReadingListActionTypes.Save;

constructor(public payload: { reading_list: ReadingList }) {}
constructor(public payload: { id: number; name: string; summary: string }) {}
}

export class ReadingListSaved implements Action {
readonly type = ReadingListActionTypes.Saved;

constructor(public payload: { reading_list: ReadingList }) {}
constructor(public payload: { readingList: ReadingList }) {}
}

export class ReadingListSaveFailed implements Action {
Expand All @@ -93,13 +65,9 @@ export class ReadingListSaveFailed implements Action {
}

export type ReadingListActions =
| ReadingListsLoad
| ReadingListsLoaded
| ReadingListLoadFailed
| ReadingListGet
| ReadingListReceived
| ReadingListGetFailed
| ReadingListCreate
| ReadingListEdit
| ReadingListCancelEdit
| ReadingListSave
| ReadingListSaved
| ReadingListSaveFailed;
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
LIBRARY_FEATURE_KEY,
reducer
} from 'app/library/reducers/library.reducer';
import { extractField } from 'app/library/utility.functions';
import { extractField } from 'app/library/library.functions';
import { LoggerModule } from '@angular-ru/logger';
import { MessageService } from 'primeng/api';
import * as LibraryActions from '../actions/library.actions';
Expand Down
14 changes: 11 additions & 3 deletions comixed-frontend/src/app/library/adaptors/library.adaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
} from 'app/library/reducers/library.reducer';
import * as _ from 'lodash';
import { filter } from 'rxjs/operators';
import { extractField } from 'app/library/utility.functions';
import { extractField } from 'app/library/library.functions';
import { LastReadDate } from 'app/library/models/last-read-date';
import {
LibraryConsolidate,
Expand All @@ -42,6 +42,7 @@ import { ComicGetIssue } from 'app/comics/actions/comic.actions';
import { ComicCollectionEntry } from 'app/library/models/comic-collection-entry';
import { LoggerService } from '@angular-ru/logger';
import { CollectionType } from 'app/library/models/collection-type.enum';
import { ReadingList } from 'app/comics/models/reading-list';

@Injectable()
export class LibraryAdaptor {
Expand All @@ -58,6 +59,7 @@ export class LibraryAdaptor {
private _locations$ = new BehaviorSubject<ComicCollectionEntry[]>([]);
private _stories$ = new BehaviorSubject<ComicCollectionEntry[]>([]);
private _readingLists$ = new BehaviorSubject<ComicCollectionEntry[]>([]);
private _lists$ = new BehaviorSubject<ReadingList[]>([]);
private _processingCount$ = new BehaviorSubject<number>(0);
private comicId = -1;
private _timeout = 60;
Expand Down Expand Up @@ -122,8 +124,7 @@ export class LibraryAdaptor {
);
const readingLists = extractField(
state.comics,
CollectionType.READING_LISTS,
state.readingLists
CollectionType.READING_LISTS
);
// merge in any reading lists that have no comics
state.readingLists.forEach(readingList => {
Expand All @@ -143,6 +144,9 @@ export class LibraryAdaptor {
});
this._readingLists$.next(readingLists);
}
if (!_.isEqual(this._lists$.getValue(), state.readingLists)) {
this._lists$.next(state.readingLists);
}
if (
!!state.lastComicId &&
this._lastComicId$.getValue() !== state.lastComicId
Expand Down Expand Up @@ -279,4 +283,8 @@ export class LibraryAdaptor {
get consolidating$(): Observable<boolean> {
return this._consolidating$.asObservable();
}

getReadingList(name: string): ReadingList {
return this._lists$.getValue().find(list => list.name === name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { EffectsModule } from '@ngrx/effects';
import { StoreModule } from '@ngrx/store';
import { Store, StoreModule } from '@ngrx/store';
import { TranslateModule } from '@ngx-translate/core';
import { ReadingListEffects } from 'app/library/effects/reading-list.effects';
import {
Expand All @@ -29,9 +29,27 @@ import {
import { LoggerModule } from '@angular-ru/logger';
import { MessageService } from 'primeng/api';
import { ReadingListAdaptor } from './reading-list.adaptor';
import {
ReadingListCancelEdit,
ReadingListCreate,
ReadingListEdit,
ReadingListSave,
ReadingListSaved,
ReadingListSaveFailed
} 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';

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;

let adaptor: ReadingListAdaptor;
let store: Store<AppState>;

beforeEach(() => {
TestBed.configureTestingModule({
Expand All @@ -48,9 +66,126 @@ describe('ReadingListAdaptor', () => {
});

adaptor = TestBed.get(ReadingListAdaptor);
store = TestBed.get(Store);
spyOn(store, 'dispatch').and.callThrough();
});

it('should create an instance', () => {
expect(adaptor).toBeTruthy();
});

describe('creating a new reading list', () => {
beforeEach(() => {
adaptor.create();
});

it('fires an action', () => {
expect(store.dispatch).toHaveBeenCalledWith(new ReadingListCreate());
});

it('provides updates on the reading list', () => {
adaptor.current$.subscribe(response =>
expect(response).toEqual(NEW_READING_LIST)
);
});

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

describe('editing a reading list', () => {
beforeEach(() => {
adaptor.edit(READING_LIST);
});

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

it('provides updates on the reading list', () => {
adaptor.current$.subscribe(response =>
expect(response).toEqual(READING_LIST)
);
});

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

describe('canceling the edit', () => {
beforeEach(() => {
adaptor.cancelEdit();
});

it('fires an action', () => {
expect(store.dispatch).toHaveBeenCalledWith(
new ReadingListCancelEdit()
);
});

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

describe('saving a reading list', () => {
beforeEach(() => {
adaptor.save(READING_LIST_ID, READING_LIST_NAME, READING_LIST_SUMMARY);
});

it('fires an action', () => {
expect(store.dispatch).toHaveBeenCalledWith(
new ReadingListSave({
id: READING_LIST_ID,
name: READING_LIST_NAME,
summary: READING_LIST_SUMMARY
})
);
});

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

describe('success', () => {
const UPDATED_READING_LIST: ReadingList = {
...READING_LIST,
name: READING_LIST.name.substr(1)
};

beforeEach(() => {
store.dispatch(
new ReadingListSaved({ readingList: UPDATED_READING_LIST })
);
});

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

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

it('provides updates on the reading list', () => {
adaptor.current$.subscribe(response =>
expect(response).toEqual(UPDATED_READING_LIST)
);
});
});

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

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

0 comments on commit 4efb972

Please sign in to comment.