Skip to content
Snippets Groups Projects
app.js 2.21 KiB
Newer Older
import React, { Component } from 'react'
import { View, BackHandler } from 'react-native'
import PropTypes from 'prop-types'

import { connect } from 'react-redux'

import { getDate } from '../slices/date'
import { getNavigation, navigate, goBack } from '../slices/navigation'
import Header from './header'
import Menu from './menu'
Sofiya Tepikin's avatar
Sofiya Tepikin committed
import { viewsList } from './views'
import { isSymptomView, isSettingsView } from './pages'
import { headerTitles } from '../i18n/en/labels'
import setupNotifications from '../lib/notifications'
Sofiya Tepikin's avatar
Sofiya Tepikin committed
import { getCycleDay } from '../db'
class App extends Component {

  static propTypes = {
    date: PropTypes.string,
    navigation: PropTypes.object.isRequired,
    navigate: PropTypes.func,
    goBack: PropTypes.func,
  constructor(props) {
    super(props)

    this.backHandler = BackHandler.addEventListener(
      'hardwareBackPress',
Sofiya Tepikin's avatar
Sofiya Tepikin committed
      props.goBack
    )

    setupNotifications(this.props.navigate)
  }

  componentWillUnmount() {
    this.backHandler.remove()
Sofiya Tepikin's avatar
Sofiya Tepikin committed
  render() {
    const { date, navigation, goBack } = this.props
    const { currentPage } = navigation
Sofiya Tepikin's avatar
Sofiya Tepikin committed
    if (!currentPage) {
      return false
    }

    const Page = viewsList[currentPage]
    const title = headerTitles[currentPage]
    const isSymptomEditView = isSymptomView(currentPage)
    const isSettingsSubView = isSettingsView(currentPage)
    const isCycleDayView = currentPage === 'CycleDay'
Sofiya Tepikin's avatar
Sofiya Tepikin committed
    const headerProps = {
      title,
      handleBack: isSettingsSubView ? goBack : null,
    }

    const pageProps = {
      cycleDay: date && getCycleDay(date),
Sofiya Tepikin's avatar
Sofiya Tepikin committed
      date,
      handleBackButtonPress: goBack,
    }

    return (
Sofiya Tepikin's avatar
Sofiya Tepikin committed
      <View style={{ flex: 1 }}>
Sofiya Tepikin's avatar
Sofiya Tepikin committed
        {
          !isSymptomEditView &&
          !isCycleDayView &&
          <Header { ...headerProps } />
Sofiya Tepikin's avatar
Sofiya Tepikin committed
        <Page { ...pageProps } />
        { !isSymptomEditView && <Menu /> }
      </View>
    )

const mapStateToProps = (state) => {
  return({
    date: getDate(state),
    navigation: getNavigation(state)
  })
}

const mapDispatchToProps = (dispatch) => {
  return({
    navigate: (page) => dispatch(navigate(page)),
    goBack: () => dispatch(goBack()),
  })
}

export default connect(
  mapStateToProps,
  mapDispatchToProps