@bigfishtv/cockpit
Version:
73 lines (56 loc) • 3.38 kB
JavaScript
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
/**
* @module Decorators/persistState
*/
import React, { Component } from 'react';
var localStorageEnabled = typeof Storage !== 'undefined';
/**
* Decorator that exposes persists state by exposing a persist prop
* @param {Object} defaultState
* @param {Function} getPersistKey - function for getting which localStorage key to use
* @return {component} returns wrapped component
*/
export default function wrapComponentWithPersistentState() {
var defaultState = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var getPersistKey = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {
return false;
};
return function (WrappedComponent) {
return function (_Component) {
_inherits(PersistentStateDecorator, _Component);
function PersistentStateDecorator(props) {
_classCallCheck(this, PersistentStateDecorator);
var _this = _possibleConstructorReturn(this, _Component.call(this, props));
_this.persist = function (state) {
_this.setState(state, function () {
_this.saveToLocalStorage();
});
};
_this.persistKey = props.persistKey || getPersistKey(props);
_this.state = _this.restoreFromLocalStorage(defaultState);
return _this;
}
PersistentStateDecorator.prototype.saveToLocalStorage = function saveToLocalStorage() {
if (!localStorageEnabled || !this.persistKey) return;
localStorage.setItem(this.persistKey, JSON.stringify(this.state));
};
PersistentStateDecorator.prototype.restoreFromLocalStorage = function restoreFromLocalStorage() {
var defaultState = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
if (!localStorageEnabled || !this.persistKey) return defaultState;
if (localStorage[this.persistKey]) {
return JSON.parse(localStorage.getItem(this.persistKey));
} else {
localStorage.setItem(this.persistKey, JSON.stringify(defaultState));
return defaultState;
}
};
PersistentStateDecorator.prototype.render = function render() {
return React.createElement(WrappedComponent, _extends({ persist: this.persist }, this.props, this.state));
};
return PersistentStateDecorator;
}(Component);
};
}