Introduce realm and persist temperatures
This commit is contained in:
@@ -4,25 +4,17 @@ import {
|
||||
Text,
|
||||
Button,
|
||||
TextInput,
|
||||
FlatList
|
||||
FlatList,
|
||||
Keyboard
|
||||
} from 'react-native'
|
||||
import * as styles from './styles'
|
||||
import Datastore from 'react-native-local-mongodb'
|
||||
|
||||
const db = new Datastore({ filename: 'asyncStorageKey', autoload: true })
|
||||
import * as styles from './styles'
|
||||
import { cycleDaysSortedbyTempValueView, saveTemperature } from './db'
|
||||
|
||||
export default class Temp extends Component {
|
||||
constructor(props) {
|
||||
super(props)
|
||||
this.state = {
|
||||
temperatures: []
|
||||
}
|
||||
db.find({ key: { $exists: true } }, (err, persistedTemperatures) => {
|
||||
if (err) throw err
|
||||
this.setState({
|
||||
temperatures: [...persistedTemperatures, ...this.state.temperatures]
|
||||
})
|
||||
})
|
||||
this.state = { currentValue: '' }
|
||||
}
|
||||
|
||||
render() {
|
||||
@@ -33,26 +25,26 @@ export default class Temp extends Component {
|
||||
onChangeText={(val) => {
|
||||
this.setState({currentValue: val})
|
||||
}}
|
||||
keyboardType='numeric'
|
||||
value = {this.state.currentValue}
|
||||
/>
|
||||
<Button
|
||||
onPress={() => {
|
||||
const newTemp = {
|
||||
value: this.state.currentValue,
|
||||
key: Date.now().toString()
|
||||
}
|
||||
this.setState({
|
||||
temperatures: [newTemp, ...this.state.temperatures]
|
||||
})
|
||||
db.insert(newTemp, (err) => {
|
||||
if (err) console.log(err)
|
||||
})
|
||||
saveTemperature(
|
||||
new Date(),
|
||||
{
|
||||
value: Number(this.state.currentValue),
|
||||
exclude: false
|
||||
}
|
||||
)
|
||||
this.setState({currentValue: ''})
|
||||
Keyboard.dismiss()
|
||||
}}
|
||||
title="Save"
|
||||
/>
|
||||
<FlatList
|
||||
data = {this.state.temperatures}
|
||||
extraData = {this.state}
|
||||
renderItem={({item}) => <Text>{item.value}</Text>}
|
||||
data = { cycleDaysSortedbyTempValueView }
|
||||
renderItem={({item}) => <Text>{item.temperature.value}</Text>}
|
||||
/>
|
||||
</View>
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user