@talend/react-cmf
Version:
A framework built on top of best react libraries
76 lines (73 loc) • 2.23 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _immutable = _interopRequireDefault(require("immutable"));
var _lodash = require("lodash");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/**
* getState read localStorage and create a initilState for redux
* @param {string} key the localStorage key where to read
* @return {Object} initialState for redux
*/
function getState(key) {
let source = localStorage[key];
if (!source) {
return {};
}
source = JSON.parse(source);
if (source.cmf) {
if (source.cmf.components) {
source.cmf.components = _immutable.default.fromJS(source.cmf.components);
}
if (source.cmf.collections) {
source.cmf.collections = _immutable.default.fromJS(source.cmf.collections);
}
}
return source;
}
/**
* getStoreCallback read localStorage and create a initialState for redux
* @param {string} key the localStorage key where to read
* @param {Array} paths the list of paths (Array) to serialize in localStorage
* @return {Object} initialState for redux
* @example
const storeCallback = getStoreCallback('myappV1', [
['cmf', 'components', 'Container(List)', 'foo'],
['cmf', 'components', 'Container(SidePanel)'],
]);
cmf.bootstrap({
...
storeCallback,
});
*/
function getStoreCallback(key, paths) {
return store => {
window.addEventListener('beforeunload', () => {
const toKeep = {};
const state = store.getState();
paths.forEach(path => {
if (path.length > 2) {
if (path[1] === 'components') {
const value = state.cmf.components.getIn(path.slice(2));
if (value) {
(0, _lodash.set)(toKeep, path, value.toJS());
}
} else if (path[1] === 'collections') {
const value = state.cmf.collections.getIn(path.slice(2));
if (value) {
(0, _lodash.set)(toKeep, path, value.toJS());
}
}
}
});
localStorage.setItem(key, JSON.stringify(toKeep));
});
};
}
var _default = exports.default = {
getState,
getStoreCallback
};
//# sourceMappingURL=localStorage.js.map
;