-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 19b0258
Showing
7 changed files
with
694 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
name: test | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
- main | ||
pull_request: | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: erlef/setup-beam@v1 | ||
with: | ||
otp-version: "26.0.2" | ||
gleam-version: "1.0.0" | ||
rebar3-version: "3" | ||
# elixir-version: "1.15.4" | ||
- run: gleam deps download | ||
- run: gleam test | ||
- run: gleam format --check src test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
*.beam | ||
*.ez | ||
/build | ||
erl_crash.dump |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# remote_data for Gleam | ||
|
||
This package is inspired on the Elm package [RemoteData](https://package.elm-lang.org/packages/krisajenkins/remotedata/latest/). | ||
|
||
[![Package Version](https://img.shields.io/hexpm/v/remote_data)](https://hex.pm/packages/remote_data) | ||
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/remote_data/) | ||
|
||
## Installation | ||
|
||
```sh | ||
gleam add remote_data | ||
``` | ||
|
||
## Usage | ||
|
||
This example shows how to use the `remote_data` package in a [lustre](https://hexdocs.pm/lustre/index.html) application. | ||
|
||
First you wrap the data you want to fetch in a `RemoteData` type: | ||
|
||
```gleam | ||
import remote_data.{type RemoteData} as rd | ||
import lustre | ||
import lustre/element | ||
import lustre/element/html | ||
import lustre_http.{type HttpError} | ||
// MODEL ----------------------------------------------------------------------- | ||
type Model { | ||
Model(quote: RemoteData(Quote, HttpError)) | ||
} | ||
type Quote { | ||
Quote(author: String, content: String) | ||
} | ||
``` | ||
|
||
Initialize the model with `rd.NotAsked`: | ||
```gleam | ||
fn init(_) -> #(Model, Effect(Msg)) { | ||
#(Model(quote: rd.NotAsked), effect.none()) | ||
} | ||
``` | ||
|
||
When you want to fetch data, you can use the `rd.Loading` constructor to indicate that the data is being fetched. | ||
When the data is fetched, you can use the `rd.from_result` to convert the `Result` to a `RemoteData` type: | ||
|
||
```gleam | ||
pub opaque type Msg { | ||
UserClickedRefresh | ||
ApiUpdatedQuote(Result(Quote, HttpError)) | ||
} | ||
fn update(model: Model, msg: Msg) -> #(Model, Effect(Msg)) { | ||
case msg { | ||
UserClickedRefresh -> #(Model(quote: rd.Loading), get_quote()) | ||
ApiUpdatedQuote(quote) -> #(Model(quote: rd.from_result(quote)), effect.none()) | ||
} | ||
} | ||
fn get_quote() -> Effect(Msg) { | ||
let url = "https://api.quotable.io/random" | ||
let decoder = | ||
dynamic.decode2( | ||
Quote, | ||
dynamic.field("author", dynamic.string), | ||
dynamic.field("content", dynamic.string), | ||
) | ||
lustre_http.get(url, lustre_http.expect_json(decoder, ApiUpdatedQuote)) | ||
} | ||
``` | ||
|
||
Finally, you can pattern match on the `RemoteData` type to display the data in the view: | ||
|
||
```gleam | ||
fn view_quote(quote: RemoteData(Quote, HttpError)) -> Element(msg) { | ||
case quote { | ||
rd.Success(quote) -> | ||
html.div([], [ | ||
element.text(quote.author <> " once said..."), | ||
html.p([attribute.style([#("font-style", "italic")])], [ | ||
element.text(quote.content), | ||
]), | ||
]) | ||
rd.NotAsked -> html.p([], [element.text("Click the button to get a quote!")]) | ||
rd.Loading -> html.p([], [element.text("Fetching quote...")]) | ||
rd.Failure(_) -> html.p([], [element.text("Failed to fetch quote!")]) | ||
} | ||
} | ||
``` | ||
|
||
Further documentation can be found at <https://hexdocs.pm/remote_data>. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
name = "remote_data" | ||
version = "1.0.0" | ||
|
||
description = "A package to deal with remote data in Gleam" | ||
licences = ["Apache-2.0"] | ||
repository = { type = "github", user = "Massolari", repo = "remote_data" } | ||
# links = [{ title = "Website", href = "https://gleam.run" }] | ||
# | ||
# For a full reference of all the available options, you can have a look at | ||
# https://gleam.run/writing-gleam/gleam-toml/. | ||
|
||
[dependencies] | ||
gleam_stdlib = "~> 0.34 or ~> 1.0" | ||
|
||
[dev-dependencies] | ||
gleeunit = "~> 1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# This file was generated by Gleam | ||
# You typically do not need to edit this file | ||
|
||
packages = [ | ||
{ name = "gleam_stdlib", version = "0.36.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "C0D14D807FEC6F8A08A7C9EF8DFDE6AE5C10E40E21325B2B29365965D82EB3D4" }, | ||
{ name = "gleeunit", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "72CDC3D3F719478F26C4E2C5FED3E657AC81EC14A47D2D2DEBB8693CA3220C3B" }, | ||
] | ||
|
||
[requirements] | ||
gleam_stdlib = { version = "~> 0.34 or ~> 1.0" } | ||
gleeunit = { version = "~> 1.0" } |
Oops, something went wrong.