|
1 | 1 | /** The route handlers for our app */ |
2 | | -module Get = { |
3 | | - type request = string; |
| 2 | +module Handlers = { |
| 3 | + /** The payload available in the <script id="ocaml_webapp_page_payload" /> element */ |
| 4 | + type request = option(string); |
4 | 5 | type response = React.element; |
5 | 6 |
|
6 | | - /** Defines a handler that replies to requests at the root endpoint */ |
7 | | - let root = _req => <PageWelcome />; |
8 | | - |
9 | | - /** Defines a handler that takes a path parameter from the route */ |
10 | | - let hello = (lang, _req) => <PageHello lang />; |
11 | | - |
12 | | - /** Fallback handler in case the endpoint is called without a language parameter */ |
13 | | - let hello_fallback = _req => <PageHelloFallback />; |
14 | | - |
15 | | - let excerpts_add = _req => <PageAddExcerpt />; |
16 | | - // let excerpts_by_author = (name, req) => |
17 | | - // Lwt.( |
18 | | - // Db.Get.excerpts_by_author(name, req) |
19 | | - // >>= respond_or_err(Content.excerpts_listing_page) |
20 | | - // ); |
21 | | - // let excerpts = req => |
22 | | - // Lwt.( |
23 | | - // Db.Get.authors(req) >>= respond_or_err(Content.author_excerpts_page) |
24 | | - // ); |
| 7 | + module Pages = { |
| 8 | + /** Defines a handler that replies to requests at the root endpoint */ |
| 9 | + let root = _req => <PageWelcome />; |
| 10 | + |
| 11 | + /** Defines a handler that takes a path parameter from the route */ |
| 12 | + let hello = (lang, _req) => { |
| 13 | + <PageHello lang />; |
| 14 | + }; |
| 15 | + |
| 16 | + /** Fallback handler in case the endpoint is called without a language parameter */ |
| 17 | + let hello_fallback = _req => <PageHelloFallback />; |
| 18 | + |
| 19 | + let excerpts_add = _req => <PageAddExcerpt />; |
| 20 | + |
| 21 | + let excerpts_by_author = (authorName, payload) => |
| 22 | + switch (payload) { |
| 23 | + | Some(p) => |
| 24 | + switch (p->Js.Json.parseExn->PageExcerpts_bs.read_payload) { |
| 25 | + | excerpts => <PageExcerpts excerpts /> |
| 26 | + | exception _exn => |
| 27 | + Js.log("Couldn't parse excerpts from JSON payload " ++ p); |
| 28 | + <PageNotFound />; |
| 29 | + } |
| 30 | + | None => |
| 31 | + <Client.FetchRender |
| 32 | + url={Routes.sprintf( |
| 33 | + Router.ApiRoutes.excerpts_by_author(), |
| 34 | + authorName, |
| 35 | + )} |
| 36 | + decoder=PageExcerpts_bs.read_payload> |
| 37 | + {( |
| 38 | + excerpts => { |
| 39 | + <PageExcerpts excerpts />; |
| 40 | + } |
| 41 | + )} |
| 42 | + </Client.FetchRender> |
| 43 | + }; |
| 44 | + |
| 45 | + let authors_with_excerpts = payload => |
| 46 | + switch (payload) { |
| 47 | + | Some(p) => |
| 48 | + switch (p->Js.Json.parseExn->PageAuthorExcerpts_bs.read_payload) { |
| 49 | + | authors => <PageAuthorExcerpts authors /> |
| 50 | + | exception _exn => |
| 51 | + Js.log("Couldn't parse authors from JSON payload " ++ p); |
| 52 | + <PageNotFound />; |
| 53 | + } |
| 54 | + | None => |
| 55 | + <Client.FetchRender |
| 56 | + url={Routes.sprintf(Router.ApiRoutes.authors_with_excerpts())} |
| 57 | + decoder=PageAuthorExcerpts_bs.read_payload> |
| 58 | + {(authors => <PageAuthorExcerpts authors />)} |
| 59 | + </Client.FetchRender> |
| 60 | + }; |
| 61 | + }; |
| 62 | + module Api = { |
| 63 | + // Api routes should never be loaded from React app, show the backing page in case it happens |
| 64 | + let excerpts_by_author = Pages.excerpts_by_author; |
| 65 | + let authors_with_excerpts = Pages.authors_with_excerpts; |
| 66 | + }; |
25 | 67 | }; |
26 | 68 |
|
27 | | -module Router = Router.Make(Get); |
28 | | -let router = Routes.one_of(Router.routes); |
| 69 | +module Router = Router.Make(Handlers); |
| 70 | +let router = Method_routes.one_of(Router.routes); |
| 71 | + |
29 | 72 | [@react.component] |
30 | 73 | let make = () => { |
31 | 74 | let url = ReasonReactRouter.useUrl(); |
32 | 75 | let target = url.path->Array.of_list->Js.Array2.joinWith("/"); |
33 | | - // let excerpts = [ |
34 | | - // { |
35 | | - // Excerpt_t.author: "Hey", |
36 | | - // excerpt: "one excerpt", |
37 | | - // source: "sdfdsf", |
38 | | - // page: Some("2"), |
39 | | - // }, |
40 | | - // ]; |
| 76 | + let payloadElement = Bindings.getElementById(Api.payload_id); |
| 77 | + |
41 | 78 | switch ( |
42 | | - Routes.match'(router, ~target=Js.Global.decodeURIComponent(target)) |
| 79 | + Method_routes.match'( |
| 80 | + ~meth=`GET, |
| 81 | + ~target=Js.Global.decodeURIComponent(target), |
| 82 | + router, |
| 83 | + ) |
43 | 84 | ) { |
44 | 85 | | None => <PageNotFound /> |
45 | | - | Some(h) => h(target) |
| 86 | + | Some(handler) => |
| 87 | + let payload = payloadElement->Belt.Option.map(Bindings.innerHTML); |
| 88 | + /* After processing the payload from server, it can be safely removed so other pages don't try to decode it again |
| 89 | + as the client will take over navigation + data fetching through API */ |
| 90 | + payloadElement->Belt.Option.forEach(e => e->Bindings.remove()); |
| 91 | + handler(payload); |
46 | 92 | }; |
47 | 93 | }; |
0 commit comments