Skip to content

Commit 19b0258

Browse files
committed
feat: first version
0 parents  commit 19b0258

File tree

7 files changed

+694
-0
lines changed

7 files changed

+694
-0
lines changed

.github/workflows/test.yml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: test
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
- main
8+
pull_request:
9+
10+
jobs:
11+
test:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v3
15+
- uses: erlef/setup-beam@v1
16+
with:
17+
otp-version: "26.0.2"
18+
gleam-version: "1.0.0"
19+
rebar3-version: "3"
20+
# elixir-version: "1.15.4"
21+
- run: gleam deps download
22+
- run: gleam test
23+
- run: gleam format --check src test

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.beam
2+
*.ez
3+
/build
4+
erl_crash.dump

README.md

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
[![Package Version](https://img.shields.io/hexpm/v/remote_data)](https://hex.pm/packages/remote_data)
6+
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](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>.

gleam.toml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name = "remote_data"
2+
version = "1.0.0"
3+
4+
description = "A package to deal with remote data in Gleam"
5+
licences = ["Apache-2.0"]
6+
repository = { type = "github", user = "Massolari", repo = "remote_data" }
7+
# links = [{ title = "Website", href = "https://gleam.run" }]
8+
#
9+
# For a full reference of all the available options, you can have a look at
10+
# https://gleam.run/writing-gleam/gleam-toml/.
11+
12+
[dependencies]
13+
gleam_stdlib = "~> 0.34 or ~> 1.0"
14+
15+
[dev-dependencies]
16+
gleeunit = "~> 1.0"

manifest.toml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# This file was generated by Gleam
2+
# You typically do not need to edit this file
3+
4+
packages = [
5+
{ name = "gleam_stdlib", version = "0.36.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "C0D14D807FEC6F8A08A7C9EF8DFDE6AE5C10E40E21325B2B29365965D82EB3D4" },
6+
{ name = "gleeunit", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "72CDC3D3F719478F26C4E2C5FED3E657AC81EC14A47D2D2DEBB8693CA3220C3B" },
7+
]
8+
9+
[requirements]
10+
gleam_stdlib = { version = "~> 0.34 or ~> 1.0" }
11+
gleeunit = { version = "~> 1.0" }

0 commit comments

Comments
 (0)