resort publications, populate dropdowns in link form

3067339 · gmorell · 2022-12-14 03:34

17 files +35 -14

Files changed

modified lib/lambent_ex/link.ex
+1 −1
@@ -55,7 +55,7 @@ defmodule LambentEx.Link do
55 55 @impl true
56 56 def handle_info(:publish, state) do
57 57 Process.send_after(self(), :publish, 400)
58 − Phoenix.PubSub.broadcast(@pubsub_name, @pubsub_topic_idx, {:publish, publish(state)})
58 + Phoenix.PubSub.broadcast(@pubsub_name, @pubsub_topic_idx, {:links_pub, publish(state)})
59 59 {:noreply, state}
60 60 end
61 61 end
modified lib/lambent_ex/machine.ex
+1 −1
@@ -145,7 +145,7 @@ defmodule LambentEx.Machine do
145 145 @impl true
146 146 def handle_info(:publish, state) do
147 147 Process.send_after(self(), :publish, 400)
148 − Phoenix.PubSub.broadcast(@pubsub_name, @pubsub_topic_idx, {:publish, publish(state)})
148 + Phoenix.PubSub.broadcast(@pubsub_name, @pubsub_topic_idx, {:machines_pub, publish(state)})
149 149 {:noreply, state}
150 150 end
151 151
modified lib/lambent_ex/scan/8266-7777.ex
+1 −1
@@ -284,7 +284,7 @@ defmodule LambentEx.Scan.ESP8266x7777 do
284 284 Phoenix.PubSub.broadcast(
285 285 @pubsub_name,
286 286 "scan-82667777",
287 − {:publish, state[:devices] |> add_type}
287 + {:devices_pub, state[:devices] |> add_type}
288 288 )
289 289
290 290 {:noreply, state}
modified lib/lambent_ex_web/live/devices/index.ex
+1 −1
@@ -19,7 +19,7 @@ defmodule LambentExWeb.DevicesLive.Index do
19 19 {:noreply, apply_action(socket, socket.assigns.live_action, params)}
20 20 end
21 21
22 − def handle_info({:publish, devices}, socket) do
22 + def handle_info({:devices_pub, devices}, socket) do
23 23 {:noreply, socket |> assign(:devices, devices)}
24 24 end
25 25
modified lib/lambent_ex_web/live/links/form_component.ex
+11 −1
@@ -34,6 +34,16 @@ defmodule LambentEx.LinksLive.FormComponent do
34 34 {:noreply, assign(socket, :changeset, changeset)}
35 35 end
36 36 # save_testyyyy(socket, socket.assigns.action, testyyyy_params)
37 −:ok
37 + :ok
38 + end
39 +
40 + def for_selectm(array) do
41 + array |> Enum.map(fn {k,v} -> {v[:id], v[:name]} end)
42 + end
43 +
44 + def for_selectd(array) do
45 + IO.inspect(array)
46 + array |> Enum.map(fn {iface,entries} -> entries |> Enum.map(fn {k, v} -> {v["name"], k} end) end) |> List.flatten |> Enum.sort_by(fn {k, v} -> k end)
47 +# [1,2,3]
38 48 end
39 49 end
modified lib/lambent_ex_web/live/links/form_component.html.heex
+2 −2
@@ -9,8 +9,8 @@
9 9 phx-submit="save"
10 10 >
11 11 <.input field={{f, :name}} type="text" label="Name" options={1..2} />
12 − <.input field={{f, :source_id}} type="select" label="Source" options={1..2} />
13 − <.input field={{f, :target_id}} type="select" label="Target" options={1..2} />
12 + <.input field={{f, :source_id}} type="select" label="Source" options={@machines |> for_selectm} />
13 + <.input field={{f, :target_id}} type="select" label="Target" options={@devices |> for_selectd} />
14 14 <:actions>
15 15 <.button phx-disable-with="Saving...">Create Link</.button>
16 16 </:actions>
modified lib/lambent_ex_web/live/links/index.ex
+15 −6
@@ -10,15 +10,16 @@ defmodule LambentExWeb.LinksLive.Index do
10 10
11 11 @impl true
12 12 def mount(_params, _session, socket) do
13 + Phoenix.PubSub.subscribe(@pubsub_name, "machines_idx")
14 + Phoenix.PubSub.subscribe(@pubsub_name, "scan-82667777")
13 15 Phoenix.PubSub.subscribe(@pubsub_name, "links_idx")
14 16
15 17 {:ok,
16 18 socket
17 − |> assign(
18 − :links,
19 − %{}
20 − )
21 − |> assign(:link, nil)
19 + |> assign(:links, %{} )
20 + |> assign(:machines, %{})
21 + |> assign(:devices, %{})
22 + |> assign(:link, nil)
22 23 |> assign(:nonew, :true)
23 24 |> assign(:newctrl, [
24 25 %{link: ~p"/cfg/links/new", icon: "plus"},
@@ -47,7 +48,15 @@ defmodule LambentExWeb.LinksLive.Index do
47 48 |> assign(:link, %LambentEx.Schema.Links{})
48 49 end
49 50
50 − def handle_info({:publish, link}, socket) do
51 + def handle_info({:machines_pub, machine}, socket) do
52 + {:noreply, socket |> assign(:machines, socket.assigns.machines |> Map.put(machine[:name], machine))}
53 + end
54 +
55 + def handle_info({:links_pub, link}, socket) do
51 56 {:noreply, socket |> assign(:links, socket.assigns.links |> Map.put(link[:name], link))}
52 57 end
58 +
59 + def handle_info({:devices_pub, device}, socket) do
60 + {:noreply, socket |> assign(:devices, socket.assigns.devices |> Map.put(device[:mac], device))}
61 + end
53 62 end
modified lib/lambent_ex_web/live/links/index.html.heex
+2 −0
@@ -11,6 +11,8 @@ on_cancel={JS.navigate(~p"/cfg/links")}>
11 11 action={@live_action}
12 12 link={@link}
13 13 return_to={~p"/cfg/links"}
14 + machines={@machines}
15 + devices={@devices}
14 16 />
15 17 </.modal>
16 18 <% end %>
modified lib/lambent_ex_web/live/machines/index.ex
+1 −1
@@ -42,7 +42,7 @@ defmodule LambentExWeb.MachinesLive.Index do
42 42 |> assign(:source, nil)
43 43 end
44 44
45 − def handle_info({:publish, machine}, socket) do
45 + def handle_info({:machines_pub, machine}, socket) do
46 46 # IO.inspect(machine)
47 47 {:noreply, socket |> assign(:machines, socket.assigns.machines |> Map.put(machine[:id], machine))}
48 48 end

Parents: ede4deb