Commit efeec8fa authored by Wietse Kuipers's avatar Wietse Kuipers
Browse files

Add back button functionality on Android

parent 1268db9d
export const LOGIN = 'LOGIN';
export const NAVIGATE = 'NAVIGATE';
export const BACK = 'BACK';
......@@ -6,3 +6,9 @@ export function navigate(scene) {
scene,
};
}
export function back() {
return {
type: types.BACK,
};
}
import React from 'react';
import { BackAndroid } 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';
const createStoreWithMiddleware = applyMiddleware(thunk)(createStore);
const reducer = combineReducers(reducers);
const store = createStoreWithMiddleware(reducer);
BackAndroid.addEventListener('hardwareBackPress', () => {
if (store.getState().navigation.previousScenes.length === 0) {
return false;
}
store.dispatch(back());
return true;
});
const Main = () =>
<Provider store={store}>
<ReduxNavigator />
......
......@@ -14,7 +14,6 @@ const mapDispatchToProps = dispatch => ({
navigate: scene => dispatch(actions.navigate(scene)),
});
const ReduxNavigator = (props) => {
const currentScene = props.currentScene;
switch (currentScene) {
......@@ -31,5 +30,4 @@ ReduxNavigator.propTypes = {
currentScene: React.PropTypes.string.isRequired,
};
export default connect(mapStateToProps, mapDispatchToProps)(ReduxNavigator);
import * as types from '../actions/actionTypes';
const initialState = {
previousScenes: [],
currentScene: 'login',
};
export default function navigate(state = initialState, action = {}) {
const { currentScene, previousScenes } = state;
switch (action.type) {
case types.LOGIN:
if (action.success) {
return { ...state, currentScene: 'welcome' };
return {
previousScenes: [
...previousScenes,
currentScene,
],
currentScene: 'welcome',
};
}
return { ...state };
case types.BACK: {
const scene = previousScenes.pop();
return {
previousScenes,
currentScene: scene,
};
}
default:
return { ...state };
}
......
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