app.re 997 Bytes
Newer Older
Emmanuel Raviart's avatar
Emmanuel Raviart committed
1 2
type page =
  | AboutPage
3
  | GraphqlPage(list(string), string);
Emmanuel Raviart's avatar
Emmanuel Raviart committed
4

Emmanuel Raviart's avatar
Emmanuel Raviart committed
5 6
type action =
  | Navigate(page);
Emmanuel Raviart's avatar
Emmanuel Raviart committed
7

Emmanuel Raviart's avatar
Emmanuel Raviart committed
8 9
type state = {
  page,
Emmanuel Raviart's avatar
Emmanuel Raviart committed
10
  todo: int,
Emmanuel Raviart's avatar
Emmanuel Raviart committed
11 12 13
};

let component = ReasonReact.reducerComponent("App");
Emmanuel Raviart's avatar
Emmanuel Raviart committed
14

Emmanuel Raviart's avatar
Emmanuel Raviart committed
15
let urlToPage = (url: ReasonReact.Router.url) : page =>
Emmanuel Raviart's avatar
Emmanuel Raviart committed
16
  switch (url.path) {
Emmanuel Raviart's avatar
Emmanuel Raviart committed
17
  | ["a-propos"] => AboutPage
18
  | _ => GraphqlPage(url.path, url.hash)
Emmanuel Raviart's avatar
Emmanuel Raviart committed
19 20 21
  };

let make = (~message as _message, _children) => {
Emmanuel Raviart's avatar
Emmanuel Raviart committed
22
  ...component,
Emmanuel Raviart's avatar
Emmanuel Raviart committed
23 24
  initialState: () => {
    page: urlToPage(ReasonReact.Router.dangerouslyGetInitialUrl()),
Emmanuel Raviart's avatar
Emmanuel Raviart committed
25
    todo: 0,
Emmanuel Raviart's avatar
Emmanuel Raviart committed
26 27 28 29 30 31 32
  },
  subscriptions: self => [
    Sub(
      () =>
        ReasonReact.Router.watchUrl(url =>
          self.send(Navigate(urlToPage(url)))
        ),
Emmanuel Raviart's avatar
Emmanuel Raviart committed
33 34
      ReasonReact.Router.unwatchUrl,
    ),
Emmanuel Raviart's avatar
Emmanuel Raviart committed
35
  ],
36
  reducer: (action, state) =>
Emmanuel Raviart's avatar
Emmanuel Raviart committed
37
    switch (action) {
38 39
    | Navigate(page) => ReasonReact.Update({...state, page})
    },
Emmanuel Raviart's avatar
Emmanuel Raviart committed
40
  render: self =>
Emmanuel Raviart's avatar
Emmanuel Raviart committed
41
    switch (self.state.page) {
42
    | AboutPage => <About />
43
    | GraphqlPage(path, hash) => <GraphqlClientQuery hash path />
Emmanuel Raviart's avatar
Emmanuel Raviart committed
44 45
    },
};