Commit 942d353a authored by Wietse Kuipers's avatar Wietse Kuipers
Browse files

Small refactor to improve readability

parent 64b4b83a
import React, { Component } from 'react';
import { BackAndroid, AsyncStorage } from 'react-native';
import { AsyncStorage } from 'react-native';
import { createStore, applyMiddleware, combineReducers } from 'redux';
import { Provider } from 'react-redux';
import thunk from 'redux-thunk';
import * as reducers from './reducers';
import ReduxNavigator from './components/navigator';
import { back } from './actions/navigation';
import { initLogin } from './actions/login';
const createStoreWithMiddleware = applyMiddleware(thunk)(createStore);
......@@ -15,27 +14,18 @@ const store = createStoreWithMiddleware(reducer);
const USERNAMEKEY = '@MyStore:username';
const TOKENKEY = '@MyStore:token';
BackAndroid.addEventListener('hardwareBackPress', () => {
if (store.getState().navigation.previousScenes.length === 0) {
return false;
}
store.dispatch(back());
return true;
});
const pairsToObject = (obj, pair) => {
const obj2 = { ...obj };
obj2[pair[0]] = pair[1];
return obj2;
};
class Main extends Component {
componentDidMount() {
AsyncStorage.multiGet([USERNAMEKEY, TOKENKEY])
.then(
(result) => {
// Transform [key, value] pairs into object, for easier access
const values = result.reduce((obj, pair) => {
const obj2 = { ...obj };
obj2[pair[0]] = pair[1];
return obj2;
}, {});
const values = result.reduce(pairsToObject, {});
const username = values[USERNAMEKEY];
const token = values[TOKENKEY];
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment