From 47da3f2b9bd31fb1c68bd52daa953b2a4cd28492 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 4 Feb 2018 12:30:35 -0800 Subject: [PATCH] Increase collections pagination limit (#561) * I think this is the pragmatic solution for now. We can readdress later. Also renamed fetchAll, to the more accurate fetchPage * :green_heart: --- app/components/Auth.js | 2 +- app/stores/CollectionsStore.js | 14 +++++------ app/stores/CollectionsStore.test.js | 39 ++++++++++++----------------- app/stores/DocumentsStore.js | 10 ++++---- 4 files changed, 29 insertions(+), 36 deletions(-) diff --git a/app/components/Auth.js b/app/components/Auth.js index d4a85cbd6..b681398f2 100644 --- a/app/components/Auth.js +++ b/app/components/Auth.js @@ -44,7 +44,7 @@ const Auth = ({ children }: Props) => { } stores.auth.fetch(); - authenticatedStores.collections.fetchAll(); + authenticatedStores.collections.fetchPage({ limit: 100 }); } return {children}; diff --git a/app/stores/CollectionsStore.js b/app/stores/CollectionsStore.js index 67973f4b7..45c4dbb95 100644 --- a/app/stores/CollectionsStore.js +++ b/app/stores/CollectionsStore.js @@ -1,6 +1,6 @@ // @flow import { observable, computed, action, runInAction, ObservableMap } from 'mobx'; -import ApiClient, { client } from 'utils/ApiClient'; +import { client } from 'utils/ApiClient'; import _ from 'lodash'; import invariant from 'invariant'; @@ -8,6 +8,7 @@ import stores from 'stores'; import Collection from 'models/Collection'; import ErrorsStore from 'stores/ErrorsStore'; import UiStore from 'stores/UiStore'; +import type { PaginationParams } from 'types'; type Options = { ui: UiStore, @@ -29,7 +30,6 @@ class CollectionsStore { @observable isLoaded: boolean = false; @observable isFetching: boolean = false; - client: ApiClient; errors: ErrorsStore; ui: UiStore; @@ -89,19 +89,20 @@ class CollectionsStore { /* Actions */ @action - fetchAll = async (): Promise<*> => { + fetchPage = async (options: ?PaginationParams): Promise<*> => { this.isFetching = true; try { - const res = await this.client.post('/collections.list'); + const res = await client.post('/collections.list', options); invariant(res && res.data, 'Collection list not available'); const { data } = res; - runInAction('CollectionsStore#fetchAll', () => { + runInAction('CollectionsStore#fetchPage', () => { data.forEach(collection => { this.data.set(collection.id, new Collection(collection)); }); this.isLoaded = true; }); + return res; } catch (e) { this.errors.add('Failed to load collections'); } finally { @@ -117,7 +118,7 @@ class CollectionsStore { this.isFetching = true; try { - const res = await this.client.post('/collections.info', { + const res = await client.post('/collections.info', { id, }); invariant(res && res.data, 'Collection not available'); @@ -152,7 +153,6 @@ class CollectionsStore { }; constructor(options: Options) { - this.client = client; this.errors = stores.errors; this.ui = options.ui; } diff --git a/app/stores/CollectionsStore.test.js b/app/stores/CollectionsStore.test.js index 674843847..841ca4efa 100644 --- a/app/stores/CollectionsStore.test.js +++ b/app/stores/CollectionsStore.test.js @@ -1,12 +1,6 @@ /* eslint-disable */ import CollectionsStore from './CollectionsStore'; - -jest.mock('utils/ApiClient', () => ({ - client: { post: {} }, -})); -jest.mock('stores', () => ({ - errors: { add: jest.fn() } -})); +const { client } = require('utils/ApiClient'); describe('CollectionsStore', () => { let store; @@ -15,32 +9,31 @@ describe('CollectionsStore', () => { store = new CollectionsStore({}); }); - describe('#fetchAll', () => { + describe('#fetchPage', () => { test('should load stores', async () => { - store.client = { - post: jest.fn(() => ({ - data: [ - { - id: 123, - name: 'New collection', - }, - ], - })), - }; + client.post = jest.fn(() => ({ + data: [ + { + id: 123, + name: 'New collection', + }, + ], + })) - await store.fetchAll(); + await store.fetchPage(); - expect(store.client.post).toHaveBeenCalledWith('/collections.list'); + expect(client.post).toHaveBeenCalledWith('/collections.list', undefined); expect(store.data.size).toBe(1); expect(store.data.values()[0].name).toBe('New collection'); }); test('should report errors', async () => { - store.client = { - post: jest.fn(() => Promise.reject), + client.post = jest.fn(() => Promise.reject()) + store.errors = { + add: jest.fn(), }; - await store.fetchAll(); + await store.fetchPage(); expect(store.errors.add).toHaveBeenCalledWith( 'Failed to load collections' diff --git a/app/stores/DocumentsStore.js b/app/stores/DocumentsStore.js index bfda9c6e1..dc59f02dc 100644 --- a/app/stores/DocumentsStore.js +++ b/app/stores/DocumentsStore.js @@ -85,7 +85,7 @@ class DocumentsStore extends BaseStore { /* Actions */ @action - fetchAll = async ( + fetchPage = async ( request: string = 'list', options: ?PaginationParams ): Promise<*> => { @@ -95,7 +95,7 @@ class DocumentsStore extends BaseStore { const res = await client.post(`/documents.${request}`, options); invariant(res && res.data, 'Document list not available'); const { data } = res; - runInAction('DocumentsStore#fetchAll', () => { + runInAction('DocumentsStore#fetchPage', () => { data.forEach(document => { this.data.set(document.id, new Document(document)); }); @@ -111,7 +111,7 @@ class DocumentsStore extends BaseStore { @action fetchRecentlyModified = async (options: ?PaginationParams): Promise<*> => { - const data = await this.fetchAll('list', options); + const data = await this.fetchPage('list', options); runInAction('DocumentsStore#fetchRecentlyModified', () => { this.recentlyEditedIds = _.map(data, 'id'); @@ -121,7 +121,7 @@ class DocumentsStore extends BaseStore { @action fetchRecentlyViewed = async (options: ?PaginationParams): Promise<*> => { - const data = await this.fetchAll('viewed', options); + const data = await this.fetchPage('viewed', options); runInAction('DocumentsStore#fetchRecentlyViewed', () => { this.recentlyViewedIds = _.map(data, 'id'); @@ -131,7 +131,7 @@ class DocumentsStore extends BaseStore { @action fetchStarred = async (): Promise<*> => { - await this.fetchAll('starred'); + await this.fetchPage('starred'); }; @action