Skip to content

Commit

Permalink
feat: first version
Browse files Browse the repository at this point in the history
  • Loading branch information
Massolari committed Apr 4, 2024
0 parents commit 19b0258
Show file tree
Hide file tree
Showing 7 changed files with 694 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.beam
*.ez
/build
erl_crash.dump
95 changes: 95 additions & 0 deletions README.md
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>.
16 changes: 16 additions & 0 deletions gleam.toml
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"
11 changes: 11 additions & 0 deletions manifest.toml
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" }
Loading

0 comments on commit 19b0258

Please sign in to comment.