Naming update, change switch to object, fertility logic review

This commit is contained in:
mashazyu
2019-09-23 15:04:46 +02:00
committed by Sofiya Tepikin
parent 889e0e36d7
commit f376602843
+94 -59
View File
@@ -27,50 +27,21 @@ class DayColumn extends Component {
constructor(props) { constructor(props) {
super() super()
const { dateString, columnHeight, chartSymptoms } = props const { dateString, chartSymptoms, columnHeight } = props
const cycleDayData = getCycleDay(dateString) const cycleDayData = getCycleDay(dateString)
this.data = {} this.data = {}
if (cycleDayData) { if (cycleDayData) {
this.data = chartSymptoms.reduce((symptomDataToDisplay, symptom) => { this.data = chartSymptoms.reduce((symptomDataToDisplay, symptom, ) => {
const symptomData = cycleDayData[symptom] const symptomData = cycleDayData[symptom]
if (symptomData && !this.isSymptomExcluded(symptomData)) { if (symptomData && symptom === 'temperature') {
symptomDataToDisplay[symptom] = {} symptomDataToDisplay[symptom] =
switch (symptom) { this.getTemperatureProps(symptomData, columnHeight, dateString)
case 'temperature': { } else {
symptomDataToDisplay[symptom].data = symptomData.value if (symptomData && ! symptomData.exclude) {
symptomDataToDisplay.y = symptomDataToDisplay[symptom] =
normalizeToScale(symptomData.value, columnHeight) (this.getSymptomColorIndex[symptom] || this.getSymptomColorIndex['default'])(symptomData)
const neighbourTemperatureValue =
getInfoForNeighborColumns(dateString, columnHeight)
for (const key in neighbourTemperatureValue) {
symptomDataToDisplay[key] = neighbourTemperatureValue[key]
}
break
}
case 'cervix':
symptomDataToDisplay[symptom].data =
(symptomData.opening + symptomData.firmness) > 0 ? 2 : 0
symptomDataToDisplay[symptom].isComplete =
this.isCervixDataComplete(symptomData)
break
case 'mucus':
symptomDataToDisplay[symptom].data =
(symptomData.feeling + symptomData.texture) > 0 ? 2 : 0
symptomDataToDisplay[symptom].isComplete =
this.isMucusDataComplete(symptomData)
break
case 'sex':
symptomDataToDisplay[symptom].data =
symptomData.solo + 2 * symptomData.partner - 1
break
case 'bleeding':
case 'desire':
symptomDataToDisplay[symptom].data = symptomData.value
break
default: // pain, mood, note
symptomDataToDisplay[symptom].data = 0
} }
} }
@@ -80,20 +51,78 @@ class DayColumn extends Component {
this.fhmAndLtl = props.getFhmAndLtlInfo( this.fhmAndLtl = props.getFhmAndLtlInfo(
props.dateString, props.dateString,
this.data.temperature, this.data.temperature ? this.data.temperature.value : null,
props.columnHeight props.columnHeight
) )
} }
isSymptomExcluded = (symptomData) => { getTemperatureProps = (symptomData, columnHeight, dateString) => {
return symptomData && symptomData.exclude ? symptomData.exclude : false const extractedData = {}
const { value, exclude } = symptomData
const neighborTemperatureGraphPoints =
getInfoForNeighborColumns(dateString, columnHeight)
for (const key in neighborTemperatureGraphPoints) {
extractedData[key] = neighborTemperatureGraphPoints[key]
}
return Object.assign({
value,
y: normalizeToScale(value, columnHeight),
temperatureExclude: exclude,
}, extractedData)
} }
isCervixDataComplete = (symptomData) => getSymptomColorIndex = {
(symptomData.opening != null) && (symptomData.firmness != null) 'mucus': (symptomData) => {
const { feeling, texture } = symptomData
const colorIndex = feeling + texture
return colorIndex
},
'cervix': (symptomData) => {
const { opening, firmness } = symptomData
const isDataComplete = opening !== null && firmness !== null
// is fertile? fertile only when opening=closed and firmness=hard (=0)
const isFertile = isDataComplete && (opening === 0 && firmness === 0)
const colorIndex = isFertile ? 0 : 2
return colorIndex
},
'sex': (symptomData) => {
const { solo, partner } = symptomData
const colorIndex = (solo !== null && partner !== null) ?
(solo + 2 * partner - 1) : 0
return colorIndex
},
'bleeding': (symptomData) => {
const { value } = symptomData
const colorIndex = value
return colorIndex
},
'default': () => { // desire, pain, mood, note
const colorIndex = 0
return colorIndex
}
}
isMucusDataComplete = (symptomData) => isSymptomDataComplete = (symptom) => {
(symptomData.feeling != null) && (symptomData.texture != null) const { dateString } = this.props
const cycleDayData = getCycleDay(dateString)
const symptomData = cycleDayData[symptom]
const dataCompletenessCheck = {
'cervix': () => {
const { opening, firmness } = symptomData
return (opening !== null) && (firmness !== null)
},
'mucus': () => {
const { feeling, texture } = symptomData
return (feeling !== null) && (texture !== null)
},
'default': () => {
return true
}
}
return (dataCompletenessCheck[symptom] || dataCompletenessCheck['default'])()
}
onDaySelect = (date) => { onDaySelect = (date) => {
this.props.setDate(date) this.props.setDate(date)
@@ -114,14 +143,13 @@ class DayColumn extends Component {
const styleSymptom = styles.iconShades[symptom] const styleSymptom = styles.iconShades[symptom]
const symptomData = this.data[symptom] const symptomData = this.data[symptom]
const isDataIncomplete = !symptomData.isComplete const dataIsComplete = this.isSymptomDataComplete(symptom)
const isMucusOrCervix = (symptom === 'mucus') || (symptom === 'cervix') const isMucusOrCervix = (symptom === 'mucus') || (symptom === 'cervix')
const backgroundColor = ( isMucusOrCervix && isDataIncomplete) ? const backgroundColor = (isMucusOrCervix && !dataIsComplete) ?
'white' : styleSymptom[symptomData.data] 'white' : styleSymptom[symptomData]
const borderWidth = ( isMucusOrCervix && isDataIncomplete) ? 2 : 0 const borderWidth = (isMucusOrCervix && !dataIsComplete) ? 2 : 0
const borderColor = styleSymptom[0] const borderColor = styleSymptom[0]
const styleChild = [styles.symptomIcon, { const styleChild = [styles.symptomIcon, {
backgroundColor, backgroundColor,
borderColor, borderColor,
@@ -130,7 +158,7 @@ class DayColumn extends Component {
return ( return (
<View style={styleParent} key={symptom}> <View style={styleParent} key={symptom}>
{shouldDrawSymptom && <View style={styleChild} />} <View style={styleChild} />
</View> </View>
) )
} else { } else {
@@ -148,7 +176,6 @@ class DayColumn extends Component {
columnHeight, columnHeight,
xAxisHeight } = this.props xAxisHeight } = this.props
if(this.fhmAndLtl.drawLtlAt) { if(this.fhmAndLtl.drawLtlAt) {
const ltlLine = (<Shape const ltlLine = (<Shape
stroke={styles.nfpLine.stroke} stroke={styles.nfpLine.stroke}
@@ -174,15 +201,23 @@ class DayColumn extends Component {
columnElements.push(fhmLine) columnElements.push(fhmLine)
} }
if (this.data.y) { if (this.data && this.data.temperature && this.data.temperature.y) {
const { temperatureExclude,
y,
rightY,
leftY,
rightTemperatureExclude,
leftTemperatureExclude
} = this.data.temperature
columnElements.push( columnElements.push(
<DotAndLine <DotAndLine
y={this.data.y} y={y}
exclude={this.data.temperatureExclude} exclude={temperatureExclude}
rightY={this.data.rightY} rightY={rightY}
rightTemperatureExclude={this.data.rightTemperatureExclude} rightTemperatureExclude={rightTemperatureExclude}
leftY={this.data.leftY} leftY={leftY}
leftTemperatureExclude={this.data.leftTemperatureExclude} leftTemperatureExclude={leftTemperatureExclude}
key='dotandline' key='dotandline'
/> />
) )