Newer
Older
import React, { Component } from 'react'
import {
View,
Text,
TextInput,
} from 'react-native'
import DateTimePicker from 'react-native-modal-datetime-picker-nevo'
import { getPreviousTemperature, saveSymptom } from '../../../db'
import styles from '../../../styles'
import { LocalTime, ChronoUnit } from 'js-joda'
import { temperature as tempLabels } from '../labels/labels'
import { scaleObservable } from '../../../local-storage'
import { shared } from '../../labels'
import ActionButtonFooter from './action-button-footer'
import config from '../../../config'
const minutes = ChronoUnit.MINUTES
export default class Temp extends Component {
constructor(props) {
super(props)
this.cycleDay = props.cycleDay
this.makeActionButtons = props.makeActionButtons
const temp = this.cycleDay.temperature
this.state = {
exclude: temp ? temp.exclude : false,
time: temp ? temp.time : LocalTime.now().truncatedTo(minutes).toString(),
isTimePickerVisible: false,
outOfRange: null
}
this.state.temperature = temp.value.toString()
if (temp.value === Math.floor(temp.value)) {
this.state.temperature = `${this.state.temperature}.0`
}
} else {
const prevTemp = getPreviousTemperature(this.cycleDay)
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
saveTemperature = () => {
const dataToSave = {
value: Number(this.state.temperature),
exclude: this.state.exclude,
time: this.state.time
}
saveSymptom('temperature', this.cycleDay, dataToSave)
this.props.navigate('CycleDay', {cycleDay: this.cycleDay})
}
checkRangeAndSave = () => {
const value = Number(this.state.temperature)
const absolute = {
min: config.temperatureScale.min,
max: config.temperatureScale.max
}
const scale = scaleObservable.value
let warningMsg
if (value < absolute.min || value > absolute.max) {
warningMsg = tempLabels.outOfAbsoluteRangeWarning
} else if (value < scale.min || value > scale.max) {
warningMsg = tempLabels.outOfRangeWarning
}
if (warningMsg) {
Alert.alert(
shared.warning,
warningMsg,
[
{ text: shared.cancel },
{ text: shared.save, onPress: this.saveTemperature}
]
)
} else {
this.saveTemperature()
}
}
render() {
return (
<View style={{ flex: 1 }}>
<ScrollView>
<View>
<View style={styles.symptomViewRowInline}>
<Text style={styles.symptomDayView}>Temperature (°C)</Text>
<TempInput
value={this.state.temperature}
setState={(val) => this.setState(val)}
isSuggestion={this.state.isSuggestion}
/>
</View>
<View style={styles.symptomViewRowInline}>
<Text style={styles.symptomDayView}>Time</Text>
<TextInput
style={styles.temperatureTextInput}
onFocus={() => {
Keyboard.dismiss()
this.setState({ isTimePickerVisible: true })
}}
value={this.state.time}
/>
</View>
<DateTimePicker
mode="time"
isVisible={this.state.isTimePickerVisible}
onConfirm={jsDate => {
this.setState({
time: `${jsDate.getHours()}:${jsDate.getMinutes()}`,
isTimePickerVisible: false
})
onCancel={() => this.setState({ isTimePickerVisible: false })}
<View style={styles.symptomViewRowInline}>
<Text style={styles.symptomDayView}>Exclude</Text>
<Switch
onValueChange={(val) => {
this.setState({ exclude: val })
}}
value={this.state.exclude}
/>
</View>
<ActionButtonFooter
symptom='temperature'
cycleDay={this.cycleDay}
saveAction={() => this.checkRangeAndSave()}
saveDisabled={
this.state.temperature === '' ||
isNaN(Number(this.state.temperature)) ||
isInvalidTime(this.state.time)
}
navigate={this.props.navigate}
</View>
)
}
}
class TempInput extends Component {
render() {
const style = [styles.temperatureTextInput]
if (this.props.isSuggestion) {
style.push(styles.temperatureTextInputSuggestion)
}
onChangeText={(val) => {
if (isNaN(Number(val))) return
this.props.setState({ temperature: val, isSuggestion: false })
function isInvalidTime(timeString) {
try {
LocalTime.parse(timeString)
} catch (err) {
return true
}
return false
}