Skip to content

Commit dc46957

Browse files
committed
move counter to its own section
1 parent ed21496 commit dc46957

File tree

6 files changed

+51
-19
lines changed

6 files changed

+51
-19
lines changed

client/src/App.re

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ module Handlers = {
5858
{(authors => <PageAuthorExcerpts authors />)}
5959
</Client.FetchRender>
6060
};
61+
62+
let counter = _req => <PageCounter />;
6163
};
6264
module Api = {
6365
// Api routes should never be loaded from React app, show the backing page in case it happens

server/lib/handlers.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ module Handlers = struct
9595
page_with_payload
9696
(Shared.PageAuthorExcerpts_j.string_of_payload authors)
9797
[ Shared.PageAuthorExcerpts.make ~authors ]
98+
99+
let counter _req = respond' @@ basic_page [ Shared.PageCounter.make () ]
98100
end
99101

100102
module Api = struct

shared/PageCounter.re

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
open Bridge;
2+
3+
[@react.component]
4+
let make = () => {
5+
let (count, setCount) = React.useState(() => 0);
6+
React.useEffect1(
7+
() => {
8+
let timer =
9+
Js.Global.setTimeout(() => {setCount(count => count + 1)}, 1000);
10+
Some(() => Js.Global.clearTimeout(timer));
11+
},
12+
[|count|],
13+
);
14+
15+
<PageContainer>
16+
<>
17+
<h1 key="header"> {React.string("Counter")} </h1>
18+
<p key="desc">
19+
{React.string(
20+
"The HTML (including counter value) comes first from the OCaml native server"
21+
++ " then is updated by React after hydration",
22+
)}
23+
</p>
24+
<p key="counter"> {React.string(string_of_int(count))} </p>
25+
</>
26+
</PageContainer>;
27+
};

shared/PageExcerpts.re

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,8 @@ open Bridge;
22

33
[@react.component]
44
let make = (~excerpts: list(Excerpt_t.t)) => {
5-
let (count, setCount) = React.useState(() => 0);
6-
React.useEffect1(
7-
() => {
8-
let timer =
9-
Js.Global.setTimeout(() => {setCount(count => count + 1)}, 1000);
10-
Some(() => Js.Global.clearTimeout(timer));
11-
},
12-
[|count|],
13-
);
14-
155
let children = [
166
<h1 key="header"> {React.string("Excerpts")} </h1>,
17-
<p key="desc">
18-
{React.string(
19-
"The HTML (including counter value) comes first from the OCaml native server"
20-
++ " then is updated by React after hydration",
21-
)}
22-
</p>,
23-
<p key="counter"> {React.string(string_of_int(count))} </p>,
247
...List.mapi(
258
(_i, e) => <Excerpt key={string_of_int(_i)} e />,
269
excerpts,

shared/PageWelcome.re

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ let make = () => {
2020
|> React.list}
2121
</Dom.Ul>
2222
<h2> {React.string("Excerpts")} </h2>
23-
<Dom.Ul cls="list-disc list-inside">
23+
<Dom.Ul cls="list-disc list-inside mb-8">
2424
{List.mapi(
2525
(_i, x) => <li key={string_of_int(_i)}> x </li>,
2626
[
@@ -33,5 +33,16 @@ let make = () => {
3333
)
3434
|> React.list}
3535
</Dom.Ul>
36+
<h2> {React.string("Other examples")} </h2>
37+
<Dom.Ul cls="list-disc list-inside mb-8">
38+
<>
39+
<li>
40+
<Link
41+
url={Routes.sprintf(Router.PageRoutes.counter())}
42+
txt="Counter"
43+
/>
44+
</li>
45+
</>
46+
</Dom.Ul>
3647
</PageContainer>;
3748
};

shared/router.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ module type T = sig
1919
val excerpts_by_author : string -> request -> response
2020

2121
val authors_with_excerpts : request -> response
22+
23+
val counter : request -> response
2224
end
2325

2426
module Api : sig
@@ -30,7 +32,11 @@ module type T = sig
3032
end
3133
end
3234

33-
module PageRoutes = struct let authors_with_excerpts () = s "authors" /? nil end
35+
module PageRoutes = struct
36+
let authors_with_excerpts () = s "authors" /? nil
37+
38+
let counter () = s "counter" /? nil
39+
end
3440

3541
module ApiRoutes = struct
3642
let authors_with_excerpts () = s "api" / s "authors" /? nil
@@ -52,6 +58,7 @@ module Make (Handlers : T) = struct
5258
( `GET,
5359
(s "excerpts" / s "author" / str /? nil) @--> Pages.excerpts_by_author );
5460
`GET, PageRoutes.authors_with_excerpts () @--> Pages.authors_with_excerpts;
61+
`GET, PageRoutes.counter () @--> Pages.counter;
5562
`GET, ApiRoutes.authors_with_excerpts () @--> Api.authors_with_excerpts;
5663
`GET, ApiRoutes.excerpts_by_author () @--> Api.excerpts_by_author;
5764
`POST, ApiRoutes.add_excerpt () @--> Api.add_excerpt;

0 commit comments

Comments
 (0)