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();
authenticatedStores.collections.fetchAll();
authenticatedStores.collections.fetchPage({ limit: 100 });
}
return <Provider {...authenticatedStores}>{children}</Provider>;

View File

@@ -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;
}

View File

@@ -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'

View File

@@ -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