@@ -5,13 +5,23 @@ type action =
55 | AuthorChanged (string )
66 | ExcerptChanged (string )
77 | SourceChanged (string )
8- | PageChanged (string );
8+ | PageChanged (string )
9+ | FormSubmitted
10+ | AddExcerptRequestSucceeded (Excerpt_t . t )
11+ | AddExcerptRequestFailed (string );
12+
13+ type submissionState =
14+ | NotAsked
15+ | Loading
16+ | Success (Excerpt_t . t )
17+ | Error (string );
918
1019type state = {
1120 author: string ,
1221 excerpt: string ,
1322 source: string ,
1423 page: string ,
24+ submissionState,
1525};
1626
1727let row = (left, right) =>
@@ -26,14 +36,26 @@ let reducer = (state, action) =>
2636 | ExcerptChanged (excerpt ) => {... state, excerpt}
2737 | SourceChanged (source ) => {... state, source}
2838 | PageChanged (page ) => {... state, page}
39+ | FormSubmitted => {... state, submissionState: Loading }
40+ | AddExcerptRequestSucceeded (excerpt ) => {
41+ ... state,
42+ submissionState: Success (excerpt),
43+ }
44+ | AddExcerptRequestFailed (msg ) => {... state, submissionState: Error (msg)}
2945 };
3046
3147[@ react . component ]
3248let make = () => {
3349 let (state , dispatch ) =
3450 React . useReducer(
3551 reducer,
36- {author: "" , excerpt: "" , source: "" , page: "" },
52+ {
53+ author: "" ,
54+ excerpt: "" ,
55+ source: "" ,
56+ page: "" ,
57+ submissionState: NotAsked ,
58+ },
3759 );
3860
3961 let txtInput = (name, value, _onChange) =>
@@ -82,54 +104,89 @@ let make = () => {
82104 );
83105
84106 <PageContainer >
85- <>
86- <h1 className= "font-semibold text-xl tracking-tight mb-8" >
87- {React . string("Add new excerpt" )}
88- </h1 >
89- {<Form
90- onSubmit= {e => {
91- ReactEvent . Form . preventDefault(e);
92- Client . request(
93- ~method_= Post ,
94- ~input= {
95- Excerpt_bs . write_t({
96- author: state. author,
97- excerpt: state. excerpt,
98- source: state. source,
99- page: Some (state. page),
100- });
101- },
102- Routes . sprintf(Router . ApiRoutes . add_excerpt() ),
103- a =>
104- a
105- )
106- |> Js . Promise . then_(res =>
107- Js . log2("received" , res)-> Js . Promise . resolve
107+ {switch (state. submissionState) {
108+ | Loading =>
109+ <>
110+ <h1 className= "font-semibold text-xl tracking-tight mb-8" >
111+ {React . string("Add new excerpt" )}
112+ </h1 >
113+ <p > {React . string("Loading...." )} </p >
114+ </>
115+ | Error (msg ) =>
116+ <>
117+ <h1 className= "font-semibold text-xl tracking-tight mb-8" >
118+ {React . string("Add new excerpt" )}
119+ </h1 >
120+ <p > {React . string("Error: " ++ msg)} </p >
121+ </>
122+ | Success (e ) =>
123+ <>
124+ <h1 className= "font-semibold text-xl tracking-tight mb-8" >
125+ {React . string("Excerpt added successfully" )}
126+ </h1 >
127+ <p > {React . string("Added the following excerpt: " )} </p >
128+ <Excerpt e />
129+ <Link url= "/" txt= "Back home" />
130+ </>
131+ | NotAsked =>
132+ <>
133+ <h1 className= "font-semibold text-xl tracking-tight mb-8" >
134+ {React . string("Add new excerpt" )}
135+ </h1 >
136+ {<Form
137+ onSubmit= {e => {
138+ ReactEvent . Form . preventDefault(e);
139+ dispatch @@ FormSubmitted ;
140+ Client . request(
141+ ~method_= Post ,
142+ ~input= {
143+ Excerpt_bs . write_t({
144+ author: state. author,
145+ excerpt: state. excerpt,
146+ source: state. source,
147+ page: Some (state. page),
148+ });
149+ },
150+ Routes . sprintf(Router . ApiRoutes . add_excerpt() ),
151+ Excerpt_bs . read_t,
108152 )
109- |> ignore ;
110- Js . log(state);
111- }}>
112- {List . mapi(
113- (_i, x) =>
114- <Div cls= "md:flex md:items-center mb-6" key= {string_of_int(_i)}>
115- x
116- </Div >,
117- [
118- txtInput("author" , state. author, author =>
119- dispatch @@ AuthorChanged (author)
120- ),
121- excerptInput,
122- txtInput("source" , state. source, author =>
123- dispatch @@ SourceChanged (author)
124- ),
125- txtInput("page" , state. page, author =>
126- dispatch @@ PageChanged (author)
127- ),
128- submit,
129- ] ,
130- )
131- |> React . list}
132- </Form >}
133- </>
153+ |> Js . Promise . then_(res =>
154+ (
155+ switch (res) {
156+ | Ok (excerpt ) =>
157+ dispatch @@ AddExcerptRequestSucceeded (excerpt)
158+ | Error (err ) =>
159+ dispatch @@
160+ AddExcerptRequestFailed (Client . errorToString(err))
161+ }
162+ )
163+ -> Js . Promise . resolve
164+ )
165+ |> ignore ;
166+ }}>
167+ {List . mapi(
168+ (_i, x) =>
169+ <Div
170+ cls= "md:flex md:items-center mb-6" key= {string_of_int(_i)}>
171+ x
172+ </Div >,
173+ [
174+ txtInput("author" , state. author, author =>
175+ dispatch @@ AuthorChanged (author)
176+ ),
177+ excerptInput,
178+ txtInput("source" , state. source, author =>
179+ dispatch @@ SourceChanged (author)
180+ ),
181+ txtInput("page" , state. page, author =>
182+ dispatch @@ PageChanged (author)
183+ ),
184+ submit,
185+ ] ,
186+ )
187+ |> React . list}
188+ </Form >}
189+ </>
190+ }}
134191 </PageContainer >;
135192};
0 commit comments