Files
superset2/superset/assets/javascripts/reduxUtils.js
Maxime Beauchemin 969ff0ce39 Fix deep equality logic (#4730)
* Fix deepequality logic

Zeroed-in on this while a Deck Scatter Plot chart was prompting for
refresh on-load.

I'm pretty sure using JSON.stringify as a proxy for deep equality is
wrong.

Not sure how to handle yarn.lock changes here. The changes on yarn.lock
are the result of "only" running `yarn add deep-equal`

* Adressing comments
2018-04-02 20:54:57 -07:00

101 lines
2.8 KiB
JavaScript

import shortid from 'shortid';
import { compose } from 'redux';
import persistState from 'redux-localstorage';
import equals from 'deep-equal';
export function addToObject(state, arrKey, obj) {
const newObject = Object.assign({}, state[arrKey]);
const copiedObject = Object.assign({}, obj);
if (!copiedObject.id) {
copiedObject.id = shortid.generate();
}
newObject[copiedObject.id] = copiedObject;
return Object.assign({}, state, { [arrKey]: newObject });
}
export function alterInObject(state, arrKey, obj, alterations) {
const newObject = Object.assign({}, state[arrKey]);
newObject[obj.id] = Object.assign({}, newObject[obj.id], alterations);
return Object.assign({}, state, { [arrKey]: newObject });
}
export function alterInArr(state, arrKey, obj, alterations, idKey = 'id') {
// Finds an item in an array in the state and replaces it with a
// new object with an altered property
const newArr = [];
state[arrKey].forEach((arrItem) => {
if (obj[idKey] === arrItem[idKey]) {
newArr.push(Object.assign({}, arrItem, alterations));
} else {
newArr.push(arrItem);
}
});
return Object.assign({}, state, { [arrKey]: newArr });
}
export function removeFromArr(state, arrKey, obj, idKey = 'id') {
const newArr = [];
state[arrKey].forEach((arrItem) => {
if (!(obj[idKey] === arrItem[idKey])) {
newArr.push(arrItem);
}
});
return Object.assign({}, state, { [arrKey]: newArr });
}
export function getFromArr(arr, id) {
let obj;
arr.forEach((o) => {
if (o.id === id) {
obj = o;
}
});
return obj;
}
export function addToArr(state, arrKey, obj) {
const newObj = Object.assign({}, obj);
if (!newObj.id) {
newObj.id = shortid.generate();
}
const newState = {};
newState[arrKey] = [...state[arrKey], newObj];
return Object.assign({}, state, newState);
}
export function initEnhancer(persist = true) {
let enhancer = persist ? compose(persistState()) : compose();
if (process.env.NODE_ENV === 'dev') {
/* eslint-disable no-underscore-dangle */
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
/* eslint-enable */
enhancer = persist ? composeEnhancers(persistState()) : composeEnhancers();
}
return enhancer;
}
export function areArraysShallowEqual(arr1, arr2) {
// returns whether 2 arrays are shallow equal
// used in shouldComponentUpdate when denormalizing arrays
// where the array object is different every time, but the content might
// be the same
if (!arr1 || !arr2) {
return false;
}
if (arr1.length !== arr2.length) {
return false;
}
const length = arr1.length;
for (let i = 0; i < length; i++) {
if (arr1[i] !== arr2[i]) {
return false;
}
}
return true;
}
export function areObjectsEqual(obj1, obj2) {
return equals(obj1, obj2, true);
}