Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
94
95
96
97
98
99
100
101
102
103
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { View } from 'react-native'
import AppText from '../../app-text'
import AppTextInput from '../../app-text-input'
import { temperature as labels } from '../../../i18n/en/cycle-day'
import styles from '../../../styles'
import { getPreviousTemperature } from '../../../db'
import { scaleObservable } from '../../../local-storage'
import config from '../../../config'
export default class TemperatureInput extends Component {
static propTypes = {
temperature: PropTypes.string,
handleTemperatureChange: PropTypes.func,
date: PropTypes.string,
}
constructor(props) {
super(props)
let shouldShowSuggestion = false
let suggestedTemperature = null
if (!props.temperature) {
const prevTemp = getPreviousTemperature(props.date)
if (prevTemp) {
shouldShowSuggestion = true
suggestedTemperature = prevTemp.toString()
}
}
this.state = {
temperature: props.temperature,
shouldShowSuggestion,
suggestedTemperature
}
}
setTemperature = (temperature) => {
this.setState({
shouldShowSuggestion: false,
temperature
})
this.props.handleTemperatureChange(Number(temperature))
}
render () {
const {
shouldShowSuggestion,
suggestedTemperature,
temperature
} = this.state
const inputStyle = [
styles.temperatureTextInput,
shouldShowSuggestion ? styles.temperatureTextInputSuggestion : null
]
return (
<React.Fragment>
<View style={styles.framedSegmentInlineChildren}>
<AppTextInput
style={inputStyle}
autoFocus={true}
value={shouldShowSuggestion ? suggestedTemperature : temperature}
onChangeText={this.setTemperature}
keyboardType='numeric'
maxLength={5}
/>
<AppText style={{ marginLeft: 5 }}>°C</AppText>
</View>
<OutOfRangeWarning temperature={this.props.temperature} />
</React.Fragment>
)
}
}
const OutOfRangeWarning = ({ temperature }) => {
if (temperature === '') {
return false
}
const value = Number(temperature)
const { min, max } = config.temperatureScale
const range = { min, max }
const scale = scaleObservable.value
let warningMsg
if (value < range.min || value > range.max) {
warningMsg = labels.outOfAbsoluteRangeWarning
} else if (value < scale.min || value > scale.max) {
warningMsg = labels.outOfRangeWarning
} else {
warningMsg = null
}
return <AppText style={styles.hint}>{warningMsg}</AppText>
}