mirror of
https://github.com/ascribe/onion.git
synced 2025-01-03 10:25:08 +01:00
Add option to specify the max edition to fetch and update the edition list to
This commit is contained in:
parent
3d017392c1
commit
4fa25ca446
@ -17,7 +17,7 @@ class EditionListActions {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchEditionList(pieceId, page, pageSize, orderBy, orderAsc, filterBy) {
|
fetchEditionList(pieceId, page, pageSize, orderBy, orderAsc, filterBy, maxEdition) {
|
||||||
if((!orderBy && typeof orderAsc === 'undefined') || !orderAsc) {
|
if((!orderBy && typeof orderAsc === 'undefined') || !orderAsc) {
|
||||||
orderBy = 'edition_number';
|
orderBy = 'edition_number';
|
||||||
orderAsc = true;
|
orderAsc = true;
|
||||||
@ -29,23 +29,32 @@ class EditionListActions {
|
|||||||
pageSize = 10;
|
pageSize = 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
return Q.Promise((resolve, reject) => {
|
return Q.Promise((resolve, reject) => {
|
||||||
EditionListFetcher
|
EditionListFetcher
|
||||||
.fetch(pieceId, page, pageSize, orderBy, orderAsc, filterBy)
|
.fetch(pieceId, page, itemsToFetch, orderBy, orderAsc, filterBy)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
if(res && !res.editions) {
|
if (res && !res.editions) {
|
||||||
throw new Error('Piece has no editions to fetch.');
|
throw new Error('Piece has no editions to fetch.');
|
||||||
}
|
}
|
||||||
|
|
||||||
this.actions.updateEditionList({
|
this.actions.updateEditionList({
|
||||||
pieceId,
|
pieceId,
|
||||||
|
editionListOfPiece: res.editions,
|
||||||
page,
|
page,
|
||||||
pageSize,
|
pageSize,
|
||||||
orderBy,
|
orderBy,
|
||||||
orderAsc,
|
orderAsc,
|
||||||
|
count: res.count,
|
||||||
filterBy,
|
filterBy,
|
||||||
'editionListOfPiece': res.editions,
|
maxEdition
|
||||||
'count': res.count
|
|
||||||
});
|
});
|
||||||
resolve(res);
|
resolve(res);
|
||||||
})
|
})
|
||||||
@ -54,7 +63,6 @@ class EditionListActions {
|
|||||||
reject(err);
|
reject(err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,7 +63,7 @@ let PieceList = React.createClass({
|
|||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
let page = this.props.location.query.page || 1;
|
let page = this.props.location.query.page || 1;
|
||||||
|
|
||||||
PieceListStore.listen(this.onChange);
|
PieceListStore.listen(this.onChange);
|
||||||
EditionListStore.listen(this.onChange);
|
EditionListStore.listen(this.onChange);
|
||||||
|
|
||||||
|
@ -12,7 +12,11 @@ class EditionListStore {
|
|||||||
this.bindActions(EditionsListActions);
|
this.bindActions(EditionsListActions);
|
||||||
}
|
}
|
||||||
|
|
||||||
onUpdateEditionList({pieceId, editionListOfPiece, page, pageSize, orderBy, orderAsc, count, filterBy}) {
|
onUpdateEditionList({pieceId, editionListOfPiece, page, pageSize, orderBy, orderAsc, count, filterBy, maxEdition}) {
|
||||||
|
// if editionList for a specific piece does not exist yet,
|
||||||
|
// just initialize a new array
|
||||||
|
const pieceEditionList = this.editionList[pieceId] || [];
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Basically there are two modes an edition list can be updated.
|
Basically there are two modes an edition list can be updated.
|
||||||
|
|
||||||
@ -20,40 +24,61 @@ class EditionListStore {
|
|||||||
2. The elements are already defined => merge current objects with the new ones from the server
|
2. The elements are already defined => merge current objects with the new ones from the server
|
||||||
|
|
||||||
*/
|
*/
|
||||||
for(let i = 0; i < editionListOfPiece.length; i++) {
|
editionListOfPiece.forEach((updatedEdition, index) => {
|
||||||
|
// this is the index formula for accessing an edition starting from a specific page
|
||||||
// if editionList for a specific piece does not exist yet,
|
const storeEditionIndex = (page - 1) * pageSize + index;
|
||||||
// just initialize a new array
|
|
||||||
if(!this.editionList[pieceId]) {
|
|
||||||
this.editionList[pieceId] = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
// this is the index formula for accessing an edition of a specific
|
|
||||||
// page
|
|
||||||
let storeEditionIndex = (page - 1) * pageSize + i;
|
|
||||||
let editionsForPieces = this.editionList[pieceId];
|
|
||||||
|
|
||||||
// if edition already exists, just merge
|
// if edition already exists, just merge
|
||||||
if(editionsForPieces[storeEditionIndex]) {
|
if(pieceEditionList[storeEditionIndex]) {
|
||||||
editionsForPieces[storeEditionIndex] = React.addons.update(editionsForPieces[storeEditionIndex], {$merge: editionListOfPiece[i]});
|
pieceEditionList[storeEditionIndex] = React.addons.update(pieceEditionList[storeEditionIndex], {$merge: updatedEdition});
|
||||||
} else {
|
} else {
|
||||||
// if does not exist, assign
|
// if does not exist, assign
|
||||||
editionsForPieces[storeEditionIndex] = editionListOfPiece[i];
|
pieceEditionList[storeEditionIndex] = updatedEdition;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Remove editions after specified max by finding the index of the first
|
||||||
|
// edition larger than the max edition and using that to cut off the rest of the list
|
||||||
|
if (typeof maxEdition === 'number') {
|
||||||
|
const largerThanMaxIndex = pieceEditionList.findIndex(edition => edition.edition_number > maxEdition);
|
||||||
|
|
||||||
|
if (largerThanMaxIndex !== -1) {
|
||||||
|
// The API defines inflexible page buckets based on the page number
|
||||||
|
// and page size, so we cannot just arbitrarily cut off the end of
|
||||||
|
// a page and expect get the rest of it on the next pagination request.
|
||||||
|
// Hence, we use the max edition index as a guide for which page to
|
||||||
|
// cut off to so as to always provide complete pages.
|
||||||
|
page = Math.ceil(largerThanMaxIndex / pageSize);
|
||||||
|
|
||||||
|
// We only want to cut off the list if there are more editions than
|
||||||
|
// there should be (ie. we're not already at the end of the editions)
|
||||||
|
const totalPageSize = page * pageSize;
|
||||||
|
if (pieceEditionList.length > totalPageSize) {
|
||||||
|
pieceEditionList.length = totalPageSize;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const lastEdition = pieceEditionList[pieceEditionList.length - 1];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* page, pageSize, orderBy, orderAsc and count are specific to a single list of editions
|
* page, pageSize, orderBy, orderAsc and count are specific to a single list of editions
|
||||||
* therefore they need to be saved in relation to their parent-piece.
|
* therefore they need to be saved in relation to their parent-piece.
|
||||||
*
|
*
|
||||||
* Default values for both are set in the editon_list_actions.
|
* Default values for both are set in the editon_list_actions.
|
||||||
*/
|
*/
|
||||||
this.editionList[pieceId].page = page;
|
pieceEditionList.page = page;
|
||||||
this.editionList[pieceId].pageSize = pageSize;
|
pieceEditionList.pageSize = pageSize;
|
||||||
this.editionList[pieceId].orderBy = orderBy;
|
pieceEditionList.orderBy = orderBy;
|
||||||
this.editionList[pieceId].orderAsc = orderAsc;
|
pieceEditionList.orderAsc = orderAsc;
|
||||||
this.editionList[pieceId].count = count;
|
pieceEditionList.count = count;
|
||||||
this.editionList[pieceId].filterBy = filterBy;
|
pieceEditionList.filterBy = filterBy;
|
||||||
|
|
||||||
|
if (pieceEditionList.maxSeen == null || lastEdition.edition_number > pieceEditionList.maxSeen) {
|
||||||
|
pieceEditionList.maxSeen = lastEdition.edition_number;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.editionList[pieceId] = pieceEditionList;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user