import React, { Component } from 'react' import { Switch } from 'react-native' import PropTypes from 'prop-types' import { cervix as labels } from '../../../i18n/en/cycle-day' import SelectTabGroup from '../select-tab-group' import SymptomSection from './symptom-section' import SymptomView from './symptom-view' import { getLabelsList } from '../../helpers/labels' import { saveSymptom } from '../../../db' class Cervix extends Component { static propTypes = { cycleDay: PropTypes.object, handleBackButtonPress: PropTypes.func, date: PropTypes.string.isRequired, } constructor(props) { super(props) const symptom = 'cervix' const { cycleDay } = props const defaultSymptomData = {} const symptomData = cycleDay && cycleDay[symptom] ? cycleDay[symptom] : defaultSymptomData this.state = { ...symptomData } this.cervixOpeningRadioProps = getLabelsList(labels.opening.categories) this.cervixFirmnessRadioProps = getLabelsList(labels.firmness.categories) this.cervixPositionRadioProps = getLabelsList(labels.position.categories) this.symptom = symptom } autoSave = () => { const { date } = this.props const { opening, firmness, position, exclude } = this.state const valuesToSave = { opening, firmness, position, exclude: Boolean(exclude) } const nothingEntered = ['opening', 'firmness', 'position'].every( val => typeof this.state[val] !== 'number') saveSymptom(this.symptom, date, nothingEntered ? null : valuesToSave) } componentDidUpdate() { this.autoSave() } render() { // TODO saving this info for notice when leaving incomplete data // const mandatoryNotCompleted = typeof this.state.opening != 'number' || typeof this.state.firmness != 'number' return ( this.setState({ opening: val })} /> this.setState({ firmness: val })} /> this.setState({ position: val })} /> { this.setState({ exclude: val }) }} value={this.state.exclude} /> ) } } export default Cervix