mirror of
https://github.com/elyby/accounts-frontend.git
synced 2024-12-27 23:40:28 +05:30
#316: add localStorage wrapper with feature detection
This commit is contained in:
parent
5d0951b218
commit
dcf95410d7
@ -113,6 +113,10 @@
|
||||
"no-undef-init": "error",
|
||||
"no-undef": "error",
|
||||
"no-use-before-define": ["warn", "nofunc"],
|
||||
"no-restricted-globals": ["error",
|
||||
"localStorage", "sessionStorage", // we have our own localStorage module
|
||||
"event"
|
||||
],
|
||||
|
||||
// CommonJS
|
||||
"no-mixed-requires": "warn",
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { routeActions } from 'react-router-redux';
|
||||
|
||||
import logger from 'services/logger';
|
||||
import localStorage from 'services/localStorage';
|
||||
import loader from 'services/loader';
|
||||
import history from 'services/history';
|
||||
import { updateUser, acceptRules as userAcceptRules } from 'components/user/actions';
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { routeActions } from 'react-router-redux';
|
||||
|
||||
import logger from 'services/logger';
|
||||
import localStorage from 'services/localStorage';
|
||||
|
||||
import RegisterState from './RegisterState';
|
||||
import LoginState from './LoginState';
|
||||
|
40
src/services/localStorage.js
Normal file
40
src/services/localStorage.js
Normal file
@ -0,0 +1,40 @@
|
||||
import logger from 'services/logger';
|
||||
|
||||
let _hasStorage = false;
|
||||
|
||||
try {
|
||||
const test = 'test';
|
||||
window.localStorage.setItem(test, test);
|
||||
window.localStorage.removeItem(test);
|
||||
|
||||
_hasStorage = true;
|
||||
} catch (err) {
|
||||
// bad luck, no storage available
|
||||
logger.info('No storage available'); // log for statistic purposes
|
||||
}
|
||||
|
||||
export function hasStorage() {
|
||||
return _hasStorage;
|
||||
}
|
||||
|
||||
function DummyStorage() {
|
||||
return {
|
||||
getItem(key) {
|
||||
return this[key] || null;
|
||||
},
|
||||
|
||||
setItem(key, value) {
|
||||
this[key] = value;
|
||||
},
|
||||
|
||||
removeItem(key) {
|
||||
Reflect.deleteProperty(this, key);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export const localStorage = _hasStorage ? window.localStorage : new DummyStorage();
|
||||
|
||||
export const sessionStorage = _hasStorage ? window.sessionStorage : new DummyStorage();
|
||||
|
||||
export default localStorage;
|
Loading…
Reference in New Issue
Block a user