add handle success functionality to piece list and implement onject merger for components with multiple stores

This commit is contained in:
Tim Daubenschütz 2015-06-04 15:30:21 +02:00
parent a93634f010
commit 1230ae7192
3 changed files with 61 additions and 13 deletions

View File

@ -1,5 +1,7 @@
import React from 'react';
import { mergeOptionList } from '../../utils/general_utils';
import EditionListStore from '../../stores/edition_list_store';
import EditionListActions from '../../actions/edition_list_actions';
@ -15,10 +17,7 @@ let PieceListBulkModal = React.createClass({
},
getInitialState() {
return {
editions: EditionListStore.getState(),
user: UserStore.getState()
};
return mergeOptionList([EditionListStore.getState(), UserStore.getState()]);
},
onChange(state) {
@ -26,9 +25,9 @@ let PieceListBulkModal = React.createClass({
},
componentDidMount() {
UserActions.fetchCurrentUser();
EditionListStore.listen(this.onChange);
UserStore.listen(this.onChange);
UserActions.fetchCurrentUser();
},
componentWillUnmount() {
@ -36,13 +35,22 @@ let PieceListBulkModal = React.createClass({
UserStore.unlisten(this.onChange);
},
fetchSelectedPieceEditionList() {
let filteredPieceIdList = Object.keys(this.state.editionList)
.filter((pieceId) => {
return this.state.editions.editionList[pieceId]
.filter((edition) => edition.selected).length > 0;
});
return filteredPieceIdList;
},
fetchSelectedEditionList() {
let selectedEditionList = [];
Object
.keys(this.state.editions.editionList)
.forEach((key) => {
let filteredEditionsForPiece = this.state.editions.editionList[key].filter((edition) => edition.selected);
.keys(this.state.editionList)
.forEach((pieceId) => {
let filteredEditionsForPiece = this.state.editionList[pieceId].filter((edition) => edition.selected);
selectedEditionList = selectedEditionList.concat(filteredEditionsForPiece);
});
@ -77,7 +85,12 @@ let PieceListBulkModal = React.createClass({
},
handleSuccess() {
this.fetchSelectedPieceEditionList()
.forEach((pieceId) => {
EditionListActions.fetchEditionList(pieceId, this.state.orderBy, this.state.orderAsc);
});
EditionListActions.clearAllEditionSelections();
},
render() {
@ -107,25 +120,25 @@ let PieceListBulkModal = React.createClass({
availableAcls={availableAcls}
action="transfer"
editions={selectedEditions}
currentUser={this.state.user.currentUser}
currentUser={this.state.currentUser}
handleSuccess={this.handleSuccess} />
<AclButton
availableAcls={availableAcls}
action="consign"
editions={selectedEditions}
currentUser={this.state.user.currentUser}
currentUser={this.state.currentUser}
handleSuccess={this.handleSuccess} />
<AclButton
availableAcls={availableAcls}
action="loan"
editions={selectedEditions}
currentUser={this.state.user.currentUser}
currentUser={this.state.currentUser}
handleSuccess={this.handleSuccess} />
<AclButton
availableAcls={availableAcls}
action="share"
editions={selectedEditions}
currentUser={this.state.user.currentUser}
currentUser={this.state.currentUser}
handleSuccess={this.handleSuccess} />
</div>
</div>

View File

@ -1,4 +1,6 @@
import React from 'react';
import alt from '../alt';
import PieceListActions from '../actions/piece_list_actions';
@ -53,6 +55,10 @@ class PieceListStore {
this.pieceListCount = pieceListCount;
/**
* Pagination - Known Issue:
* #########################
*
*
* The piece list store currently stores the open/close state of a piece list item.
*
* Once a new page is requested, this.pieceList will be overwritten, which means that the
@ -64,7 +70,6 @@ class PieceListStore {
*
* We did not implement this, as we're going to add pagination to pieceList at some
* point anyway. Then, this problem is automatically resolved.
*
*/
this.pieceList = pieceList;
}

View File

@ -61,3 +61,33 @@ export function formatText() {
return val;
});
};
/**
* Takes a list of object and merges their keys to one object.
* Uses mergeOptions for two objects.
* @param {[type]} l [description]
* @return {[type]} [description]
*/
export function mergeOptionList(l) {
let newObj = {};
for(let i = 1; i < l.length; i++) {
newObj = mergeOptions(newObj, mergeOptions(l[i-1], l[i]));
}
return newObj;
};
/**
* Overwrites obj1's values with obj2's and adds obj2's if non existent in obj1
* @param obj1
* @param obj2
* @returns obj3 a new object based on obj1 and obj2
* Taken from: http://stackoverflow.com/a/171256/1263876
*/
function mergeOptions(obj1,obj2){
var obj3 = {};
for (var attrname in obj1) { obj3[attrname] = obj1[attrname]; }
for (var attrname in obj2) { obj3[attrname] = obj2[attrname]; }
return obj3;
};