@@ -8,7 +8,7 @@ export async function action({ request }: ActionArgs) {
8
8
const formData = await request . formData ( ) ;
9
9
const intent = formData . get ( "intent" ) ;
10
10
11
- if ( intent === "update " ) {
11
+ if ( intent === "add " ) {
12
12
await db . insert ( example ) . values ( { } ) ;
13
13
return new Response ( null , { status : 201 } ) ;
14
14
}
@@ -21,38 +21,41 @@ export async function action({ request }: ActionArgs) {
21
21
return new Response ( null , { status : 400 } ) ;
22
22
}
23
23
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)` } )
30
29
. from ( example )
31
- . get ( ) ;
30
+ . execute ( )
31
+ . then ( ( rows ) => rows [ 0 ] . updated ) ;
32
32
33
33
return {
34
- count : result ?. count ?? 0 ,
35
- lastUpdated : result ?. lastUpdated ?? "never" ,
34
+ rows : await rowsQuery ,
35
+ lastUpdated : await lastUpdatedQuery ,
36
36
} ;
37
37
}
38
38
39
39
export default function Index ( ) {
40
- const { count, lastUpdated } = useLoaderData < typeof loader > ( ) ;
40
+ const { rows, lastUpdated } = useLoaderData < typeof loader > ( ) ;
41
+
41
42
return (
42
43
< div style = { { fontFamily : "system-ui, sans-serif" , lineHeight : "1.8" } } >
43
44
< h1 > Welcome to Remix</ h1 >
44
- < p >
45
- Count: { count } < br />
46
- Last updated: { lastUpdated }
47
- </ p >
45
+ < p > Last Updated: { lastUpdated ?? "--" } </ p >
48
46
< Form method = "post" >
49
- < button type = "submit" name = "intent" value = "update " >
50
- Update
47
+ < button type = "submit" name = "intent" value = "add " >
48
+ Add Row
51
49
</ button >
52
50
< button type = "submit" name = "intent" value = "reset" >
53
51
Reset
54
52
</ button >
55
53
</ Form >
54
+ < ul >
55
+ { rows . map ( ( row ) => (
56
+ < li key = { row . id } > { row . id } </ li >
57
+ ) ) }
58
+ </ ul >
56
59
</ div >
57
60
) ;
58
61
}
0 commit comments