Skip to content
Snippets Groups Projects
sex.js 2.78 KiB
Newer Older
import React, { Component } from 'react'
import { TextInput } from 'react-native'
import PropTypes from 'prop-types'
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'
import { saveSymptom } from '../../../db'

class Sex extends Component {

  static propTypes = {
    cycleDay: PropTypes.object,
    date: PropTypes.string.isRequired,
  }

Bl00dyMarie's avatar
Bl00dyMarie committed
  constructor(props) {
    super(props)
    const symptom = 'sex'
    const { cycleDay } = props

    const defaultSymptomData = {}

    const symptomData =
      cycleDay && cycleDay[symptom] ? cycleDay[symptom] : defaultSymptomData

    this.state = { ...symptomData }

    // 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

  autoSave = () => {
    const { date } = this.props
    const valuesToSave = Object.assign({}, this.state)
    if (!valuesToSave.other) {
      valuesToSave.note = null
    const nothingEntered = Object.values(this.state).every(val => !val)
    saveSymptom(this.symptom, date, nothingEntered ? null : valuesToSave)
  }

  componentDidUpdate() {
    this.autoSave()
  toggleState = (key) => {
    const curr = this.state[key]
    this.setState({[key]: !curr})
    if (key === 'other'){
      if (curr){
        this.setState({note: ""})
      } else {
        this.setState({focusTextArea: true})
      }
Bl00dyMarie's avatar
Bl00dyMarie committed
    return (
      <SymptomView
        symptom={this.symptom}
        values={this.state}
        date={this.props.date}
      >
        <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
    )
  }