feat: detect duplicate measurement dates, ask to overwrite
This commit is contained in:
parent
3523637b17
commit
1f43918e84
2 changed files with 36 additions and 2 deletions
|
|
@ -47,6 +47,15 @@ export async function getLatestMeasurement(): Promise<Measurement | null> {
|
||||||
return rows.length > 0 ? (rows[0] as Measurement) : null
|
return rows.length > 0 ? (rows[0] as Measurement) : null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getMeasurementByDate(date: string): Promise<Measurement | null> {
|
||||||
|
const day = date.slice(0, 10)
|
||||||
|
const rows = await db!.getAllAsync<any>(
|
||||||
|
"SELECT * FROM measurements WHERE measuredAt LIKE ? LIMIT 1",
|
||||||
|
[`${day}%`]
|
||||||
|
)
|
||||||
|
return rows.length > 0 ? (rows[0] as Measurement) : null
|
||||||
|
}
|
||||||
|
|
||||||
export async function updateMeasurement(id: string, syncStatus: SyncStatus): Promise<void> {
|
export async function updateMeasurement(id: string, syncStatus: SyncStatus): Promise<void> {
|
||||||
await db!.runAsync(
|
await db!.runAsync(
|
||||||
'UPDATE measurements SET syncStatus = ?, updatedAt = ? WHERE id = ?',
|
'UPDATE measurements SET syncStatus = ?, updatedAt = ? WHERE id = ?',
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ import { View, Text, Alert } from 'react-native'
|
||||||
import { useFocusEffect } from '@react-navigation/native'
|
import { useFocusEffect } from '@react-navigation/native'
|
||||||
import MeasurementForm from '../components/MeasurementForm'
|
import MeasurementForm from '../components/MeasurementForm'
|
||||||
import { Measurement } from '../types'
|
import { Measurement } from '../types'
|
||||||
import { initDatabase, insertMeasurement, getLatestMeasurement } from '../database/db'
|
import { initDatabase, insertMeasurement, getLatestMeasurement, getMeasurementByDate, upsertMeasurement } from '../database/db'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
navigation: any
|
navigation: any
|
||||||
|
|
@ -29,7 +29,32 @@ export default function NewMeasurementScreen({ navigation }: Props) {
|
||||||
|
|
||||||
const handleSubmit = async (m: Measurement) => {
|
const handleSubmit = async (m: Measurement) => {
|
||||||
await initDatabase()
|
await initDatabase()
|
||||||
await insertMeasurement(m)
|
const existing = await getMeasurementByDate(m.measuredAt)
|
||||||
|
if (existing) {
|
||||||
|
Alert.alert(
|
||||||
|
'Dubbele datum',
|
||||||
|
`Er bestaat al een meting van ${new Date(m.measuredAt).toLocaleDateString('nl-NL')}. Overschrijven?`,
|
||||||
|
[
|
||||||
|
{ text: 'Annuleren', style: 'cancel' },
|
||||||
|
{
|
||||||
|
text: 'Overschrijven',
|
||||||
|
style: 'destructive',
|
||||||
|
onPress: () => doSave(m, existing.id),
|
||||||
|
},
|
||||||
|
]
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
doSave(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
const doSave = async (m: Measurement, existingId?: string) => {
|
||||||
|
if (existingId) {
|
||||||
|
m.id = existingId
|
||||||
|
await upsertMeasurement(m)
|
||||||
|
} else {
|
||||||
|
await insertMeasurement(m)
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const { syncAll } = await import('../sync/syncService')
|
const { syncAll } = await import('../sync/syncService')
|
||||||
await syncAll()
|
await syncAll()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue