fix: code review cleanup — filter deletedAt, deduplicate, use toast util
- getMeasurementByDate: AND deletedAt IS NULL (soft-deleted meting triggerde dubbele-datum alert) - Dashboard/History: verwijder redundante JS .filter(m => !m.deletedAt) (SQL filterde al) - HistoryScreen confirmDelete: fix dubbele load() call - NewMeasurementScreen: gebruik toast utility i.p.v. direct ToastAndroid
This commit is contained in:
parent
4c34dff629
commit
a32b9e0a88
4 changed files with 12 additions and 16 deletions
|
|
@ -77,7 +77,7 @@ export async function getMeasurementsSince(date: string): Promise<Measurement[]>
|
||||||
export async function getMeasurementByDate(date: string): Promise<Measurement | null> {
|
export async function getMeasurementByDate(date: string): Promise<Measurement | null> {
|
||||||
const day = date.slice(0, 10)
|
const day = date.slice(0, 10)
|
||||||
const rows = await db!.getAllAsync<any>(
|
const rows = await db!.getAllAsync<any>(
|
||||||
"SELECT * FROM measurements WHERE measuredAt LIKE ? LIMIT 1",
|
"SELECT * FROM measurements WHERE measuredAt LIKE ? AND deletedAt IS NULL LIMIT 1",
|
||||||
[`${day}%`]
|
[`${day}%`]
|
||||||
)
|
)
|
||||||
return rows.length > 0 ? (rows[0] as Measurement) : null
|
return rows.length > 0 ? (rows[0] as Measurement) : null
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ export default function DashboardScreen({ navigation }: Props) {
|
||||||
const load = async () => {
|
const load = async () => {
|
||||||
// 1. Laad alleen laatste 2 metingen voor de kaart (snel!)
|
// 1. Laad alleen laatste 2 metingen voor de kaart (snel!)
|
||||||
await loadLatest(2)
|
await loadLatest(2)
|
||||||
setAll(getCached().filter(m => !m.deletedAt))
|
setAll(getCached())
|
||||||
|
|
||||||
// 2. Laad rest van de data op achtergrond voor grafieken + sync
|
// 2. Laad rest van de data op achtergrond voor grafieken + sync
|
||||||
const loadRest = async () => {
|
const loadRest = async () => {
|
||||||
|
|
@ -28,7 +28,7 @@ export default function DashboardScreen({ navigation }: Props) {
|
||||||
await loadSince(cutoffDate.toISOString())
|
await loadSince(cutoffDate.toISOString())
|
||||||
|
|
||||||
// Update UI met grafiek-data (nog geen sync)
|
// Update UI met grafiek-data (nog geen sync)
|
||||||
setAll(getCached().filter(m => !m.deletedAt))
|
setAll(getCached())
|
||||||
|
|
||||||
// Sync met server (achtergrond)
|
// Sync met server (achtergrond)
|
||||||
try {
|
try {
|
||||||
|
|
@ -40,7 +40,7 @@ export default function DashboardScreen({ navigation }: Props) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update UI met eventuele nieuwe data van sync
|
// Update UI met eventuele nieuwe data van sync
|
||||||
setAll(getCached().filter(m => !m.deletedAt))
|
setAll(getCached())
|
||||||
}
|
}
|
||||||
loadRest()
|
loadRest()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ export default function HistoryScreen({ navigation }: Props) {
|
||||||
const load = useCallback(async () => {
|
const load = useCallback(async () => {
|
||||||
await initDatabase()
|
await initDatabase()
|
||||||
const all = await getAllMeasurements()
|
const all = await getAllMeasurements()
|
||||||
setMeasurements(all.filter(m => !m.deletedAt))
|
setMeasurements(all)
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
useFocusEffect(
|
useFocusEffect(
|
||||||
|
|
@ -72,11 +72,10 @@ export default function HistoryScreen({ navigation }: Props) {
|
||||||
await createApi(config).deleteMeasurement(m.id)
|
await createApi(config).deleteMeasurement(m.id)
|
||||||
await removeMeasurement(m.id)
|
await removeMeasurement(m.id)
|
||||||
}
|
}
|
||||||
|
load()
|
||||||
} catch {
|
} catch {
|
||||||
await load()
|
load()
|
||||||
return
|
|
||||||
}
|
}
|
||||||
load()
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
import React, { useCallback, useEffect, useState } from 'react'
|
import React, { useCallback, useState } from 'react'
|
||||||
import { View, Text, Alert, Platform, ToastAndroid } from 'react-native'
|
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, upsertMeasurement, getLatestMeasurement, getMeasurementByDate } from '../database/db'
|
import { initDatabase, insertMeasurement, upsertMeasurement, getLatestMeasurement, getMeasurementByDate } from '../database/db'
|
||||||
import { updateCache } from '../store/measurementStore'
|
import { updateCache } from '../store/measurementStore'
|
||||||
|
import { showSuccess, showError } from '../utils/toast'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
navigation: any
|
navigation: any
|
||||||
|
|
@ -81,13 +82,9 @@ export default function NewMeasurementScreen({ navigation, route }: Props) {
|
||||||
try {
|
try {
|
||||||
const { uploadMeasurement } = await import('../sync/syncService')
|
const { uploadMeasurement } = await import('../sync/syncService')
|
||||||
await uploadMeasurement(m)
|
await uploadMeasurement(m)
|
||||||
if (Platform.OS === 'android') {
|
showSuccess('Opgeslagen & gesynct')
|
||||||
ToastAndroid.show('Opgeslagen & gesynct', ToastAndroid.SHORT)
|
|
||||||
}
|
|
||||||
} catch {
|
} catch {
|
||||||
if (Platform.OS === 'android') {
|
showError('Opgeslagen (sync mislukt)')
|
||||||
ToastAndroid.show('Opgeslagen (sync mislukt)', ToastAndroid.LONG)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue