From 8cad357e607d4b997ba33cc3961ce3729d6d7847 Mon Sep 17 00:00:00 2001 From: Sofiya Tepikin Date: Wed, 5 Feb 2020 19:50:40 +0100 Subject: [PATCH] Fixes navigation logic --- components/app.js | 47 ++++++++++++++++++++++---------------------- components/pages.js | 25 ++++------------------- components/views.js | 19 ++++++++++++++++++ slices/navigation.js | 30 ++++++++++++++++++++-------- 4 files changed, 69 insertions(+), 52 deletions(-) create mode 100644 components/views.js diff --git a/components/app.js b/components/app.js index 3f6f2cd..a351513 100644 --- a/components/app.js +++ b/components/app.js @@ -10,11 +10,12 @@ import { getNavigation, navigate, goBack } from '../slices/navigation' import Header from './header' import Menu from './menu' -import { viewsList, isSymptomView, isSettingsView } from './pages' +import { viewsList } from './views' +import { isSymptomView, isSettingsView } from './pages' import { headerTitles } from '../i18n/en/labels' import setupNotifications from '../lib/notifications' -import { closeDb, getCycleDay } from '../db' +import { getCycleDay } from '../db' class App extends Component { @@ -35,7 +36,7 @@ class App extends Component { this.backHandler = BackHandler.addEventListener( 'hardwareBackPress', - this.handleBackButtonPress + props.goBack ) setupNotifications(this.props.navigate) @@ -45,21 +46,15 @@ class App extends Component { this.backHandler.remove() } - handleBackButtonPress = () => { - const { currentPage } = this.props.navigation + render() { + const { date, navigation, goBack } = this.props + const { currentPage } = navigation - if (currentPage === 'Home') { - closeDb() + if (!currentPage) { return false } - this.props.goBack() - return true - } - - render() { const { cycleDay } = this.state - const { currentPage } = this.props.navigation const Page = viewsList[currentPage] const title = headerTitles[currentPage] @@ -68,20 +63,26 @@ class App extends Component { const isSettingsSubView = isSettingsView(currentPage) const isCycleDayView = currentPage === 'CycleDay' + const headerProps = { + title, + handleBack: isSettingsSubView ? goBack : null, + } + + const pageProps = { + cycleDay, + date, + handleBackButtonPress: goBack, + } + return ( - { !isSymptomEditView && !isCycleDayView && -
+ { + !isSymptomEditView && + !isCycleDayView && +
} - + { !isSymptomEditView && } diff --git a/components/pages.js b/components/pages.js index 26a281d..943a6a9 100644 --- a/components/pages.js +++ b/components/pages.js @@ -1,25 +1,13 @@ -import Home from './home' -import Calendar from './calendar' -import CycleDay from './cycle-day/cycle-day-overview' import symptomViews from './cycle-day/symptoms' -import Chart from './chart/chart' -import SettingsMenu from './settings/settings-menu' import settingsViews from './settings' -import Stats from './stats' import settingsLabels from '../i18n/en/settings' const labels = settingsLabels.menuTitles -export const viewsList = { - Home, - Calendar, - CycleDay, - Chart, - SettingsMenu, - ...settingsViews, - Stats, - ...symptomViews -} +const symptomsPages = Object.keys(symptomViews).map(symptomView => ({ + component: symptomView, + parent: 'CycleDay', +})) export const isSymptomView = (page) => Object.keys(symptomViews).includes(page) @@ -27,11 +15,6 @@ export const isSymptomView = export const isSettingsView = (page) => Object.keys(settingsViews).includes(page) -const symptomsPages = Object.keys(symptomViews).map(symptomView => ({ - component: symptomView, - parent: 'CycleDay', -})) - export const pages = [ { component: 'Home', diff --git a/components/views.js b/components/views.js new file mode 100644 index 0000000..1cb8545 --- /dev/null +++ b/components/views.js @@ -0,0 +1,19 @@ +import Home from './home' +import Calendar from './calendar' +import CycleDay from './cycle-day/cycle-day-overview' +import symptomViews from './cycle-day/symptoms' +import Chart from './chart/chart' +import SettingsMenu from './settings/settings-menu' +import settingsViews from './settings' +import Stats from './stats' + +export const viewsList = { + Home, + Calendar, + CycleDay, + Chart, + SettingsMenu, + ...settingsViews, + Stats, + ...symptomViews +} diff --git a/slices/navigation.js b/slices/navigation.js index 909610c..13674fd 100644 --- a/slices/navigation.js +++ b/slices/navigation.js @@ -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 } } }