|
1 |
| -import { deleteCarbs } from '@/actions/delete-carbs' |
2 |
| -import { validateRequest } from '@/auth' |
3 |
| -import { CarbDialog } from '@/components/carb-dialog' |
4 |
| -import { ClientDateTime } from '@/components/client-datetime' |
5 |
| -import { DeleteDialog } from '@/components/delete-dialog' |
6 |
| -import { PageDatePicker } from '@/components/page-date-picker' |
7 |
| -import { Button } from '@/components/ui/button' |
8 |
| -import { Progress } from '@/components/ui/progress' |
9 |
| -import { Separator } from '@/components/ui/separator' |
10 |
| -import { Statistics } from '@/lib/sql_utils' |
11 |
| -import { addHours, endOfDay, isValid, parseISO, startOfDay } from 'date-fns' |
12 |
| -import { Pencil } from 'lucide-react' |
13 |
| -import { notFound, redirect } from 'next/navigation' |
14 |
| -import * as React from 'react' |
| 1 | +import { CarbsList } from './carbs-list' |
15 | 2 |
|
16 |
| -interface Props { |
17 |
| - date: Date |
18 |
| -} |
19 |
| - |
20 |
| -async function ListCarbTable({ date }: Props) { |
21 |
| - const { user } = await validateRequest() |
22 |
| - if (!user) { |
23 |
| - redirect('/login') |
24 |
| - } |
25 |
| - |
26 |
| - if (!user) redirect('/login') |
27 |
| - const now = new Date() |
28 |
| - |
29 |
| - const start = date |
30 |
| - const end = addHours(date, 24) |
31 |
| - |
32 |
| - const tf = Statistics.carbs_timeframe(user.id, start, end) |
33 |
| - let carbs = await Statistics.execute( |
34 |
| - Statistics.attributed_carbs_simple( |
35 |
| - tf, |
36 |
| - user.id, |
37 |
| - user.carbohydrateRatio, |
38 |
| - user.correctionRatio |
39 |
| - ) |
40 |
| - ) |
41 |
| - |
42 |
| - carbs = carbs |
43 |
| - .filter( |
44 |
| - (carb) => |
45 |
| - carb.id !== -1 && |
46 |
| - carb.timestamp.getTime() >= start.getTime() && |
47 |
| - carb.timestamp.getTime() <= end.getTime() |
48 |
| - ) |
49 |
| - .sort((a, b) => b.timestamp.getTime() - a.timestamp.getTime()) |
50 |
| - |
51 |
| - return ( |
52 |
| - <div> |
53 |
| - {carbs.map((carb, i, arr) => { |
54 |
| - const over = carb.observedCarbs > carb.carbs |
55 |
| - |
56 |
| - return ( |
57 |
| - <div key={carb.id}> |
58 |
| - <div className="p-2"> |
59 |
| - <div className="grid grid-cols-2 pb-2"> |
60 |
| - <div> |
61 |
| - <div>{Math.round(carb.carbs)} g </div> |
62 |
| - <div>Observed: {Math.round(carb.observedCarbs)} g </div> |
63 |
| - </div> |
64 |
| - <div className="flex items-center gap-2 justify-end"> |
65 |
| - <div> |
66 |
| - <ClientDateTime timestamp={carb.timestamp} /> +{' '} |
67 |
| - {(carb.decay / 60).toLocaleString([], { |
68 |
| - maximumFractionDigits: 1, |
69 |
| - })} |
70 |
| - <span> h</span> |
71 |
| - </div> |
72 |
| - <div> |
73 |
| - <CarbDialog carb={carb}> |
74 |
| - <Button variant="ghost" className="p-2"> |
75 |
| - <Pencil className="cursor-pointer w-4 h-4" /> |
76 |
| - </Button> |
77 |
| - </CarbDialog> |
78 |
| - <DeleteDialog id={carb.id} action={deleteCarbs} /> |
79 |
| - </div> |
80 |
| - </div> |
81 |
| - </div> |
82 |
| - {over ? ( |
83 |
| - <Progress |
84 |
| - className="h-2 bg-orange-400" |
85 |
| - value={(carb.carbs / carb.observedCarbs) * 100} |
86 |
| - /> |
87 |
| - ) : ( |
88 |
| - <Progress |
89 |
| - className="h-2 " |
90 |
| - value={(carb.observedCarbs / carb.carbs) * 100} |
91 |
| - /> |
92 |
| - )} |
93 |
| - </div> |
94 |
| - {arr.length - 1 !== i && <Separator />} |
95 |
| - </div> |
96 |
| - ) |
97 |
| - })} |
98 |
| - {carbs.length === 0 && ( |
99 |
| - <p className="text-center p-2 text-slate-400">No entries</p> |
100 |
| - )} |
101 |
| - </div> |
102 |
| - ) |
103 |
| -} |
104 |
| - |
105 |
| -export default async function InsulinListByDate({ |
106 |
| - searchParams, |
107 |
| -}: { |
108 |
| - searchParams: { date: string } |
109 |
| -}) { |
110 |
| - const date = searchParams.date ? parseISO(searchParams.date) : new Date() |
111 |
| - if (!isValid(date)) { |
112 |
| - notFound() |
113 |
| - } |
114 |
| - |
115 |
| - return ( |
116 |
| - <> |
117 |
| - <PageDatePicker date={date} /> |
118 |
| - <div className="space-y-4 border bg-white max-w-5xl mx-auto rounded-md shadow p-2 mt-4"> |
119 |
| - <ListCarbTable date={date} /> |
120 |
| - </div> |
121 |
| - </> |
122 |
| - ) |
123 |
| -} |
| 3 | +export default CarbsList |
0 commit comments