TICKET-008 (frontend): Foods library view — search, pagination, edit, delete, restore
This commit is contained in:
@@ -121,6 +121,9 @@
|
||||
let saving = $state(false)
|
||||
let error = $state(null)
|
||||
|
||||
// Edit mode: an existing food (with id) was passed in — update, don't create.
|
||||
let isEdit = $derived(!!food?.id)
|
||||
|
||||
// ── Pre-fill from food prop ─────────────────────────────────────────────
|
||||
$effect(() => {
|
||||
if (food) {
|
||||
@@ -176,12 +179,21 @@
|
||||
serving_size_g: servingSizeG ? parseFloat(servingSizeG) : null,
|
||||
serving_name: servingName.trim() || null,
|
||||
source,
|
||||
is_meal: false,
|
||||
is_meal: food?.is_meal ?? false,
|
||||
}
|
||||
// Include barcode when present
|
||||
if (barcode.trim()) payload.barcode = barcode.trim()
|
||||
else payload.barcode = null
|
||||
|
||||
if (isEdit) {
|
||||
const saved = await api.updateFood(food.id, payload)
|
||||
// Nutrition edits affect historical log rendering — refresh the day
|
||||
await refreshDayData()
|
||||
if (onSaved) onSaved(saved)
|
||||
else appView.current = 'dashboard'
|
||||
return
|
||||
}
|
||||
|
||||
const saved = await api.createFood(payload)
|
||||
// After save, offer to log the new food
|
||||
justSaved = { food: saved }
|
||||
@@ -224,10 +236,10 @@
|
||||
|
||||
function goBack() {
|
||||
reset()
|
||||
if (mealId) {
|
||||
appView.current = 'dashboard'
|
||||
} else if (onCancel) {
|
||||
if (onCancel) {
|
||||
onCancel()
|
||||
} else if (mealId) {
|
||||
appView.current = 'dashboard'
|
||||
} else if (onSaved) {
|
||||
// Came from scan flow with no cancel — go to dashboard
|
||||
appView.current = 'dashboard'
|
||||
@@ -355,7 +367,7 @@
|
||||
|
||||
{:else}
|
||||
<!-- Creation/edit form -->
|
||||
<h3>{isPrefilled ? 'Confirm & edit food' : 'Create food'}</h3>
|
||||
<h3>{isEdit ? 'Edit food' : isPrefilled ? 'Confirm & edit food' : 'Create food'}</h3>
|
||||
|
||||
<form onsubmit={handleSubmit}>
|
||||
<label>
|
||||
@@ -436,7 +448,7 @@
|
||||
|
||||
<div class="form-actions">
|
||||
<button type="submit" disabled={saving}>
|
||||
{saving ? 'Saving…' : 'Save food'}
|
||||
{saving ? 'Saving…' : isEdit ? 'Save changes' : 'Save food'}
|
||||
</button>
|
||||
<button type="button" class="secondary" onclick={goBack} disabled={saving}>
|
||||
Cancel
|
||||
|
||||
Reference in New Issue
Block a user