mirror of
https://github.com/ascribe/onion.git
synced 2024-11-15 09:35:10 +01:00
add handle success functionality to piece list and implement onject merger for components with multiple stores
This commit is contained in:
parent
a93634f010
commit
1230ae7192
@ -1,5 +1,7 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
|
||||||
|
import { mergeOptionList } from '../../utils/general_utils';
|
||||||
|
|
||||||
import EditionListStore from '../../stores/edition_list_store';
|
import EditionListStore from '../../stores/edition_list_store';
|
||||||
import EditionListActions from '../../actions/edition_list_actions';
|
import EditionListActions from '../../actions/edition_list_actions';
|
||||||
|
|
||||||
@ -15,10 +17,7 @@ let PieceListBulkModal = React.createClass({
|
|||||||
},
|
},
|
||||||
|
|
||||||
getInitialState() {
|
getInitialState() {
|
||||||
return {
|
return mergeOptionList([EditionListStore.getState(), UserStore.getState()]);
|
||||||
editions: EditionListStore.getState(),
|
|
||||||
user: UserStore.getState()
|
|
||||||
};
|
|
||||||
},
|
},
|
||||||
|
|
||||||
onChange(state) {
|
onChange(state) {
|
||||||
@ -26,9 +25,9 @@ let PieceListBulkModal = React.createClass({
|
|||||||
},
|
},
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
UserActions.fetchCurrentUser();
|
|
||||||
EditionListStore.listen(this.onChange);
|
EditionListStore.listen(this.onChange);
|
||||||
UserStore.listen(this.onChange);
|
UserStore.listen(this.onChange);
|
||||||
|
UserActions.fetchCurrentUser();
|
||||||
},
|
},
|
||||||
|
|
||||||
componentWillUnmount() {
|
componentWillUnmount() {
|
||||||
@ -36,13 +35,22 @@ let PieceListBulkModal = React.createClass({
|
|||||||
UserStore.unlisten(this.onChange);
|
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() {
|
fetchSelectedEditionList() {
|
||||||
let selectedEditionList = [];
|
let selectedEditionList = [];
|
||||||
|
|
||||||
Object
|
Object
|
||||||
.keys(this.state.editions.editionList)
|
.keys(this.state.editionList)
|
||||||
.forEach((key) => {
|
.forEach((pieceId) => {
|
||||||
let filteredEditionsForPiece = this.state.editions.editionList[key].filter((edition) => edition.selected);
|
let filteredEditionsForPiece = this.state.editionList[pieceId].filter((edition) => edition.selected);
|
||||||
selectedEditionList = selectedEditionList.concat(filteredEditionsForPiece);
|
selectedEditionList = selectedEditionList.concat(filteredEditionsForPiece);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -77,7 +85,12 @@ let PieceListBulkModal = React.createClass({
|
|||||||
},
|
},
|
||||||
|
|
||||||
handleSuccess() {
|
handleSuccess() {
|
||||||
|
this.fetchSelectedPieceEditionList()
|
||||||
|
.forEach((pieceId) => {
|
||||||
|
EditionListActions.fetchEditionList(pieceId, this.state.orderBy, this.state.orderAsc);
|
||||||
|
});
|
||||||
|
|
||||||
|
EditionListActions.clearAllEditionSelections();
|
||||||
},
|
},
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
@ -107,25 +120,25 @@ let PieceListBulkModal = React.createClass({
|
|||||||
availableAcls={availableAcls}
|
availableAcls={availableAcls}
|
||||||
action="transfer"
|
action="transfer"
|
||||||
editions={selectedEditions}
|
editions={selectedEditions}
|
||||||
currentUser={this.state.user.currentUser}
|
currentUser={this.state.currentUser}
|
||||||
handleSuccess={this.handleSuccess} />
|
handleSuccess={this.handleSuccess} />
|
||||||
<AclButton
|
<AclButton
|
||||||
availableAcls={availableAcls}
|
availableAcls={availableAcls}
|
||||||
action="consign"
|
action="consign"
|
||||||
editions={selectedEditions}
|
editions={selectedEditions}
|
||||||
currentUser={this.state.user.currentUser}
|
currentUser={this.state.currentUser}
|
||||||
handleSuccess={this.handleSuccess} />
|
handleSuccess={this.handleSuccess} />
|
||||||
<AclButton
|
<AclButton
|
||||||
availableAcls={availableAcls}
|
availableAcls={availableAcls}
|
||||||
action="loan"
|
action="loan"
|
||||||
editions={selectedEditions}
|
editions={selectedEditions}
|
||||||
currentUser={this.state.user.currentUser}
|
currentUser={this.state.currentUser}
|
||||||
handleSuccess={this.handleSuccess} />
|
handleSuccess={this.handleSuccess} />
|
||||||
<AclButton
|
<AclButton
|
||||||
availableAcls={availableAcls}
|
availableAcls={availableAcls}
|
||||||
action="share"
|
action="share"
|
||||||
editions={selectedEditions}
|
editions={selectedEditions}
|
||||||
currentUser={this.state.user.currentUser}
|
currentUser={this.state.currentUser}
|
||||||
handleSuccess={this.handleSuccess} />
|
handleSuccess={this.handleSuccess} />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
import React from 'react';
|
||||||
import alt from '../alt';
|
import alt from '../alt';
|
||||||
|
|
||||||
import PieceListActions from '../actions/piece_list_actions';
|
import PieceListActions from '../actions/piece_list_actions';
|
||||||
|
|
||||||
|
|
||||||
@ -53,6 +55,10 @@ class PieceListStore {
|
|||||||
this.pieceListCount = pieceListCount;
|
this.pieceListCount = pieceListCount;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Pagination - Known Issue:
|
||||||
|
* #########################
|
||||||
|
*
|
||||||
|
*
|
||||||
* The piece list store currently stores the open/close state of a piece list item.
|
* 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
|
* 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
|
* We did not implement this, as we're going to add pagination to pieceList at some
|
||||||
* point anyway. Then, this problem is automatically resolved.
|
* point anyway. Then, this problem is automatically resolved.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
this.pieceList = pieceList;
|
this.pieceList = pieceList;
|
||||||
}
|
}
|
||||||
|
@ -61,3 +61,33 @@ export function formatText() {
|
|||||||
return val;
|
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;
|
||||||
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user