Cleanups symptom view
This commit is contained in:
@@ -16,27 +16,30 @@ import { sharedDialogs } from '../../../i18n/en/cycle-day'
|
|||||||
|
|
||||||
import styles from '../../../styles'
|
import styles from '../../../styles'
|
||||||
|
|
||||||
|
const checkIfHasValues = data => {
|
||||||
|
const isMeaningfulValue = value => value || value === 0
|
||||||
|
return Object.values(data).some(isMeaningfulValue)
|
||||||
|
}
|
||||||
|
|
||||||
class SymptomView extends Component {
|
class SymptomView extends Component {
|
||||||
|
|
||||||
static propTypes = {
|
static propTypes = {
|
||||||
symptom: PropTypes.string.isRequired,
|
symptom: PropTypes.string.isRequired,
|
||||||
values: PropTypes.object,
|
values: PropTypes.object,
|
||||||
date: PropTypes.string,
|
date: PropTypes.string,
|
||||||
|
handleBackButtonPress: PropTypes.func,
|
||||||
|
children: PropTypes.node,
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super()
|
super()
|
||||||
this.values = props.values
|
|
||||||
this.state = {
|
this.state = {
|
||||||
shouldShowDelete: this.checkIfHasValuesToDelete()
|
shouldShowDelete: checkIfHasValues(props.values)
|
||||||
}
|
}
|
||||||
this.date = props.date
|
|
||||||
this.navigate = props.navigate
|
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidUpdate() {
|
componentDidUpdate() {
|
||||||
this.values = this.props.values
|
const shouldShowDelete = checkIfHasValues(this.props.values)
|
||||||
const shouldShowDelete = this.checkIfHasValuesToDelete()
|
|
||||||
if (shouldShowDelete !== this.state.shouldShowDelete) {
|
if (shouldShowDelete !== this.state.shouldShowDelete) {
|
||||||
this.setState({ shouldShowDelete })
|
this.setState({ shouldShowDelete })
|
||||||
}
|
}
|
||||||
@@ -47,17 +50,6 @@ class SymptomView extends Component {
|
|||||||
saveSymptom(symptom, date, null)
|
saveSymptom(symptom, date, null)
|
||||||
}
|
}
|
||||||
|
|
||||||
checkIfHasValuesToDelete() {
|
|
||||||
const valueHasBeenFilledOut = key => {
|
|
||||||
// is there any meaningful value in the current state?
|
|
||||||
return this.values[key] || this.values[key] === 0
|
|
||||||
}
|
|
||||||
|
|
||||||
const valuesKeys = Object.keys(this.values)
|
|
||||||
|
|
||||||
return valuesKeys.some(valueHasBeenFilledOut)
|
|
||||||
}
|
|
||||||
|
|
||||||
onDeleteConfirmation = () => {
|
onDeleteConfirmation = () => {
|
||||||
this.deleteSymptomEntry()
|
this.deleteSymptomEntry()
|
||||||
this.props.handleBackButtonPress()
|
this.props.handleBackButtonPress()
|
||||||
@@ -82,32 +74,13 @@ class SymptomView extends Component {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
showConfirmationAlert = () => {
|
|
||||||
|
|
||||||
const cancelButton = {
|
|
||||||
text: sharedDialogs.cancel,
|
|
||||||
style: 'cancel'
|
|
||||||
}
|
|
||||||
|
|
||||||
const confirmationButton = {
|
|
||||||
text: sharedDialogs.reallyDeleteData,
|
|
||||||
onPress: this.onDeleteConfirmation
|
|
||||||
}
|
|
||||||
|
|
||||||
return Alert.alert(
|
|
||||||
sharedDialogs.areYouSureTitle,
|
|
||||||
sharedDialogs.areYouSureToDelete,
|
|
||||||
[cancelButton, confirmationButton]
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { symptom } = this.props
|
const { symptom, date } = this.props
|
||||||
return (
|
return (
|
||||||
<View style={{flex: 1}}>
|
<View style={{flex: 1}}>
|
||||||
<Header
|
<Header
|
||||||
title={headerTitles[symptom]}
|
title={headerTitles[symptom]}
|
||||||
subtitle={formatDate(this.date)}
|
subtitle={formatDate(date)}
|
||||||
handleBack={this.props.handleBackButtonPress}
|
handleBack={this.props.handleBackButtonPress}
|
||||||
handleDelete={
|
handleDelete={
|
||||||
this.state.shouldShowDelete && this.showConfirmationAlert
|
this.state.shouldShowDelete && this.showConfirmationAlert
|
||||||
|
|||||||
Reference in New Issue
Block a user