2024-02-18 01:48:10 +00:00
|
|
|
import gleam/int
|
2024-02-18 02:49:50 +00:00
|
|
|
import gleam/javascript/promise.{type Promise}
|
|
|
|
import gleam/io
|
2024-02-18 01:48:10 +00:00
|
|
|
import lustre
|
|
|
|
import lustre/attribute
|
|
|
|
import lustre/element.{type Element}
|
|
|
|
import lustre/element/html
|
|
|
|
import lustre/event
|
|
|
|
|
|
|
|
pub fn main() {
|
|
|
|
let app = lustre.simple(init, update, view)
|
|
|
|
let assert Ok(dispatch) = lustre.start(app, "#app", Nil)
|
|
|
|
|
|
|
|
dispatch
|
|
|
|
}
|
|
|
|
|
|
|
|
type Model =
|
|
|
|
Int
|
|
|
|
|
|
|
|
fn init(_) -> Model {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type Msg {
|
|
|
|
Increment
|
|
|
|
Decrement
|
2024-02-18 02:49:50 +00:00
|
|
|
Greet
|
2024-02-18 01:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn update(model: Model, msg: Msg) -> Model {
|
|
|
|
case msg {
|
|
|
|
Increment -> model + 1
|
|
|
|
Decrement -> model - 1
|
2024-02-18 02:49:50 +00:00
|
|
|
Greet -> {
|
|
|
|
{
|
|
|
|
use greeting <- promise.await(greet("Gleam"))
|
|
|
|
case greeting {
|
|
|
|
Ok(g) -> io.print(g)
|
|
|
|
Error(e) -> io.print("greeting err: " <> e)
|
|
|
|
}
|
|
|
|
promise.resolve(#())
|
|
|
|
}
|
|
|
|
model
|
|
|
|
}
|
2024-02-18 01:48:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-18 02:49:50 +00:00
|
|
|
@external(javascript, "./ffi/commands.js", "greet")
|
|
|
|
pub fn greet(name: String) -> Promise(Result(String, String))
|
|
|
|
|
2024-02-18 01:48:10 +00:00
|
|
|
fn view(model: Model) -> Element(Msg) {
|
|
|
|
let count = int.to_string(model)
|
|
|
|
|
2024-02-18 02:49:50 +00:00
|
|
|
html.div([], [
|
|
|
|
html.h1([], [element.text("Gleam + Vite + Tauri")]),
|
|
|
|
html.p([attribute.style([#("text-align", "center")])], [
|
|
|
|
element.text("Gleam counter " <> count <> " ✨"),
|
|
|
|
]),
|
|
|
|
html.p([attribute.style([#("text-align", "center")])], [
|
|
|
|
html.button([event.on_click(Decrement)], [element.text("-")]),
|
|
|
|
html.button([event.on_click(Increment)], [element.text("+")]),
|
|
|
|
html.button([event.on_click(Greet)], [element.text("Greet")]),
|
|
|
|
]),
|
|
|
|
])
|
2024-02-18 01:48:10 +00:00
|
|
|
}
|