feat: pre-fill measurement form with latest measurement values
This commit is contained in:
parent
129ac2c241
commit
56168c3021
1 changed files with 23 additions and 3 deletions
|
|
@ -1,14 +1,34 @@
|
||||||
import React from 'react'
|
import React, { useEffect, useState } from 'react'
|
||||||
import { Alert } from 'react-native'
|
import { Alert } from 'react-native'
|
||||||
import MeasurementForm from '../components/MeasurementForm'
|
import MeasurementForm from '../components/MeasurementForm'
|
||||||
import { Measurement } from '../types'
|
import { Measurement } from '../types'
|
||||||
import { initDatabase, insertMeasurement } from '../database/db'
|
import { initDatabase, insertMeasurement, getLatestMeasurement } from '../database/db'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
navigation: any
|
navigation: any
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function NewMeasurementScreen({ navigation }: Props) {
|
export default function NewMeasurementScreen({ navigation }: Props) {
|
||||||
|
const [latest, setLatest] = useState<Partial<Measurement> | undefined>(undefined)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const load = async () => {
|
||||||
|
await initDatabase()
|
||||||
|
const m = await getLatestMeasurement()
|
||||||
|
if (m) {
|
||||||
|
setLatest({
|
||||||
|
weight: m.weight,
|
||||||
|
bmi: m.bmi,
|
||||||
|
bodyFatPercentage: m.bodyFatPercentage,
|
||||||
|
musclePercentage: m.musclePercentage,
|
||||||
|
visceralFat: m.visceralFat,
|
||||||
|
bmr: m.bmr,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
load()
|
||||||
|
}, [])
|
||||||
|
|
||||||
const handleSubmit = async (m: Measurement) => {
|
const handleSubmit = async (m: Measurement) => {
|
||||||
await initDatabase()
|
await initDatabase()
|
||||||
await insertMeasurement(m)
|
await insertMeasurement(m)
|
||||||
|
|
@ -17,5 +37,5 @@ export default function NewMeasurementScreen({ navigation }: Props) {
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
return <MeasurementForm onSubmit={handleSubmit} />
|
return <MeasurementForm onSubmit={handleSubmit} initialValues={latest} />
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue