2015-06-05 11:06:36 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-06-12 15:00:26 +02:00
|
|
|
import React from 'react';
|
2015-05-19 17:13:09 +02:00
|
|
|
import alt from '../alt';
|
2015-06-04 15:30:21 +02:00
|
|
|
|
2015-05-19 17:13:09 +02:00
|
|
|
import PieceListActions from '../actions/piece_list_actions';
|
|
|
|
|
2015-05-20 16:44:45 +02:00
|
|
|
|
2015-05-19 17:13:09 +02:00
|
|
|
class PieceListStore {
|
|
|
|
constructor() {
|
2015-05-22 13:43:53 +02:00
|
|
|
/**
|
2015-06-05 11:06:36 +02:00
|
|
|
* The store manages the state that is introduced by fetching
|
2015-05-22 13:43:53 +02:00
|
|
|
* the resource with certain parameters.
|
|
|
|
*
|
|
|
|
* This means that pieceList for example only contains pageSize-many items.
|
|
|
|
* Of course this can be altered by page as well.
|
|
|
|
*
|
|
|
|
* This is also the reason why we store a pieceListCount, which is essentially
|
|
|
|
* the number of items the resource actually - without pagination - provides.
|
|
|
|
*/
|
|
|
|
this.pieceList = [];
|
2015-07-13 19:12:24 +02:00
|
|
|
// -1 specifies that the store is currently loading
|
2015-07-07 09:22:46 +02:00
|
|
|
this.pieceListCount = -1;
|
2015-05-21 12:12:25 +02:00
|
|
|
this.page = 1;
|
|
|
|
this.pageSize = 10;
|
2015-06-05 11:06:36 +02:00
|
|
|
this.search = '';
|
|
|
|
this.orderBy = 'artist_name';
|
2015-05-21 12:12:25 +02:00
|
|
|
this.orderAsc = true;
|
2015-05-19 17:13:09 +02:00
|
|
|
this.bindActions(PieceListActions);
|
|
|
|
}
|
2015-06-04 10:25:31 +02:00
|
|
|
|
2015-05-22 13:43:53 +02:00
|
|
|
onUpdatePieceList({ page, pageSize, search, pieceList, orderBy, orderAsc, pieceListCount }) {
|
2015-05-21 17:54:27 +02:00
|
|
|
this.page = page;
|
|
|
|
this.pageSize = pageSize;
|
|
|
|
this.search = search;
|
2015-05-21 12:12:25 +02:00
|
|
|
this.orderAsc = orderAsc;
|
|
|
|
this.orderBy = orderBy;
|
2015-05-22 13:43:53 +02:00
|
|
|
this.pieceListCount = pieceListCount;
|
2015-06-04 10:25:31 +02:00
|
|
|
|
|
|
|
/**
|
2015-06-04 15:30:21 +02:00
|
|
|
* Pagination - Known Issue:
|
|
|
|
* #########################
|
|
|
|
*
|
2015-06-05 11:06:36 +02:00
|
|
|
*
|
2015-06-04 10:25:31 +02:00
|
|
|
* The piece list store currently stores the open/close state of a piece list item.
|
|
|
|
*
|
2015-06-05 11:06:36 +02:00
|
|
|
* Once a new page is requested, this.pieceList will be overwritten, which means that the
|
2015-06-04 10:25:31 +02:00
|
|
|
* open/close state of a specific list item will be thrown away.
|
|
|
|
*
|
2015-06-05 11:06:36 +02:00
|
|
|
* This means that when opening an editionListTable on a piece, and continuing
|
2015-06-04 10:25:31 +02:00
|
|
|
* clicking next or back in the pagination, the editionListTable will return to its
|
|
|
|
* default value, which is "close".
|
|
|
|
*
|
|
|
|
* We did not implement this, as we're going to add pagination to pieceList at some
|
|
|
|
* point anyway. Then, this problem is automatically resolved.
|
|
|
|
*/
|
2015-06-12 15:00:26 +02:00
|
|
|
pieceList.forEach((piece, i) => {
|
|
|
|
let oldPiece = this.pieceList[i];
|
|
|
|
if(oldPiece) {
|
|
|
|
piece = React.addons.update(piece, {
|
|
|
|
show: { $set: oldPiece.show }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2015-06-04 10:25:31 +02:00
|
|
|
this.pieceList = pieceList;
|
2015-05-19 17:13:09 +02:00
|
|
|
}
|
2015-07-09 15:44:20 +02:00
|
|
|
|
2015-07-01 19:05:47 +02:00
|
|
|
onUpdatePieceListRequestActions(requestActions) {
|
|
|
|
this.pieceList.forEach((piece) => {
|
|
|
|
piece.requestAction = requestActions.indexOf(piece.id) > -1;
|
|
|
|
});
|
|
|
|
}
|
2015-07-09 15:44:20 +02:00
|
|
|
|
2015-07-10 13:54:25 +02:00
|
|
|
onUpdatePropertyForPiece({pieceId, key, value}) {
|
|
|
|
let filteredPieceList = this.pieceList.filter((piece) => piece.id === pieceId);
|
|
|
|
|
|
|
|
if(filteredPieceList.length === 1) {
|
|
|
|
|
|
|
|
let piece = filteredPieceList[0];
|
|
|
|
piece[key] = value;
|
|
|
|
} else {
|
|
|
|
throw new Error('Could not find a matching piece in piece list since its either not there or piecelist contains duplicates.');
|
|
|
|
}
|
|
|
|
}
|
2015-06-05 11:06:36 +02:00
|
|
|
}
|
2015-05-19 17:13:09 +02:00
|
|
|
|
2015-06-15 08:44:44 +02:00
|
|
|
export default alt.createStore(PieceListStore, 'PieceListStore');
|