2015-06-05 11:06:36 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-06 16:47:59 +02:00
|
|
|
import { alt } from '../alt';
|
2015-07-24 13:44:28 +02:00
|
|
|
import Q from 'q';
|
2015-05-21 17:59:38 +02:00
|
|
|
|
|
|
|
import EditionListFetcher from '../fetchers/edition_list_fetcher.js';
|
|
|
|
|
|
|
|
class EditionListActions {
|
|
|
|
constructor() {
|
|
|
|
this.generateActions(
|
2015-05-26 16:48:48 +02:00
|
|
|
'updateEditionList',
|
2015-07-14 14:45:33 +02:00
|
|
|
'refreshEditionList',
|
2015-06-01 15:12:31 +02:00
|
|
|
'selectEdition',
|
2015-06-12 17:18:40 +02:00
|
|
|
'clearAllEditionSelections',
|
2015-06-15 09:29:34 +02:00
|
|
|
'closeAllEditionLists',
|
2015-06-12 17:18:40 +02:00
|
|
|
'toggleEditionList'
|
2015-05-21 17:59:38 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-01-18 10:53:01 +01:00
|
|
|
fetchEditionList({ pieceId, page, pageSize, orderBy, orderAsc, filterBy, maxEdition }) {
|
2016-01-18 10:17:49 +01:00
|
|
|
if ((!orderBy && typeof orderAsc === 'undefined') || !orderAsc) {
|
2015-06-04 16:41:14 +02:00
|
|
|
orderBy = 'edition_number';
|
|
|
|
orderAsc = true;
|
|
|
|
}
|
|
|
|
|
2015-07-03 16:31:09 +02:00
|
|
|
// Taken from: http://stackoverflow.com/a/519157/1263876
|
2016-01-18 10:40:13 +01:00
|
|
|
if ((typeof page === 'undefined' || !page) && (typeof pageSize === 'undefined' || !pageSize)) {
|
2015-07-03 16:31:09 +02:00
|
|
|
page = 1;
|
|
|
|
pageSize = 10;
|
|
|
|
}
|
|
|
|
|
2015-12-08 13:41:15 +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) => {
|
2015-06-12 17:18:40 +02:00
|
|
|
EditionListFetcher
|
2016-01-25 10:23:20 +01:00
|
|
|
.fetch({ pieceId, page, orderBy, orderAsc, filterBy, pageSize: itemsToFetch })
|
2015-06-12 17:18:40 +02:00
|
|
|
.then((res) => {
|
2015-12-08 13:41:15 +01:00
|
|
|
if (res && !res.editions) {
|
2015-09-30 11:22:20 +02:00
|
|
|
throw new Error('Piece has no editions to fetch.');
|
|
|
|
}
|
|
|
|
|
2015-06-12 17:18:40 +02:00
|
|
|
this.actions.updateEditionList({
|
|
|
|
pieceId,
|
2015-07-03 16:31:09 +02:00
|
|
|
page,
|
|
|
|
pageSize,
|
2015-06-12 17:18:40 +02:00
|
|
|
orderBy,
|
2015-07-06 15:56:14 +02:00
|
|
|
orderAsc,
|
2015-08-04 15:06:02 +02:00
|
|
|
filterBy,
|
2015-12-09 18:03:52 +01:00
|
|
|
maxEdition,
|
|
|
|
count: res.count,
|
|
|
|
editionListOfPiece: res.editions
|
2015-06-12 17:18:40 +02:00
|
|
|
});
|
|
|
|
resolve(res);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2015-09-30 11:22:20 +02:00
|
|
|
console.logGlobal(err);
|
2015-06-12 17:18:40 +02:00
|
|
|
reject(err);
|
2015-05-26 13:14:35 +02:00
|
|
|
});
|
2015-06-12 17:18:40 +02:00
|
|
|
});
|
2015-05-21 17:59:38 +02:00
|
|
|
}
|
2015-05-26 13:33:35 +02:00
|
|
|
}
|
2015-05-21 17:59:38 +02:00
|
|
|
|
2015-06-05 14:14:59 +02:00
|
|
|
export default alt.createActions(EditionListActions);
|