73 lines
2.7 KiB
React
73 lines
2.7 KiB
React
import { useState } from 'react'
|
|
import apiClient from '../api/client'
|
|
import MealTypeSelect from './MealTypeSelect'
|
|
import { parseDecimal } from '../utils/numbers'
|
|
|
|
export default function EditLogModal({ log, onSaved, onClose }) {
|
|
const [amountG, setAmountG] = useState(String(log.amount_g))
|
|
const [mealType, setMealType] = useState(log.meal_type)
|
|
const [error, setError] = useState('')
|
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
|
|
|
async function handleSubmit(e) {
|
|
e.preventDefault()
|
|
setError('')
|
|
setIsSubmitting(true)
|
|
try {
|
|
await apiClient.put(`/api/logs/${log.id}`, {
|
|
amount_g: parseDecimal(amountG),
|
|
meal_type: mealType,
|
|
})
|
|
onSaved()
|
|
} catch {
|
|
setError('Eintrag konnte nicht gespeichert werden.')
|
|
} finally {
|
|
setIsSubmitting(false)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="fixed inset-0 z-50 flex items-end justify-center bg-black/50 sm:items-center">
|
|
<div className="w-full max-w-sm rounded-t-2xl bg-white p-6 sm:rounded-2xl dark:bg-gray-800">
|
|
<h2 className="text-lg font-semibold text-gray-900 dark:text-gray-50">{log.name}</h2>
|
|
<p className="mb-4 text-sm text-gray-500 dark:text-gray-400">Eintrag bearbeiten</p>
|
|
|
|
<form onSubmit={handleSubmit} className="space-y-4">
|
|
<MealTypeSelect value={mealType} onChange={setMealType} />
|
|
<div>
|
|
<label className="mb-1 block text-sm font-medium text-gray-700 dark:text-gray-300">
|
|
Menge (Gramm)
|
|
</label>
|
|
<input
|
|
type="text"
|
|
inputMode="decimal"
|
|
value={amountG}
|
|
onChange={(e) => setAmountG(e.target.value)}
|
|
required
|
|
autoFocus
|
|
className="w-full rounded-lg border border-gray-300 px-3 py-2 text-base text-gray-900 focus:border-emerald-500 focus:outline-none dark:border-gray-600 dark:bg-gray-700 dark:text-gray-50"
|
|
/>
|
|
</div>
|
|
{error && <p className="text-sm text-red-600 dark:text-red-400">{error}</p>}
|
|
<div className="flex gap-3">
|
|
<button
|
|
type="button"
|
|
onClick={onClose}
|
|
className="flex-1 rounded-lg border border-gray-300 py-2.5 text-base font-medium text-gray-700 dark:border-gray-600 dark:text-gray-300"
|
|
>
|
|
Abbrechen
|
|
</button>
|
|
<button
|
|
type="submit"
|
|
disabled={isSubmitting}
|
|
className="flex-1 rounded-lg bg-emerald-600 py-2.5 text-base font-medium text-white active:bg-emerald-700 disabled:opacity-60"
|
|
>
|
|
{isSubmitting ? 'Speichern…' : 'Speichern'}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|