-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[redux] move some redux utils to shared file (#1164)
* move some redux utils to shared file so they can be used in the new export view too * enhancer is a func now
- Loading branch information
Alanna Scott
committed
Sep 22, 2016
1 parent
b587576
commit 2f2ed22
Showing
3 changed files
with
80 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import shortid from 'shortid'; | ||
import { compose } from 'redux'; | ||
import persistState from 'redux-localstorage'; | ||
|
||
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) { | ||
// Finds an item in an array in the state and replaces it with a | ||
// new object with an altered property | ||
const idKey = 'id'; | ||
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 enhancer() { | ||
let enhancerWithPersistState = compose(persistState()); | ||
if (process.env.NODE_ENV === 'dev') { | ||
enhancerWithPersistState = compose( | ||
persistState(), window.devToolsExtension && window.devToolsExtension() | ||
); | ||
} | ||
return enhancerWithPersistState; | ||
} |