Skip to content

Instantly share code, notes, and snippets.

@relekang
Last active October 6, 2019 12:50
Show Gist options
  • Save relekang/c4a87b737f21c1eae09515547df4e633 to your computer and use it in GitHub Desktop.
Save relekang/c4a87b737f21c1eae09515547df4e633 to your computer and use it in GitHub Desktop.
Helpers for working with ReasonUrql.Types.response
type t('a) = ReasonUrql.Types.response('a);
let map: (t('a), 'a => 'b) => t('b) =
(response, fn) =>
switch (response) {
| Fetching => Fetching
| NotFound => NotFound
| Error(error) => Error(error)
| Data(data) => Data(fn(data))
};
let log: t('a) => unit =
response =>
switch (response) {
| Fetching => Js.log("fetching")
| NotFound => Js.log("not found")
| Error(error) => Js.log2("error", error)
| Data(data) => Js.log2("data", data)
};
let getWithDefault: (t('a), 'a) => React.element =
(response, default) =>
switch (response) {
| Fetching => default
| NotFound => default
| Error(_) => default
| Data(data) => data
};
let toElement: (t('a), 'a => React.element) => React.element =
(response, fn) => response
->map(fn)
->getWithDefault(React.null);
let isDone: t('a) => bool =
response =>
switch (response) {
| Fetching => false
| NotFound => false
| Error(_) => true
| Data(_) => true
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment