Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Settings saved in local storage #163

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/containers/SettingsDrawer/SettingsDrawer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import localStore from '../../utils/localStore';

import bindMethods from 'yaab';

Expand All @@ -27,6 +28,7 @@ const mapDispatchToProps = dispatch => ({
dispatch(setTrust(trustLevel));
dispatch(clearTransactions());
dispatch(getAllTransactions());
localStore.setItems({ walletAddress, trustLevel });
},
});

Expand Down
11 changes: 8 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Provider } from 'react-redux';
import store from './store';
import DevTools from './containers/DevTools/DevTools';
import env from './utils/environment';
import localStore from './utils/localStore';

import {
getAllTransactions,
Expand All @@ -19,9 +20,13 @@ import {
} from './actions';

// Default settings
store.dispatch(addWallet(env.REACT_APP_WALLET_ADDRESS));
store.dispatch(addToken(env.REACT_APP_CONTRACT_ADDRESS));
store.dispatch(setTrust(6));
store.dispatch(
addWallet(localStore.getItem('walletAddress') || env.REACT_APP_WALLET_ADDRESS)
);
store.dispatch(
addToken(localStore.getItem('tokenAddress') || env.REACT_APP_CONTRACT_ADDRESS)
);
store.dispatch(setTrust(localStore.getItem('trustLevel') || 6));

// Preload token data for contract
store.dispatch(addTokenMetadata(env.REACT_APP_CONTRACT_ADDRESS));
Expand Down
20 changes: 20 additions & 0 deletions src/utils/localStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class LocalStore {
getItem(key) {
let value = localStorage.getItem(key);
try {
return JSON.parse(value);
} catch (e) {
return value;
}
}
setItem(key, value) {
localStorage.setItem(key, JSON.stringify(value));
}
setItems(object) {
for (let key in object) {
this.setItem(key, object[key]);
}
}
}

export default new LocalStore();