Hooked up the enabled toggle to the sources page

This commit is contained in:
Kieran Eglin 2024-11-21 12:13:12 -08:00
parent f6eedb1783
commit 53ab5daa96
No known key found for this signature in database
GPG key ID: 193984967FCF432D

View file

@ -4,6 +4,7 @@ defmodule PinchflatWeb.Sources.IndexTableLive do
use Pinchflat.Sources.SourcesQuery use Pinchflat.Sources.SourcesQuery
alias Pinchflat.Repo alias Pinchflat.Repo
alias Pinchflat.Sources
alias Pinchflat.Sources.Source alias Pinchflat.Sources.Source
alias Pinchflat.Media.MediaItem alias Pinchflat.Media.MediaItem
@ -35,6 +36,7 @@ defmodule PinchflatWeb.Sources.IndexTableLive do
<:col :let={source} label="Enabled?"> <:col :let={source} label="Enabled?">
<.input <.input
name={"source[#{source.id}][enabled]"} name={"source[#{source.id}][enabled]"}
value={source.enabled}
id={"source_#{source.id}_enabled"} id={"source_#{source.id}_enabled"}
phx-hook="formless-input" phx-hook="formless-input"
data-subscribe="change" data-subscribe="change"
@ -51,18 +53,18 @@ defmodule PinchflatWeb.Sources.IndexTableLive do
end end
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
new_assigns = %{ {:ok, assign(socket, %{sources: get_sources()})}
sources: get_sources()
}
{:ok, assign(socket, new_assigns)}
end end
def handle_event("formless-input", %{"event" => "toggle_enabled"} = params, socket) do def handle_event("formless-input", %{"event" => "toggle_enabled"} = params, socket) do
# should_enable = params["value"] == "true" source = Sources.get_source!(params["id"])
IO.inspect(params) should_enable = params["value"] == "true"
{:noreply, socket} {:ok, new_source} = Sources.update_source(source, %{enabled: should_enable})
# Trying to be efficient with the update. Let's see if it pays off
updated_sources = Enum.map(socket.assigns.sources, fn s -> if s.id == new_source.id, do: new_source, else: s end)
{:noreply, assign(socket, sources: updated_sources)}
end end
defp get_sources do defp get_sources do