Fixes navigation logic

This commit is contained in:
Sofiya Tepikin
2020-02-05 19:50:40 +01:00
parent 026a9c17c3
commit 8cad357e60
4 changed files with 69 additions and 52 deletions
+22 -8
View File
@@ -1,25 +1,39 @@
import { createSlice } from 'redux-starter-kit'
import { pages, isSymptomView } from '../components/pages'
import { closeDb } from '../db'
import { BackHandler } from 'react-native'
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,
previousPage: state.currentPage,
}
},
goBack: (state) => {
const { history } = state
const lastIndex = history.length - 1
goBack: ({ currentPage, previousPage }) => {
if (currentPage === 'Home') {
closeDb()
BackHandler.exitApp()
return false
}
if (currentPage === 'CycleDay' || isSymptomView(currentPage)) {
if (previousPage) {
return {
currentPage: previousPage
}
}
}
const page = pages.find(p => p.component === currentPage)
return {
currentPage: history[lastIndex],
history: history.slice(0, lastIndex),
currentPage: page.parent
}
}
}