1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-22 17:33:14 +01:00

fix bug in mergeOptions method

This commit is contained in:
Tim Daubenschütz 2015-07-01 16:19:02 +02:00
parent 787f25ddde
commit 740f506ced
2 changed files with 40 additions and 6 deletions

View File

@ -9,6 +9,9 @@ import Router from 'react-router';
import LicenseActions from '../actions/license_actions';
import LicenseStore from '../stores/license_store';
import PieceListStore from '../stores/piece_list_store';
import PieceListActions from '../actions/piece_list_actions';
import GlobalNotificationModel from '../models/global_notification_model';
import GlobalNotificationActions from '../actions/global_notification_actions';
@ -29,6 +32,7 @@ let RegisterPiece = React.createClass( {
getInitialState(){
return mergeOptions(
LicenseStore.getState(),
PieceListStore.getState(),
{
digitalWorkKey: null,
uploadStatus: false,
@ -39,10 +43,12 @@ let RegisterPiece = React.createClass( {
componentDidMount() {
LicenseActions.fetchLicense();
LicenseStore.listen(this.onChange);
PieceListStore.listen(this.onChange);
},
componentWillUnmount() {
LicenseStore.unlisten(this.onChange);
PieceListStore.unlisten(this.onChange);
},
onChange(state) {
@ -52,6 +58,12 @@ let RegisterPiece = React.createClass( {
handleSuccess(){
let notification = new GlobalNotificationModel('Login successsful', 'success', 10000);
GlobalNotificationActions.appendGlobalNotification(notification);
// once the user was able to register a piece successfully, we need to make sure to keep
// the piece list up to date
//console.log(this.state);
PieceListActions.fetchPieceList(this.state.page, this.state.pageSize, this.state.searchTerm, this.state.orderBy, this.state.orderAsc);
this.transitionTo('pieces');
},

View File

@ -64,6 +64,26 @@ export function formatText() {
});
}
/*
Checks a list of objects for key duplicates and returns a boolean
*/
function _doesObjectListHaveDuplicates(l) {
let mergedList = [];
l = l.map((obj) => Object.keys(obj));
// Taken from: http://stackoverflow.com/a/10865042
// How to flatten an array of arrays in javascript.
// If two objects contain the same key, then these two keys
// will actually be represented in the merged array
mergedList = mergedList.concat.apply(mergedList, l);
// Taken from: http://stackoverflow.com/a/7376645/1263876
// By casting the array to a set, and then checking if the size of the array
// shrunk in the process of casting, we can check if there were any duplicates
return (new Set(mergedList)).size !== mergedList.length;
}
/**
* Takes a list of object and merges their keys to one object.
* Uses mergeOptions for two objects.
@ -71,6 +91,12 @@ export function formatText() {
* @return {[type]} [description]
*/
export function mergeOptions(...l) {
// If the objects submitted in the list have duplicates,in their key names,
// abort the merge and tell the function's user to check his objects.
if(_doesObjectListHaveDuplicates(l)) {
throw new Error('The objects you submitted for merging have duplicates. Merge aborted.');
}
let newObj = {};
for(let i = 1; i < l.length; i++) {
@ -86,18 +112,14 @@ export function mergeOptions(...l) {
* @returns obj3 a new object based on obj1 and obj2
* Taken from: http://stackoverflow.com/a/171256/1263876
*/
function _mergeOptions(obj1, obj2){
function _mergeOptions(obj1, obj2) {
let obj3 = {};
for (let attrname in obj1) {
obj3[attrname] = obj1[attrname];
}
for (let attrname in obj2) {
if(attrname in obj3) {
throw Error('Overwrite Conflict: You\'re merging two objects with the same keys: ' + attrname);
} else {
obj3[attrname] = obj2[attrname];
}
obj3[attrname] = obj2[attrname];
}
return obj3;
}