diff --git a/components/cycle-day/symptoms/bleeding.js b/components/cycle-day/symptoms/bleeding.js index 890df1b..ddb5795 100644 --- a/components/cycle-day/symptoms/bleeding.js +++ b/components/cycle-day/symptoms/bleeding.js @@ -5,7 +5,6 @@ import { ScrollView } from 'react-native' import styles from '../../../styles' -import { saveSymptom } from '../../../db' import { bleeding } from '../../../i18n/en/cycle-day' import ActionButtonFooter from './action-button-footer' import SelectTabGroup from '../select-tab-group' @@ -23,8 +22,14 @@ export default class Bleeding extends SymptomView { } } - save() { - saveSymptom('bleeding', this.props.date, { + symptomName = 'bleeding' + + onBackButtonPress() { + if (typeof this.state.currentValue != 'number') { + this.deleteSymptomEntry() + return + } + this.saveSymptomEntry({ value: this.state.currentValue, exclude: this.state.exclude }) diff --git a/components/cycle-day/symptoms/cervix.js b/components/cycle-day/symptoms/cervix.js index 811cb2e..8ff4385 100644 --- a/components/cycle-day/symptoms/cervix.js +++ b/components/cycle-day/symptoms/cervix.js @@ -14,14 +14,22 @@ import SymptomView from './symptom-view' export default class Cervix extends SymptomView { constructor(props) { - super() + super(props) const cycleDay = props.cycleDay this.cervix = cycleDay && cycleDay.cervix this.state = this.cervix ? this.cervix : {} } - save() { - saveSymptom('cervix', this.props.date, { + symptomName = 'cervix' + + onBackButtonPress() { + const nothingEntered = ['opening', 'firmness', 'position'].every(val => typeof this.state[val] != 'number') + if (nothingEntered) { + this.deleteSymptomEntry() + return + } + + this.saveSymptomEntry({ opening: this.state.opening, firmness: this.state.firmness, position: this.state.position, @@ -95,14 +103,6 @@ export default class Cervix extends SymptomView { symptom='cervix' date={this.props.date} currentSymptomValue={this.cervix} - saveAction={() => { - saveSymptom('cervix', this.props.date, { - opening: this.state.opening, - firmness: this.state.firmness, - position: this.state.position, - exclude: Boolean(this.state.exclude) - }) - }} navigate={this.props.navigate} /> diff --git a/components/cycle-day/symptoms/desire.js b/components/cycle-day/symptoms/desire.js index 6b690db..990b4f5 100644 --- a/components/cycle-day/symptoms/desire.js +++ b/components/cycle-day/symptoms/desire.js @@ -4,7 +4,6 @@ import { ScrollView } from 'react-native' import styles from '../../../styles' -import { saveSymptom } from '../../../db' import { intensity, desire } from '../../../i18n/en/cycle-day' import ActionButtonFooter from './action-button-footer' import SelectTabGroup from '../select-tab-group' @@ -13,15 +12,21 @@ import SymptomView from './symptom-view' export default class Desire extends SymptomView { constructor(props) { - super() + super(props) const cycleDay = props.cycleDay this.desire = cycleDay && cycleDay.desire const desireValue = this.desire && this.desire.value this.state = { currentValue: desireValue } } - save() { - saveSymptom('desire', this.props.date, { value: this.state.currentValue }) + symptomName = 'desire' + + onBackButtonPress() { + if (!this.state.currentValue) { + this.deleteSymptomEntry() + return + } + this.saveSymptomEntry({ value: this.state.currentValue }) } render() { diff --git a/components/cycle-day/symptoms/mood.js b/components/cycle-day/symptoms/mood.js index bbaa2e4..5221deb 100644 --- a/components/cycle-day/symptoms/mood.js +++ b/components/cycle-day/symptoms/mood.js @@ -4,7 +4,6 @@ import { TextInput, View } from 'react-native' -import { saveSymptom } from '../../../db' import { mood as labels } from '../../../i18n/en/cycle-day' import ActionButtonFooter from './action-button-footer' import SelectBoxGroup from '../select-box-group' @@ -14,7 +13,7 @@ import SymptomView from './symptom-view' export default class Mood extends SymptomView { constructor(props) { - super() + super(props) const cycleDay = props.cycleDay if (cycleDay && cycleDay.mood) { this.state = Object.assign({}, cycleDay.mood) @@ -26,12 +25,19 @@ export default class Mood extends SymptomView { } } - save() { + symptomName = "mood" + + onBackButtonPress() { + 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 } - saveSymptom('mood', this.props.date, copyOfState) + this.saveSymptomEntry(copyOfState) } toggleState = (key) => { diff --git a/components/cycle-day/symptoms/mucus.js b/components/cycle-day/symptoms/mucus.js index 16e5529..0eee93f 100644 --- a/components/cycle-day/symptoms/mucus.js +++ b/components/cycle-day/symptoms/mucus.js @@ -5,7 +5,6 @@ import { ScrollView } from 'react-native' import styles from '../../../styles' -import { saveSymptom } from '../../../db' import { mucus as labels } from '../../../i18n/en/cycle-day' import computeNfpValue from '../../../lib/nfp-mucus' import ActionButtonFooter from './action-button-footer' @@ -15,16 +14,24 @@ import SymptomView from './symptom-view' export default class Mucus extends SymptomView { constructor(props) { - super() + super(props) const cycleDay = props.cycleDay this.mucus = cycleDay && cycleDay.mucus this.state = this.mucus ? this.mucus : {} } - save() { + symptomName = 'mucus' + + onBackButtonPress() { + const nothingEntered = ['feeling', 'texture'].every(val => typeof this.state[val] != 'number') + if (nothingEntered) { + this.deleteSymptomEntry() + return + } + const feeling = this.state.feeling const texture = this.state.texture - saveSymptom('mucus', this.props.date, { + this.saveSymptomEntry({ feeling, texture, value: computeNfpValue(feeling, texture), diff --git a/components/cycle-day/symptoms/note.js b/components/cycle-day/symptoms/note.js index 06ee83a..08fac70 100644 --- a/components/cycle-day/symptoms/note.js +++ b/components/cycle-day/symptoms/note.js @@ -24,8 +24,14 @@ export default class Note extends SymptomView { } } - save() { - saveSymptom('note', this.props.date, { + symptomName = 'note' + + onBackButtonPress() { + if (!this.state.currentValue) { + this.deleteSymptomEntry() + return + } + this.saveSymptomEntry({ value: this.state.currentValue }) } diff --git a/components/cycle-day/symptoms/pain.js b/components/cycle-day/symptoms/pain.js index 48eb15b..aa1c074 100644 --- a/components/cycle-day/symptoms/pain.js +++ b/components/cycle-day/symptoms/pain.js @@ -27,12 +27,20 @@ export default class Pain extends SymptomView { } } - save() { + symptomName = 'pain' + + onBackButtonPress() { + 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 } - saveSymptom('pain', this.props.date, copyOfState) + this.saveSymptomEntry(copyOfState) } toggleState = (key) => { diff --git a/components/cycle-day/symptoms/sex.js b/components/cycle-day/symptoms/sex.js index 4d5c8e6..596e3c6 100644 --- a/components/cycle-day/symptoms/sex.js +++ b/components/cycle-day/symptoms/sex.js @@ -5,7 +5,6 @@ import { ScrollView } from 'react-native' import styles from '../../../styles' -import { saveSymptom } from '../../../db' import { sex as sexLabels, contraceptives as contraceptivesLabels } from '../../../i18n/en/cycle-day' import { shared as sharedLabels } from '../../../i18n/en/labels' import ActionButtonFooter from './action-button-footer' @@ -27,12 +26,20 @@ export default class Sex extends SymptomView { if (this.state.note) this.state.other = true } - save() { + symptomName = "sex" + + onBackButtonPress() { + 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 } - saveSymptom('sex', this.props.date, copyOfState) + this.saveSymptomEntry(copyOfState) } toggleState = (key) => { diff --git a/components/cycle-day/symptoms/symptom-view.js b/components/cycle-day/symptoms/symptom-view.js index 4110d99..987a993 100644 --- a/components/cycle-day/symptoms/symptom-view.js +++ b/components/cycle-day/symptoms/symptom-view.js @@ -1,10 +1,21 @@ import { Component } from 'react' import { BackHandler } from 'react-native' +import { saveSymptom } from '../../../db' export default class SymptomView extends Component { - constructor() { + constructor(props) { super() - this.backHandler = BackHandler.addEventListener('hardwareBackPress', this.save.bind(this)) + this.backHandler = BackHandler.addEventListener('hardwareBackPress', this.onBackButtonPress.bind(this)) + this.symptomName = props.symptomName + this.date = props.date + } + + saveSymptomEntry(entry) { + saveSymptom(this.symptomName, this.date, entry) + } + + deleteSymptomEntry() { + saveSymptom(this.symptomName, this.date) } componentWillUnmount() { diff --git a/components/cycle-day/symptoms/temperature.js b/components/cycle-day/symptoms/temperature.js index f9de279..476d90c 100644 --- a/components/cycle-day/symptoms/temperature.js +++ b/components/cycle-day/symptoms/temperature.js @@ -9,7 +9,7 @@ import { import DateTimePicker from 'react-native-modal-datetime-picker-nevo' import padWithZeros from '../../helpers/pad-time-with-zeros' -import { getPreviousTemperature, saveSymptom } from '../../../db' +import { getPreviousTemperature } from '../../../db' import styles from '../../../styles' import { LocalTime, ChronoUnit } from 'js-joda' import { temperature as labels } from '../../../i18n/en/cycle-day' @@ -54,7 +54,14 @@ export default class Temp extends SymptomView { } } - save() { + symptomName = 'temperature' + + onBackButtonPress() { + if (this.state.temperature === '') { + this.deleteSymptomEntry() + return + } + this.checkRangeAndSave() } @@ -65,8 +72,8 @@ export default class Temp extends SymptomView { time: this.state.time, note: this.state.note } - saveSymptom('temperature', this.props.date, dataToSave) - this.props.navigate('CycleDay', {date: this.props.date}) + + this.saveSymptomEntry(dataToSave) } checkRangeAndSave = () => {