Skip to content
Snippets Groups Projects
sex.js 2.6 KiB
Newer Older
import React from 'react'
Bl00dyMarie's avatar
Bl00dyMarie committed
import {
  TextInput,
  ScrollView
Bl00dyMarie's avatar
Bl00dyMarie committed
} from 'react-native'
import { connect } from 'react-redux'

import { getDate } from '../../../slices/date'

Bl00dyMarie's avatar
Bl00dyMarie committed
import styles from '../../../styles'
import { sex as sexLabels, contraceptives as contraceptivesLabels } from '../../../i18n/en/cycle-day'
import { shared as sharedLabels } from '../../../i18n/en/labels'
import SelectBoxGroup from '../select-box-group'
import SymptomSection from './symptom-section'
import SymptomView from './symptom-view'
class Sex extends SymptomView {
Bl00dyMarie's avatar
Bl00dyMarie committed
  constructor(props) {
    super(props)
    const cycleDay = props.cycleDay
    if (cycleDay && cycleDay.sex) {
      this.state = Object.assign({}, cycleDay.sex)
    } else {
      this.state = {}
Bl00dyMarie's avatar
Bl00dyMarie committed
    }
    // We make sure other is always true when there is a note,
    // e.g. when import is messed up.
    if (this.state.note) this.state.other = true
Bl00dyMarie's avatar
Bl00dyMarie committed
  }

  symptomName = "sex"

  autoSave = () => {
    const nothingEntered = Object.values(this.state).every(val => !val)
    if (nothingEntered) {
      this.deleteSymptomEntry()
      return
    }

    const copyOfState = Object.assign({}, this.state)
    if (!copyOfState.other) {
      copyOfState.note = null
    }
    this.saveSymptomEntry(copyOfState)
  toggleState = (key) => {
    const curr = this.state[key]
    this.setState({[key]: !curr})
    if (key === 'other' && !curr) {
      this.setState({focusTextArea: true})
    }
  renderContent() {
Bl00dyMarie's avatar
Bl00dyMarie committed
    return (
      <ScrollView style={styles.page}>
        <SymptomSection
          header={sexLabels.header}
          explainer={sexLabels.explainer}
        >
          <SelectBoxGroup
            labels={sexLabels.categories}
            onSelect={this.toggleState}
            optionsState={this.state}
          />
        </SymptomSection>
        <SymptomSection
          header={contraceptivesLabels.header}
          explainer={contraceptivesLabels.explainer}
        >
          <SelectBoxGroup
            labels={contraceptivesLabels.categories}
            onSelect={this.toggleState}
            optionsState={this.state}
          />
        </SymptomSection>
        {this.state.other &&
            <TextInput
              autoFocus={this.state.focusTextArea}
              multiline={true}
              placeholder={sharedLabels.enter}
              value={this.state.note}
              onChangeText={(val) => {
                this.setState({ note: val })
              }}
            />
Bl00dyMarie's avatar
Bl00dyMarie committed
    )
  }

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

export default connect(
  mapStateToProps,
  null
)(Sex)