diff options
author | Hayleigh Thompson <me@hayleigh.dev> | 2023-07-10 23:11:32 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-10 23:11:32 +0100 |
commit | f350db196bcab490b8a6b67f9536f0b9b7322073 (patch) | |
tree | 59664ca671be2cfdb473424ca1bf737cf12622e8 /example/src/lustre_counter.gleam | |
parent | 6d314230346336ba5b452b1df39b908ffa666f45 (diff) | |
download | lustre-f350db196bcab490b8a6b67f9536f0b9b7322073.tar.gz lustre-f350db196bcab490b8a6b67f9536f0b9b7322073.zip |
♻️ Replace React with diffhtml (#10)
* :wrench: Remove react dependency, add vite for running examples.
* :heavy_plus_sign: Update stdlib version to 0.29
* :fire: Remove old examples.
* :sparkles: Vendor diffhtml and update runtime ffi code to replace react.
* :recycle: Refactor all the things now react is gone.
* :memo: Remove references to react in the readme.
* :sparkles: Create a simple counter example.
Diffstat (limited to 'example/src/lustre_counter.gleam')
-rw-r--r-- | example/src/lustre_counter.gleam | 95 |
1 files changed, 0 insertions, 95 deletions
diff --git a/example/src/lustre_counter.gleam b/example/src/lustre_counter.gleam deleted file mode 100644 index 25872a4..0000000 --- a/example/src/lustre_counter.gleam +++ /dev/null @@ -1,95 +0,0 @@ -import gleam/int -import lustre -import lustre/element.{button, div, p, span, text} -import lustre/event.{dispatch, on_click} -import lustre/cmd -import gleam/map.{Map} -import gleam/list -import gleam/option - -pub fn main() { - let app = lustre.application(#(init_state(), cmd.none()), update, render) - lustre.start(app, "#app") -} - -type State { - State(ctr: Int, counters: Map(Int, Int)) -} - -fn init_state() { - State(ctr: 2, counters: map.from_list([#(1, 0)])) -} - -pub type Action { - Add - Remove(id: Int) - Increment(id: Int) - Decrement(id: Int) -} - -fn update(state, action) { - case action { - Add -> #( - State( - ..state, - ctr: state.ctr + 1, - counters: state.counters - |> map.insert(state.ctr, 0), - ), - cmd.none(), - ) - Remove(id) -> #( - State( - ..state, - counters: state.counters - |> map.delete(id), - ), - cmd.none(), - ) - Increment(id) -> #( - State( - ..state, - counters: state.counters - |> map.update(id, fn(opt_ctr) { option.unwrap(opt_ctr, 0) + 1 }), - ), - cmd.none(), - ) - Decrement(id) -> #( - State( - ..state, - counters: state.counters - |> map.update(id, fn(opt_ctr) { option.unwrap(opt_ctr, 0) - 1 }), - ), - cmd.none(), - ) - } -} - -fn render(state) { - let render_counter = fn(pair) { - let #(id, value) = pair - p( - [], - [ - button([on_click(dispatch(Decrement(id)))], [text("-")]), - span([], [text(" "), text(int.to_string(value)), text(" ")]), - button([on_click(dispatch(Increment(id)))], [text("+")]), - span([], [text(" ")]), - button([on_click(dispatch(Remove(id)))], [text("remove")]), - ], - ) - } - - div( - [], - [ - div( - [], - state.counters - |> map.to_list - |> list.map(render_counter), - ), - p([], [button([on_click(dispatch(Add))], [text("add")])]), - ], - ) -} |