import { useEffect, useState } from 'react'
import { api } from '../lib/api.js'
/** UseCase1002 - log a meal and see the daily totals. */
export default function Planner() {
const [entries, setEntries] = useState([])
const [feed, setFeed] = useState([])
const [daily, setDaily] = useState([])
const [macros, setMacros] = useState([])
const [error, setError] = useState(null)
const [pick, setPick] = useState([])
const [notes, setNotes] = useState('')
const today = new Date().toISOString().slice(0, 10)
const weekAgo = new Date(Date.now() - 7 * 864e5).toISOString().slice(0, 10)
const load = () => {
api.get('/planner').then(setEntries).catch(e => setError(e.message))
api.get(`/planner/daily?from=${weekAgo}&to=${today}`).then(setDaily).catch(() => {})
api.get(`/planner/macros?from=${weekAgo}&to=${today}`).then(setMacros).catch(() => {})
}
useEffect(() => {
load()
api.get('/posts/feed').then(setFeed).catch(() => {})
}, [])
const log = async () => {
setError(null)
try {
await api.post('/planner', {
notes,
postIds: pick.map(Number),
consumed: true
})
setNotes('')
setPick([])
load()
} catch (e) { setError(e.message) }
}
return (
{error &&
{error}
}
Last 7 days
| day | kcal | meals |
{daily.map(d => (
| {d.day} | {d.kcalConsumed} | {d.mealsLogged} |
))}
{daily.length === 0 &&
Nothing logged in the last week.
}
{macros.length > 0 && (
Macronutrients supplied this week
{macros.map(m => (
| {m.nutrient} | {m.total} {m.unit} |
))}
)}
Diary
{entries.map(e => (
| {new Date(e.dateTime).toLocaleString()} |
{e.notes} |
{e.meals.join(', ')} |
{e.kcal} |
{e.consumed ? 'eaten' : 'planned'} |
))}
)
}