diff --git a/components/app.js b/components/app.js index 256c18f..3f6f2cd 100644 --- a/components/app.js +++ b/components/app.js @@ -6,11 +6,11 @@ import { LocalDate } from 'js-joda' import { connect } from 'react-redux' import { getDate, setDate } from '../slices/date' -import { getNavigation, navigate } from '../slices/navigation' +import { getNavigation, navigate, goBack } from '../slices/navigation' import Header from './header' import Menu from './menu' -import { viewsList, isSymptomView, isSettingsView, pages } from './pages' +import { viewsList, isSymptomView, isSettingsView } from './pages' import { headerTitles } from '../i18n/en/labels' import setupNotifications from '../lib/notifications' @@ -53,9 +53,7 @@ class App extends Component { return false } - const page = pages.find(item => item.component === currentPage) - this.props.navigate(page.parent) - + this.props.goBack() return true } @@ -102,6 +100,7 @@ const mapDispatchToProps = (dispatch) => { return({ setDate: (date) => dispatch(setDate(date)), navigate: (page) => dispatch(navigate(page)), + goBack: () => dispatch(goBack()), }) } diff --git a/components/pages.js b/components/pages.js index 3558ea8..26a281d 100644 --- a/components/pages.js +++ b/components/pages.js @@ -28,7 +28,8 @@ export const isSettingsView = (page) => Object.keys(settingsViews).includes(page) const symptomsPages = Object.keys(symptomViews).map(symptomView => ({ - component: symptomView, parent: 'CycleDay', + component: symptomView, + parent: 'CycleDay', })) export const pages = [ diff --git a/slices/navigation.js b/slices/navigation.js index bd53dd3..909610c 100644 --- a/slices/navigation.js +++ b/slices/navigation.js @@ -4,12 +4,23 @@ const navigationSlice = createSlice({ slice: 'navigation', initialState: { currentPage: 'Home', + history: [], }, reducers: { navigate: (state, action) => { + const { history, currentPage } = state return { + history: history.concat(currentPage), currentPage: action.payload, } + }, + goBack: (state) => { + const { history } = state + const lastIndex = history.length - 1 + return { + currentPage: history[lastIndex], + history: history.slice(0, lastIndex), + } } } }) @@ -17,7 +28,7 @@ const navigationSlice = createSlice({ // Extract the action creators object and the reducer const { actions, reducer, selectors } = navigationSlice // Extract and export each action creator by name -export const { navigate } = actions +export const { navigate, goBack } = actions export const { getNavigation } = selectors