@@ -5,6 +5,7 @@ import { createFileRoute, Link } from "@tanstack/react-router";
55import { Effect } from "effect" ;
66import { startTransition , useEffect } from "react" ;
77import { useFood } from "../../lib/hooks/use-food" ;
8+ import { useMeal } from "../../lib/hooks/use-meal" ;
89import { RuntimeClient } from "../../lib/runtime-client" ;
910import { Storage } from "../../lib/storage" ;
1011
@@ -44,11 +45,16 @@ function RouteComponent() {
4445 workspaceId : workspace . workspaceId ,
4546 } ) ;
4647
48+ const { data : meals } = useMeal ( {
49+ workspaceId : workspace . workspaceId ,
50+ } ) ;
51+
4752 const [ , onBootstrap , bootstrapping ] = useActionEffect (
4853 RuntimeClient ,
4954 bootstrap
5055 ) ;
51- const [ , onAdd ] = useActionEffect ( RuntimeClient , ( formData : FormData ) =>
56+
57+ const [ , onAddFood ] = useActionEffect ( RuntimeClient , ( formData : FormData ) =>
5258 Effect . gen ( function * ( ) {
5359 const loroStorage = yield * Storage ;
5460
@@ -66,6 +72,24 @@ function RouteComponent() {
6672 } )
6773 ) ;
6874
75+ const [ , onAddMeal ] = useActionEffect ( RuntimeClient , ( formData : FormData ) =>
76+ Effect . gen ( function * ( ) {
77+ const loroStorage = yield * Storage ;
78+
79+ const foodId = formData . get ( "foodId" ) as string ;
80+ const quantity = formData . get ( "quantity" ) as string ;
81+
82+ yield * loroStorage . insertMeal ( {
83+ workspaceId : workspace . workspaceId ,
84+ value : {
85+ id : crypto . randomUUID ( ) ,
86+ foodId,
87+ quantity : parseInt ( quantity , 10 ) ,
88+ } ,
89+ } ) ;
90+ } )
91+ ) ;
92+
6993 useEffect ( ( ) => {
7094 const url = new URL ( "./src/workers/live.ts" , globalThis . origin ) ;
7195 const newWorker = new globalThis . Worker ( url , { type : "module" } ) ;
@@ -112,7 +136,7 @@ function RouteComponent() {
112136 { bootstrapping ? "Bootstrapping..." : "Bootstrap" }
113137 </ button >
114138
115- < form action = { onAdd } >
139+ < form action = { onAddFood } >
116140 < input type = "text" name = "name" />
117141 < input type = "number" name = "calories" min = { 1 } />
118142 < button type = "submit" > Add food</ button >
@@ -128,6 +152,26 @@ function RouteComponent() {
128152 </ div >
129153 ) ) }
130154 </ div >
155+
156+ < form action = { onAddMeal } >
157+ { ( data ?? [ ] ) . map ( ( food ) => (
158+ < div key = { food . id } >
159+ < input type = "radio" name = "foodId" id = { food . id } value = { food . id } />
160+ < label htmlFor = { food . id } > { food . name } </ label >
161+ </ div >
162+ ) ) }
163+ < input type = "number" name = "quantity" min = { 1 } />
164+ < button type = "submit" > Add meal</ button >
165+ </ form >
166+
167+ < div >
168+ { ( meals ?? [ ] ) . map ( ( meal ) => (
169+ < div key = { meal . id } >
170+ < p > Food: { meal . food ?. name } </ p >
171+ < p > Quantity: { meal . quantity } </ p >
172+ </ div >
173+ ) ) }
174+ </ div >
131175 </ div >
132176 ) ;
133177}
0 commit comments