1
+ /** @format */
2
+
1
3
import type { FormEvent } from "react" ;
2
4
import { Head , useData } from "aleph/react" ;
3
5
@@ -14,9 +16,9 @@ const store = {
14
16
} ,
15
17
} ;
16
18
17
- export function data ( ) {
19
+ export const data = ( ) => {
18
20
return Response . json ( store ) ;
19
- }
21
+ } ;
20
22
21
23
export async function mutation ( req : Request ) : Promise < Response > {
22
24
const { id, message, completed } = await req . json ( ) ;
@@ -43,24 +45,34 @@ export async function mutation(req: Request): Promise<Response> {
43
45
}
44
46
45
47
export default function Todos ( ) {
46
- const { data : { todos } , isMutating, mutation } = useData < { todos : Todo [ ] } > ( ) ;
48
+ const {
49
+ data : { todos } ,
50
+ isMutating,
51
+ mutation,
52
+ } = useData < { todos : Todo [ ] } > ( ) ;
47
53
48
54
const onSubmit = async ( e : FormEvent < HTMLFormElement > ) => {
49
55
e . preventDefault ( ) ;
50
56
const form = e . currentTarget ;
51
57
const fd = new FormData ( form ) ;
52
58
const message = fd . get ( "message" ) ?. toString ( ) . trim ( ) ;
53
59
if ( message ) {
54
- await mutation . put ( { message } , {
55
- // optimistic update data without waiting for the server response
56
- optimisticUpdate : ( data ) => {
57
- return {
58
- todos : [ ...data . todos , { id : 0 , message, completed : false } ] ,
59
- } ;
60
+ await mutation . put (
61
+ { message } ,
62
+ {
63
+ // optimistic update data without waiting for the server response
64
+ optimisticUpdate : ( data ) => {
65
+ return {
66
+ todos : [
67
+ ...data . todos ,
68
+ { id : 0 , message, completed : false } ,
69
+ ] ,
70
+ } ;
71
+ } ,
72
+ // replace the data with the new data that is from the server response
73
+ replace : true ,
60
74
} ,
61
- // replace the data with the new data that is from the server response
62
- replace : true ,
63
- } ) ;
75
+ ) ;
64
76
setTimeout ( ( ) => form . querySelector ( "input" ) ?. focus ( ) , 0 ) ;
65
77
form . reset ( ) ;
66
78
}
@@ -70,25 +82,39 @@ export default function Todos() {
70
82
< div className = "todos-app" >
71
83
< Head >
72
84
< title > Todos</ title >
73
- < meta name = "description" content = "A todos app powered by Aleph.js" />
85
+ < meta
86
+ name = "description"
87
+ content = "A todos app powered by Aleph.js"
88
+ />
74
89
</ Head >
75
90
< h1 >
76
91
< span > Todos</ span >
77
- { todos . length > 0 && < em > { todos . filter ( ( todo ) => todo . completed ) . length } /{ todos . length } </ em > }
92
+ { todos . length > 0 && (
93
+ < em >
94
+ { todos . filter ( ( todo ) => todo . completed ) . length } /
95
+ { todos . length }
96
+ </ em >
97
+ ) }
78
98
</ h1 >
79
99
< ul >
80
100
{ todos . map ( ( todo ) => (
81
101
< li key = { todo . id } >
82
102
< input
83
103
type = "checkbox"
84
104
checked = { todo . completed }
85
- onChange = { ( ) => mutation . patch ( { id : todo . id , completed : ! todo . completed } , "replace" ) }
105
+ onChange = { ( ) =>
106
+ mutation . patch (
107
+ { id : todo . id , completed : ! todo . completed } ,
108
+ "replace" ,
109
+ ) }
86
110
/>
87
111
< label className = { todo . completed ? "completed" : "" } >
88
112
{ todo . message }
89
113
</ label >
90
114
{ todo . id > 0 && (
91
- < button onClick = { ( ) => mutation . delete ( { id : todo . id } , "replace" ) } >
115
+ < button
116
+ onClick = { ( ) => mutation . delete ( { id : todo . id } , "replace" ) }
117
+ >
92
118
< svg
93
119
viewBox = "0 0 32 32"
94
120
fill = "none"
0 commit comments