Replaces the inheritance with composition pattern in the Symptom view

This commit is contained in:
Sofiya Tepikin
2019-08-16 20:32:02 +02:00
parent c0235d148b
commit 8ce840306a
17 changed files with 2577 additions and 2495 deletions
+53 -53
View File
@@ -1,68 +1,77 @@
import React from 'react'
import {
Switch,
ScrollView
} from 'react-native'
import { connect } from 'react-redux'
import React, { Component } from 'react'
import { Switch } from 'react-native'
import PropTypes from 'prop-types'
import { getDate } from '../../../slices/date'
import styles from '../../../styles'
import { mucus as labels } from '../../../i18n/en/cycle-day'
import computeNfpValue from '../../../lib/nfp-mucus'
import SelectTabGroup from '../select-tab-group'
import SymptomSection from './symptom-section'
import SymptomView from './symptom-view'
class Mucus extends SymptomView {
constructor(props) {
super(props)
const cycleDay = props.cycleDay
this.mucus = cycleDay && cycleDay.mucus
this.state = this.mucus ? this.mucus : {}
import { getLabelsList } from '../../helpers/labels'
import { saveSymptom } from '../../../db'
class Mucus extends Component {
static propTypes = {
cycleDay: PropTypes.object,
handleBackButtonPress: PropTypes.func,
date: PropTypes.string.isRequired,
}
symptomName = 'mucus'
constructor(props) {
super(props)
const symptom = 'mucus'
const { cycleDay } = props
autoSave = () => {
const nothingEntered = ['feeling', 'texture'].every(val => typeof this.state[val] != 'number')
if (nothingEntered) {
this.deleteSymptomEntry()
return
}
const defaultSymptomData = {}
const feeling = this.state.feeling
const texture = this.state.texture
this.saveSymptomEntry({
const symptomData =
cycleDay && cycleDay[symptom] ? cycleDay[symptom] : defaultSymptomData
this.state = { ...symptomData }
this.mucusFeeling = getLabelsList(labels.feeling.categories)
this.mucusTexture = getLabelsList(labels.texture.categories)
this.symptom = symptom
}
shouldAutoSave = () => {
const { date } = this.props
const nothingEntered = ['feeling', 'texture'].every(
val => typeof this.state[val] !== 'number'
)
const { feeling, texture, exclude} = this.state
const valuesToSave = {
feeling,
texture,
value: computeNfpValue(feeling, texture),
exclude: Boolean(this.state.exclude)
})
exclude: Boolean(exclude)
}
saveSymptom(this.symptom, date, nothingEntered ? null : valuesToSave)
}
renderContent() {
const mucusFeeling = [
{ label: labels.feeling.categories[0], value: 0 },
{ label: labels.feeling.categories[1], value: 1 },
{ label: labels.feeling.categories[2], value: 2 },
{ label: labels.feeling.categories[3], value: 3 }
]
const mucusTexture = [
{ label: labels.texture.categories[0], value: 0 },
{ label: labels.texture.categories[1], value: 1 },
{ label: labels.texture.categories[2], value: 2 }
]
componentDidUpdate() {
this.shouldAutoSave()
}
render() {
// TODO leaving this info for notice when leaving incomplete data
// const mandatoryNotCompletedYet = typeof this.state.feeling != 'number' || typeof this.state.texture != 'number'
return (
<ScrollView style={styles.page}>
<SymptomView
symptom={this.symptom}
values={this.state}
handleBackButtonPress={this.props.handleBackButtonPress}
date={this.props.date}
>
<SymptomSection
header='Feeling'
explainer={labels.feeling.explainer}
>
<SelectTabGroup
buttons={mucusFeeling}
buttons={this.mucusFeeling}
onSelect={val => this.setState({ feeling: val })}
active={this.state.feeling}
/>
@@ -72,7 +81,7 @@ class Mucus extends SymptomView {
explainer={labels.texture.explainer}
>
<SelectTabGroup
buttons={mucusTexture}
buttons={this.mucusTexture}
onSelect={val => this.setState({ texture: val })}
active={this.state.texture}
/>
@@ -89,18 +98,9 @@ class Mucus extends SymptomView {
value={this.state.exclude}
/>
</SymptomSection>
</ScrollView>
</SymptomView>
)
}
}
const mapStateToProps = (state) => {
return({
date: getDate(state)
})
}
export default connect(
mapStateToProps,
null
)(Mucus)
export default Mucus