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

* 💚
This commit is contained in:
Tom Moor
2018-02-04 12:30:35 -08:00
committed by GitHub
parent f076582ce4
commit 47da3f2b9b
4 changed files with 29 additions and 36 deletions

View File

@@ -44,7 +44,7 @@ const Auth = ({ children }: Props) => {
} }
stores.auth.fetch(); stores.auth.fetch();
authenticatedStores.collections.fetchAll(); authenticatedStores.collections.fetchPage({ limit: 100 });
} }
return <Provider {...authenticatedStores}>{children}</Provider>; return <Provider {...authenticatedStores}>{children}</Provider>;

View File

@@ -1,6 +1,6 @@
// @flow // @flow
import { observable, computed, action, runInAction, ObservableMap } from 'mobx'; import { observable, computed, action, runInAction, ObservableMap } from 'mobx';
import ApiClient, { client } from 'utils/ApiClient'; import { client } from 'utils/ApiClient';
import _ from 'lodash'; import _ from 'lodash';
import invariant from 'invariant'; import invariant from 'invariant';
@@ -8,6 +8,7 @@ import stores from 'stores';
import Collection from 'models/Collection'; import Collection from 'models/Collection';
import ErrorsStore from 'stores/ErrorsStore'; import ErrorsStore from 'stores/ErrorsStore';
import UiStore from 'stores/UiStore'; import UiStore from 'stores/UiStore';
import type { PaginationParams } from 'types';
type Options = { type Options = {
ui: UiStore, ui: UiStore,
@@ -29,7 +30,6 @@ class CollectionsStore {
@observable isLoaded: boolean = false; @observable isLoaded: boolean = false;
@observable isFetching: boolean = false; @observable isFetching: boolean = false;
client: ApiClient;
errors: ErrorsStore; errors: ErrorsStore;
ui: UiStore; ui: UiStore;
@@ -89,19 +89,20 @@ class CollectionsStore {
/* Actions */ /* Actions */
@action @action
fetchAll = async (): Promise<*> => { fetchPage = async (options: ?PaginationParams): Promise<*> => {
this.isFetching = true; this.isFetching = true;
try { 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'); invariant(res && res.data, 'Collection list not available');
const { data } = res; const { data } = res;
runInAction('CollectionsStore#fetchAll', () => { runInAction('CollectionsStore#fetchPage', () => {
data.forEach(collection => { data.forEach(collection => {
this.data.set(collection.id, new Collection(collection)); this.data.set(collection.id, new Collection(collection));
}); });
this.isLoaded = true; this.isLoaded = true;
}); });
return res;
} catch (e) { } catch (e) {
this.errors.add('Failed to load collections'); this.errors.add('Failed to load collections');
} finally { } finally {
@@ -117,7 +118,7 @@ class CollectionsStore {
this.isFetching = true; this.isFetching = true;
try { try {
const res = await this.client.post('/collections.info', { const res = await client.post('/collections.info', {
id, id,
}); });
invariant(res && res.data, 'Collection not available'); invariant(res && res.data, 'Collection not available');
@@ -152,7 +153,6 @@ class CollectionsStore {
}; };
constructor(options: Options) { constructor(options: Options) {
this.client = client;
this.errors = stores.errors; this.errors = stores.errors;
this.ui = options.ui; this.ui = options.ui;
} }

View File

@@ -1,12 +1,6 @@
/* eslint-disable */ /* eslint-disable */
import CollectionsStore from './CollectionsStore'; import CollectionsStore from './CollectionsStore';
const { client } = require('utils/ApiClient');
jest.mock('utils/ApiClient', () => ({
client: { post: {} },
}));
jest.mock('stores', () => ({
errors: { add: jest.fn() }
}));
describe('CollectionsStore', () => { describe('CollectionsStore', () => {
let store; let store;
@@ -15,32 +9,31 @@ describe('CollectionsStore', () => {
store = new CollectionsStore({}); store = new CollectionsStore({});
}); });
describe('#fetchAll', () => { describe('#fetchPage', () => {
test('should load stores', async () => { test('should load stores', async () => {
store.client = { client.post = jest.fn(() => ({
post: jest.fn(() => ({ data: [
data: [ {
{ id: 123,
id: 123, name: 'New collection',
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.size).toBe(1);
expect(store.data.values()[0].name).toBe('New collection'); expect(store.data.values()[0].name).toBe('New collection');
}); });
test('should report errors', async () => { test('should report errors', async () => {
store.client = { client.post = jest.fn(() => Promise.reject())
post: jest.fn(() => Promise.reject), store.errors = {
add: jest.fn(),
}; };
await store.fetchAll(); await store.fetchPage();
expect(store.errors.add).toHaveBeenCalledWith( expect(store.errors.add).toHaveBeenCalledWith(
'Failed to load collections' 'Failed to load collections'

View File

@@ -85,7 +85,7 @@ class DocumentsStore extends BaseStore {
/* Actions */ /* Actions */
@action @action
fetchAll = async ( fetchPage = async (
request: string = 'list', request: string = 'list',
options: ?PaginationParams options: ?PaginationParams
): Promise<*> => { ): Promise<*> => {
@@ -95,7 +95,7 @@ class DocumentsStore extends BaseStore {
const res = await client.post(`/documents.${request}`, options); const res = await client.post(`/documents.${request}`, options);
invariant(res && res.data, 'Document list not available'); invariant(res && res.data, 'Document list not available');
const { data } = res; const { data } = res;
runInAction('DocumentsStore#fetchAll', () => { runInAction('DocumentsStore#fetchPage', () => {
data.forEach(document => { data.forEach(document => {
this.data.set(document.id, new Document(document)); this.data.set(document.id, new Document(document));
}); });
@@ -111,7 +111,7 @@ class DocumentsStore extends BaseStore {
@action @action
fetchRecentlyModified = async (options: ?PaginationParams): Promise<*> => { fetchRecentlyModified = async (options: ?PaginationParams): Promise<*> => {
const data = await this.fetchAll('list', options); const data = await this.fetchPage('list', options);
runInAction('DocumentsStore#fetchRecentlyModified', () => { runInAction('DocumentsStore#fetchRecentlyModified', () => {
this.recentlyEditedIds = _.map(data, 'id'); this.recentlyEditedIds = _.map(data, 'id');
@@ -121,7 +121,7 @@ class DocumentsStore extends BaseStore {
@action @action
fetchRecentlyViewed = async (options: ?PaginationParams): Promise<*> => { fetchRecentlyViewed = async (options: ?PaginationParams): Promise<*> => {
const data = await this.fetchAll('viewed', options); const data = await this.fetchPage('viewed', options);
runInAction('DocumentsStore#fetchRecentlyViewed', () => { runInAction('DocumentsStore#fetchRecentlyViewed', () => {
this.recentlyViewedIds = _.map(data, 'id'); this.recentlyViewedIds = _.map(data, 'id');
@@ -131,7 +131,7 @@ class DocumentsStore extends BaseStore {
@action @action
fetchStarred = async (): Promise<*> => { fetchStarred = async (): Promise<*> => {
await this.fetchAll('starred'); await this.fetchPage('starred');
}; };
@action @action