|
| 1 | +# remote_data for Gleam |
| 2 | + |
| 3 | +This package is inspired on the Elm package [RemoteData](https://package.elm-lang.org/packages/krisajenkins/remotedata/latest/). |
| 4 | + |
| 5 | +[](https://hex.pm/packages/remote_data) |
| 6 | +[](https://hexdocs.pm/remote_data/) |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +```sh |
| 11 | +gleam add remote_data |
| 12 | +``` |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +This example shows how to use the `remote_data` package in a [lustre](https://hexdocs.pm/lustre/index.html) application. |
| 17 | + |
| 18 | +First you wrap the data you want to fetch in a `RemoteData` type: |
| 19 | + |
| 20 | +```gleam |
| 21 | +import remote_data.{type RemoteData} as rd |
| 22 | +import lustre |
| 23 | +import lustre/element |
| 24 | +import lustre/element/html |
| 25 | +import lustre_http.{type HttpError} |
| 26 | +
|
| 27 | +// MODEL ----------------------------------------------------------------------- |
| 28 | +
|
| 29 | +type Model { |
| 30 | + Model(quote: RemoteData(Quote, HttpError)) |
| 31 | +} |
| 32 | +
|
| 33 | +type Quote { |
| 34 | + Quote(author: String, content: String) |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +Initialize the model with `rd.NotAsked`: |
| 39 | +```gleam |
| 40 | +fn init(_) -> #(Model, Effect(Msg)) { |
| 41 | + #(Model(quote: rd.NotAsked), effect.none()) |
| 42 | +} |
| 43 | +
|
| 44 | +``` |
| 45 | + |
| 46 | +When you want to fetch data, you can use the `rd.Loading` constructor to indicate that the data is being fetched. |
| 47 | +When the data is fetched, you can use the `rd.from_result` to convert the `Result` to a `RemoteData` type: |
| 48 | + |
| 49 | +```gleam |
| 50 | +pub opaque type Msg { |
| 51 | + UserClickedRefresh |
| 52 | + ApiUpdatedQuote(Result(Quote, HttpError)) |
| 53 | +} |
| 54 | +
|
| 55 | +fn update(model: Model, msg: Msg) -> #(Model, Effect(Msg)) { |
| 56 | + case msg { |
| 57 | + UserClickedRefresh -> #(Model(quote: rd.Loading), get_quote()) |
| 58 | + ApiUpdatedQuote(quote) -> #(Model(quote: rd.from_result(quote)), effect.none()) |
| 59 | + } |
| 60 | +} |
| 61 | +
|
| 62 | +fn get_quote() -> Effect(Msg) { |
| 63 | + let url = "https://api.quotable.io/random" |
| 64 | + let decoder = |
| 65 | + dynamic.decode2( |
| 66 | + Quote, |
| 67 | + dynamic.field("author", dynamic.string), |
| 68 | + dynamic.field("content", dynamic.string), |
| 69 | + ) |
| 70 | +
|
| 71 | + lustre_http.get(url, lustre_http.expect_json(decoder, ApiUpdatedQuote)) |
| 72 | +} |
| 73 | +
|
| 74 | +``` |
| 75 | + |
| 76 | +Finally, you can pattern match on the `RemoteData` type to display the data in the view: |
| 77 | + |
| 78 | +```gleam |
| 79 | +fn view_quote(quote: RemoteData(Quote, HttpError)) -> Element(msg) { |
| 80 | + case quote { |
| 81 | + rd.Success(quote) -> |
| 82 | + html.div([], [ |
| 83 | + element.text(quote.author <> " once said..."), |
| 84 | + html.p([attribute.style([#("font-style", "italic")])], [ |
| 85 | + element.text(quote.content), |
| 86 | + ]), |
| 87 | + ]) |
| 88 | + rd.NotAsked -> html.p([], [element.text("Click the button to get a quote!")]) |
| 89 | + rd.Loading -> html.p([], [element.text("Fetching quote...")]) |
| 90 | + rd.Failure(_) -> html.p([], [element.text("Failed to fetch quote!")]) |
| 91 | + } |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +Further documentation can be found at <https://hexdocs.pm/remote_data>. |
0 commit comments