Implementing Julia's suggestion to remove repetition

This commit is contained in:
emelko
2018-07-20 19:11:24 +02:00
parent 32c08c8df7
commit 0f6f28e8e4
3 changed files with 36 additions and 195 deletions
-155
View File
@@ -1,155 +0,0 @@
import React, { Component } from 'react'
import {
View,
Button,
Text,
Switch
} from 'react-native'
import RadioForm from 'react-native-simple-radio-button'
import { saveCervix } from '../db'
import styles from '../styles/index'
import {
cervixPosition as positionLabels,
cervixConsistency as consistencyLabels
} from '../labels/labels'
import computeSensiplanValue from '../lib/sensiplan-cervix'
export default class Cervix extends Component {
constructor(props) {
super(props)
this.cycleDay = props.cycleDay
this.showView = props.showView
let currentPositionValue = this.cycleDay.cervix && this.cycleDay.cervix.position
if (typeof currentPositionValue !== 'number') {
currentPositionValue = -1
}
let currentConsistencyValue = this.cycleDay.cervix && this.cycleDay.cervix.consistency
if (typeof currentConsistencyValue !== 'number') {
currentConsistencyValue = -1
}
this.state = {
currentPositionValue,
currentConsistencyValue,
computeSensiplanValue,
exclude: this.cycleDay.cervix ? this.cycleDay.cervix.exclude : false
}
}
render() {
const cervixPositionRadioProps = [
{label: positionLabels[0], value: 0 },
{label: positionLabels[1], value: 1 },
{label: positionLabels[2], value: 2 }
]
const cervixConsistencyRadioProps = [
{label: consistencyLabels[0], value: 0 },
{label: consistencyLabels[1], value: 1 }
]
return(
<View style={ styles.symptomEditView }>
<View style={ styles.symptomEditSplitSymptomsAndLastRowButtons }>
<View style={ styles.symptomEditListedSymptomView }>
<View style={{flex: 1}}>
<Text style={styles.symptomDayView}>Cervix</Text>
</View>
<View style={{flex: 1}}>
<Text style={styles.symptomDayView}>Position</Text>
</View>
<View style={{flex: 1}}>
<RadioForm
radio_props={cervixPositionRadioProps}
initial={this.state.currentPositionValue}
formHorizontal={true}
labelHorizontal={false}
labelStyle={styles.radioButton}
onPress={(itemValue) => {
this.setState({currentPositionValue: itemValue})
}}
/>
</View>
<View style={{flex: 1}}>
<Text style={styles.symptomDayView}>Consistency</Text>
</View>
<View style={{flex: 1}}>
<RadioForm
radio_props={cervixConsistencyRadioProps}
initial={this.state.currentConsistencyValue}
formHorizontal={true}
labelHorizontal={false}
labelStyle={styles.radioButton}
onPress={(itemValue) => {
this.setState({currentConsistencyValue: itemValue})
}}
/>
</View>
</View>
<View style={ styles.itemsInRowSeparatedView }>
<View style={ styles.singleButtonView }>
<Text style={ styles.symptomDayView }>Exclude</Text>
</View>
<View style={ styles.singleButtonView }>
<Switch
onValueChange={(val) => {
this.setState({exclude: val})
}}
value={this.state.exclude}
/>
</View>
</View>
</View>
<View style={ styles.itemsInRowSeparatedView }>
<View style={ styles.singleButtonView }>
<Button
onPress={() => this.showView('dayView')}
title="Cancel">
</Button>
</View>
<View style={ styles.singleButtonView }>
<Button
onPress={() => {
saveCervix(this.cycleDay)
this.showView('dayView')
}}
title="Delete">
</Button>
</View>
<View style={ styles.singleButtonView }>
<Button
onPress={() => {
saveCervix(this.cycleDay, {
position: this.state.currentPositionValue,
consistency: this.state.currentConsistencyValue,
computedNfp: computeSensiplanValue(this.state.currentPositionValue, this.state.currentConsistencyValue),
exclude: this.state.exclude
})
this.showView('dayView')
}}
disabled={ this.state.currentPositionValue === -1 || this.state.currentConsistencyValue === -1 }
title="Save">
</Button>
</View>
</View>
</View>
)
}
}
+19 -23
View File
@@ -20,20 +20,16 @@ export default class Cervix extends Component {
this.makeActionButtons = props.makeActionButtons this.makeActionButtons = props.makeActionButtons
this.state = { this.state = {
exclude: this.cycleDay.cervix ? this.cycleDay.cervix.exclude : false exclude: this.cycleDay.cervix ? this.cycleDay.cervix.exclude : false
} };
this.state.currentOpeningValue = this.cycleDay.cervix && this.cycleDay.cervix.opening /* eslint-disable react/no-direct-mutation-state */
if (typeof this.state.currentOpeningValue !== 'number') { ['opening', 'firmness', 'position'].forEach(label => {
this.state.currentOpeningValue = -1 this.state[label] = this.cycleDay.cervix && this.cycleDay.cervix[label]
} if (typeof this.state[label] !== 'number') {
this.state.currentFirmnessValue = this.cycleDay.cervix && this.cycleDay.cervix.firmness this.state[label] = -1
if (typeof this.state.currentFirmnessValue !== 'number') { }
this.state.currentFirmnessValue = -1 })
} /* eslint-enable react/no-direct-mutation-state */
this.state.currentPositionValue = this.cycleDay.cervix && this.cycleDay.cervix.position
if (typeof this.state.currentPositionValue !== 'number') {
this.state.currentPositionValue = -1
}
} }
render() { render() {
@@ -58,12 +54,12 @@ export default class Cervix extends Component {
<View style={styles.radioButtonRow}> <View style={styles.radioButtonRow}>
<RadioForm <RadioForm
radio_props={cervixOpeningRadioProps} radio_props={cervixOpeningRadioProps}
initial={this.state.currentOpeningValue} initial={this.state.opening}
formHorizontal={true} formHorizontal={true}
labelHorizontal={false} labelHorizontal={false}
labelStyle={styles.radioButton} labelStyle={styles.radioButton}
onPress={(itemValue) => { onPress={(itemValue) => {
this.setState({currentOpeningValue: itemValue}) this.setState({opening: itemValue})
}} }}
/> />
</View> </View>
@@ -71,12 +67,12 @@ export default class Cervix extends Component {
<View style={styles.radioButtonRow}> <View style={styles.radioButtonRow}>
<RadioForm <RadioForm
radio_props={cervixFirmnessRadioProps} radio_props={cervixFirmnessRadioProps}
initial={this.state.currentFirmnessValue} initial={this.state.firmness}
formHorizontal={true} formHorizontal={true}
labelHorizontal={false} labelHorizontal={false}
labelStyle={styles.radioButton} labelStyle={styles.radioButton}
onPress={(itemValue) => { onPress={(itemValue) => {
this.setState({currentFirmnessValue: itemValue}) this.setState({firmness: itemValue})
}} }}
/> />
</View> </View>
@@ -84,12 +80,12 @@ export default class Cervix extends Component {
<View style={styles.radioButtonRow}> <View style={styles.radioButtonRow}>
<RadioForm <RadioForm
radio_props={cervixPositionRadioProps} radio_props={cervixPositionRadioProps}
initial={this.state.currentPositionValue} initial={this.state.position}
formHorizontal={true} formHorizontal={true}
labelHorizontal={false} labelHorizontal={false}
labelStyle={styles.radioButton} labelStyle={styles.radioButton}
onPress={(itemValue) => { onPress={(itemValue) => {
this.setState({currentPositionValue: itemValue}) this.setState({position: itemValue})
}} }}
/> />
</View> </View>
@@ -109,13 +105,13 @@ export default class Cervix extends Component {
cycleDay: this.cycleDay, cycleDay: this.cycleDay,
saveAction: () => { saveAction: () => {
saveSymptom('cervix', this.cycleDay, { saveSymptom('cervix', this.cycleDay, {
opening: this.state.currentOpeningValue, opening: this.state.opening,
firmness: this.state.currentFirmnessValue, firmness: this.state.firmness,
position: this.state.currentPositionValue, position: this.state.position,
exclude: this.state.exclude exclude: this.state.exclude
}) })
}, },
saveDisabled: this.state.currentOpeningValue === -1 || this.state.currentFirmnessValue === -1 saveDisabled: this.state.opening === -1 || this.state.firmness === -1
} }
)} )}
</View> </View>
+17 -17
View File
@@ -21,17 +21,17 @@ export default class Mucus extends Component {
this.makeActionButtons = props.makeActionButtons this.makeActionButtons = props.makeActionButtons
this.state = { this.state = {
exclude: this.cycleDay.mucus ? this.cycleDay.mucus.exclude : false exclude: this.cycleDay.mucus ? this.cycleDay.mucus.exclude : false
} };
this.state.currentFeelingValue = this.cycleDay.mucus && this.cycleDay.mucus.feeling /* eslint-disable react/no-direct-mutation-state */
if (typeof this.state.currentFeelingValue !== 'number') { ['feeling', 'texture'].forEach(label => {
this.state.currentFeelingValue = -1 this.state[label] = this.cycleDay.mucus && this.cycleDay.mucus[label]
} if (typeof this.state[label] !== 'number') {
this.state[label] = -1
}
})
/* eslint-enable react/no-direct-mutation-state */
this.state.currentTextureValue = this.cycleDay.mucus && this.cycleDay.mucus.texture
if (typeof this.state.currentTextureValue !== 'number') {
this.state.currentTextureValue = -1
}
} }
render() { render() {
@@ -53,12 +53,12 @@ export default class Mucus extends Component {
<View style={styles.radioButtonRow}> <View style={styles.radioButtonRow}>
<RadioForm <RadioForm
radio_props={mucusFeelingRadioProps} radio_props={mucusFeelingRadioProps}
initial={this.state.currentFeelingValue} initial={this.state.feeling}
formHorizontal={true} formHorizontal={true}
labelHorizontal={false} labelHorizontal={false}
labelStyle={styles.radioButton} labelStyle={styles.radioButton}
onPress={(itemValue) => { onPress={(itemValue) => {
this.setState({ currentFeelingValue: itemValue }) this.setState({feeling: itemValue })
}} }}
/> />
</View> </View>
@@ -66,12 +66,12 @@ export default class Mucus extends Component {
<View style={styles.radioButtonRow}> <View style={styles.radioButtonRow}>
<RadioForm <RadioForm
radio_props={mucusTextureRadioProps} radio_props={mucusTextureRadioProps}
initial={this.state.currentTextureValue} initial={this.state.texture}
formHorizontal={true} formHorizontal={true}
labelHorizontal={false} labelHorizontal={false}
labelStyle={styles.radioButton} labelStyle={styles.radioButton}
onPress={(itemValue) => { onPress={(itemValue) => {
this.setState({ currentTextureValue: itemValue }) this.setState({texture: itemValue })
}} }}
/> />
</View> </View>
@@ -92,13 +92,13 @@ export default class Mucus extends Component {
cycleDay: this.cycleDay, cycleDay: this.cycleDay,
saveAction: () => { saveAction: () => {
saveSymptom('mucus', this.cycleDay, { saveSymptom('mucus', this.cycleDay, {
feeling: this.state.currentFeelingValue, feeling: this.state.feeling,
texture: this.state.currentTextureValue, texture: this.state.texture,
computedNfp: computeSensiplanValue(this.state.currentFeelingValue, this.state.currentTextureValue), computedNfp: computeSensiplanValue(this.state.feeling, this.state.texture),
exclude: this.state.exclude exclude: this.state.exclude
}) })
}, },
saveDisabled: this.state.currentFeelingValue === -1 || this.state.currentTextureValue === -1 saveDisabled: this.state.feeling === -1 || this.state.texture === -1
} }
)} )}
</View> </View>