Support message parameters

This commit is contained in:
Paul-Henri Froidmont 2025-09-24 01:40:27 +02:00
parent 763788fb89
commit 681feced9f
Signed by: phfroidmont
GPG key ID: BE948AFD7E7873BE
16 changed files with 277 additions and 106 deletions

View file

@ -2,7 +2,6 @@ import CounterLiveView.*
import monocle.syntax.all.*
import scalive.*
import zio.*
import zio.json.*
import zio.stream.ZStream
class CounterLiveView() extends LiveView[Msg, Model]:
@ -34,8 +33,8 @@ class CounterLiveView() extends LiveView[Msg, Model]:
div(
cls := "flex flex-wrap items-center gap-3",
button(
cls := "btn btn-default",
phx.click := Msg.ToggleCounter,
cls := "btn btn-default",
phx.onClick(Msg.ToggleCounter),
model(_.isVisible match
case true => "Hide counter"
case false => "Show counter")
@ -45,8 +44,8 @@ class CounterLiveView() extends LiveView[Msg, Model]:
div(
cls := "flex items-center justify-center gap-4",
button(
cls := "btn btn-neutral",
phx.click := Msg.DecCounter,
cls := "btn btn-neutral",
phx.onClick(Msg.DecCounter),
"-"
),
div(
@ -54,8 +53,8 @@ class CounterLiveView() extends LiveView[Msg, Model]:
model(_.counter.toString)
),
button(
cls := "btn btn-neutral",
phx.click := Msg.IncCounter,
cls := "btn btn-neutral",
phx.onClick(Msg.IncCounter),
"+"
)
)
@ -70,7 +69,7 @@ end CounterLiveView
object CounterLiveView:
enum Msg derives JsonCodec:
enum Msg:
case ToggleCounter
case IncCounter
case DecCounter

View file

@ -38,6 +38,10 @@ object Example extends ZIOAppDefault:
(_, req) =>
val q = req.queryParam("q").map("Param : " ++ _).getOrElse("No param")
ListLiveView(q)
),
LiveRoute(
Root / "todo",
(_, _) => TodoLiveView()
)
)
)

View file

@ -5,7 +5,8 @@ import zio.stream.ZStream
class HomeLiveView() extends LiveView[String, Unit]:
val links = List(
"/counter" -> "Counter",
"/list?q=test" -> "List"
"/list?q=test" -> "List",
"/todo" -> "Todo"
)
def init = ZIO.succeed(())

View file

@ -2,7 +2,6 @@ import ListLiveView.*
import monocle.syntax.all.*
import scalive.*
import zio.*
import zio.json.*
import zio.stream.ZStream
class ListLiveView(someParam: String) extends LiveView[Msg, Model]:
@ -48,14 +47,14 @@ class ListLiveView(someParam: String) extends LiveView[Msg, Model]:
div(
cls := "card-actions",
button(
cls := "btn btn-default",
phx.click := Msg.IncAge(1),
cls := "btn btn-default",
phx.onClick(Msg.IncAge(1)),
"Inc age"
),
span(cls := "grow"),
button(
cls := "btn btn-neutral",
phx.click := JS.toggleClass("btn-neutral btn-accent").push(Msg.IncAge(-5)),
cls := "btn btn-neutral",
phx.onClick(JS.toggleClass("btn-neutral btn-accent").push(Msg.IncAge(-5))),
"Toggle color"
)
)
@ -68,7 +67,7 @@ end ListLiveView
object ListLiveView:
enum Msg derives JsonCodec:
enum Msg:
case IncAge(value: Int)
final case class Model(

View file

@ -0,0 +1,88 @@
import TodoLiveView.*
import monocle.syntax.all.*
import scalive.*
import zio.*
import zio.stream.ZStream
class TodoLiveView() extends LiveView[Msg, Model]:
def init = ZIO.succeed(Model(List(Todo(99, "some task"))))
def update(model: Model) =
case Msg.Add(text) =>
val nextId = model.todos.maxByOption(_.id).map(_.id).getOrElse(1)
ZIO.succeed(
model
.focus(_.todos)
.modify(_.appended(Todo(nextId, text)))
)
case Msg.Remove(id) =>
ZIO.succeed(
model
.focus(_.todos)
.modify(_.filterNot(_.id == id))
)
case Msg.ToggleCompletion(id) =>
ZIO.succeed(
model
.focus(_.todos)
.modify(
_.map(todo => if todo.id == id then todo.copy(completed = todo.completed) else todo)
)
)
def view(model: Dyn[Model]) =
div(
cls := "mx-auto card bg-base-100 max-w-2xl shadow-xl space-y-6 p-6",
div(
cls := "card-body",
h1(cls := "card-title", "Todos"),
div(
cls := "flex items-center gap-3",
input(
cls := "input input-bordered grow",
typ := "text",
nameAttr := "todo-text",
placeholder := "What needs to be done?",
phx.onKeyup.withValue(Msg.Add(_)),
phx.key := "Enter",
phx.value("test") := "some value"
)
),
form(
cls := "flex items-center gap-3",
phx.onChange(p => Msg.Add(p("todo-text"))),
phx.onSubmit(p => Msg.Add(p("todo-text"))),
input(
cls := "input input-bordered grow",
typ := "text",
nameAttr := "todo-text",
placeholder := "What needs to be done?"
)
),
ul(
cls := "divide-y divide-base-200",
model(_.todos).splitByIndex((_, elem) =>
li(
cls := "py-3 flex flex-wrap items-center justify-between gap-2",
elem(_.text)
)
)
)
)
)
def subscriptions(model: Model) = ZStream.empty
end TodoLiveView
object TodoLiveView:
enum Msg:
case Add(text: String)
case Remove(id: Int)
case ToggleCompletion(id: Int)
final case class Model(todos: List[Todo] = List.empty, filter: Filter = Filter.All)
final case class Todo(id: Int, text: String, completed: Boolean = false)
enum Filter:
case All, Active, Completed