@@ -8,7 +8,7 @@ export async function action({ request }: ActionArgs) {
88 const formData = await request . formData ( ) ;
99 const intent = formData . get ( "intent" ) ;
1010
11- if ( intent === "update " ) {
11+ if ( intent === "add " ) {
1212 await db . insert ( example ) . values ( { } ) ;
1313 return new Response ( null , { status : 201 } ) ;
1414 }
@@ -21,38 +21,41 @@ export async function action({ request }: ActionArgs) {
2121 return new Response ( null , { status : 400 } ) ;
2222}
2323
24- export function loader ( ) {
25- const result = db
26- . select ( {
27- count : sql < number > `COUNT(*)` ,
28- lastUpdated : sql < string > `MAX(created_at)` ,
29- } )
24+ export async function loader ( ) {
25+ const rowsQuery = db . select ( ) . from ( example ) ;
26+
27+ const lastUpdatedQuery = db
28+ . select ( { updated : sql < string > `MAX(created_at)` } )
3029 . from ( example )
31- . get ( ) ;
30+ . execute ( )
31+ . then ( ( rows ) => rows [ 0 ] . updated ) ;
3232
3333 return {
34- count : result ?. count ?? 0 ,
35- lastUpdated : result ?. lastUpdated ?? "never" ,
34+ rows : await rowsQuery ,
35+ lastUpdated : await lastUpdatedQuery ,
3636 } ;
3737}
3838
3939export default function Index ( ) {
40- const { count, lastUpdated } = useLoaderData < typeof loader > ( ) ;
40+ const { rows, lastUpdated } = useLoaderData < typeof loader > ( ) ;
41+
4142 return (
4243 < div style = { { fontFamily : "system-ui, sans-serif" , lineHeight : "1.8" } } >
4344 < h1 > Welcome to Remix</ h1 >
44- < p >
45- Count: { count } < br />
46- Last updated: { lastUpdated }
47- </ p >
45+ < p > Last Updated: { lastUpdated ?? "--" } </ p >
4846 < Form method = "post" >
49- < button type = "submit" name = "intent" value = "update " >
50- Update
47+ < button type = "submit" name = "intent" value = "add " >
48+ Add Row
5149 </ button >
5250 < button type = "submit" name = "intent" value = "reset" >
5351 Reset
5452 </ button >
5553 </ Form >
54+ < ul >
55+ { rows . map ( ( row ) => (
56+ < li key = { row . id } > { row . id } </ li >
57+ ) ) }
58+ </ ul >
5659 </ div >
5760 ) ;
5861}
0 commit comments