1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-23 01:39:36 +01:00
onion/js/actions/edition_list_actions.js

70 lines
2.2 KiB
JavaScript
Raw Normal View History

'use strict';
import { alt } from '../alt';
2015-07-24 13:44:28 +02:00
import Q from 'q';
import EditionListFetcher from '../fetchers/edition_list_fetcher.js';
class EditionListActions {
constructor() {
this.generateActions(
2015-05-26 16:48:48 +02:00
'updateEditionList',
'refreshEditionList',
2015-06-01 15:12:31 +02:00
'selectEdition',
'clearAllEditionSelections',
'closeAllEditionLists',
'toggleEditionList'
);
}
2016-02-05 10:38:59 +01:00
fetchEditionList({ pieceId, page, pageSize, orderBy, orderAsc, filterBy, maxEdition }) {
if ((!orderBy && typeof orderAsc === 'undefined') || !orderAsc) {
orderBy = 'edition_number';
orderAsc = true;
}
2015-07-03 16:31:09 +02:00
// Taken from: http://stackoverflow.com/a/519157/1263876
2016-02-05 10:38:59 +01:00
if ((typeof page === 'undefined' || !page) && (typeof pageSize === 'undefined' || !pageSize)) {
2015-07-03 16:31:09 +02:00
page = 1;
pageSize = 10;
}
2016-02-05 10:38:59 +01:00
let itemsToFetch = pageSize;
// If we only want to fetch up to a specified edition, fetch all pages up to it
// as one page and adjust afterwards
if (typeof maxEdition === 'number') {
itemsToFetch = Math.ceil(maxEdition / pageSize) * pageSize;
page = 1;
}
2015-07-24 13:44:28 +02:00
return Q.Promise((resolve, reject) => {
EditionListFetcher
2016-02-05 10:38:59 +01:00
.fetch({ pieceId, page, orderBy, orderAsc, filterBy, pageSize: itemsToFetch })
.then((res) => {
2016-02-05 10:38:59 +01:00
if (res && !res.editions) {
throw new Error('Piece has no editions to fetch.');
}
this.actions.updateEditionList({
pieceId,
2015-07-03 16:31:09 +02:00
page,
pageSize,
orderBy,
2015-07-06 15:56:14 +02:00
orderAsc,
filterBy,
2016-02-05 10:38:59 +01:00
maxEdition,
count: res.count,
editionListOfPiece: res.editions
});
resolve(res);
})
.catch((err) => {
console.logGlobal(err);
reject(err);
2015-05-26 13:14:35 +02:00
});
});
}
2015-05-26 13:33:35 +02:00
}
2015-06-05 14:14:59 +02:00
export default alt.createActions(EditionListActions);