query loading/saving

d35b4f7 · gmorell · 2026-04-23 14:54

8 files +72 -55

Files changed

modified apps/room_sanctum/lib/room_sanctum_web/live/cfg_query_live/form_component.ex
+71 −54
@@ -13,6 +13,11 @@ defmodule RoomSanctumWeb.QueryLive.FormComponent do
13 13 def update(%{query: query} = assigns, socket) do
14 14 changeset = Configuration.change_query(query)
15 15
16 + sources = Configuration.list_cfg_sources({:user, assigns.current_user.id})
17 + focis = Configuration.list_focis({:user, assigns.current_user.id})
18 +
19 + current_type = type_for_source_id(sources, Map.get(query, :source_id))
20 +
16 21 {
17 22 :ok,
18 23 socket
@@ -20,21 +25,12 @@ defmodule RoomSanctumWeb.QueryLive.FormComponent do
20 25 |> assign_form(changeset)
21 26 |> assign(:changeset, changeset)
22 27 |> assign(:tint_opts, ["amber", "lime", "emerald", "sky", "violet", "fuchsia", "rose", "stone", "slate"])
23 − |> assign(:cfg_sources, list_cfg_sources(assigns.current_user.id))
24 − |> assign(:cfg_foci, list_cfg_foci(assigns.current_user.id))
28 + |> assign(:cfg_sources, sources)
29 + |> assign(:cfg_foci, focis)
30 + |> assign(:current_type, current_type)
25 31 |> assign(:results, [])
26 − |> assign(
27 − :cfg_sources_sel,
28 − list_cfg_sources(assigns.current_user.id)
29 − |> Enum.map(fn x -> {x.name, x.id} end)
30 − |> Enum.into(%{})
31 − )
32 − |> assign(
33 − :cfg_foci_sel,
34 − list_cfg_foci(assigns.current_user.id)
35 − |> Enum.map(fn x -> {x.name, x.id} end)
36 − |> Enum.into(%{})
37 − )
32 + |> assign(:cfg_sources_sel, Enum.into(sources, %{}, fn x -> {x.name, x.id} end))
33 + |> assign(:cfg_foci_sel, Enum.into(focis, %{}, fn x -> {x.name, x.id} end))
38 34 }
39 35 end
40 36
@@ -47,7 +43,13 @@ defmodule RoomSanctumWeb.QueryLive.FormComponent do
47 43 |> Configuration.change_query(query_params)
48 44 |> Map.put(:action, :validate)
49 45
50 − {:noreply, assign_form(socket, changeset)}
46 + current_type =
47 + type_for_source_id(socket.assigns.cfg_sources, parse_id(query_params["source_id"]))
48 +
49 + {:noreply,
50 + socket
51 + |> assign(:current_type, current_type)
52 + |> assign_form(changeset)}
51 53 end
52 54
53 55 def handle_event("save", %{"query" => query_params}, socket) do
@@ -57,38 +59,53 @@ defmodule RoomSanctumWeb.QueryLive.FormComponent do
57 59
58 60 def handle_event("do-gtfs-search", %{"value" => value}, socket) do
59 61 case value do
60 − nil -> {:noreply, socket}
61 − _otherwise -> {:noreply, socket |> assign(:results, Storage.list_stops(get_source_id(socket.assigns.form), value))}
62 + nil ->
63 + {:noreply, socket}
64 +
65 + _ ->
66 + {:noreply,
67 + socket
68 + |> assign(:results, Storage.list_stops(get_source_id(socket.assigns.form), value))}
62 69 end
63 70 end
64 71
65 − def handle_event("do-gbfs-search", %{"value" => value}, socket) do
72 + def handle_event("do-gbfs-search", %{"value" => value}, socket) do
66 73 case value do
67 − nil -> {:noreply, socket}
68 − _otherwise -> {:noreply, socket |> assign(:results, Storage.list_gbfs_station_information(get_source_id(socket.assigns.form), value))}
74 + nil ->
75 + {:noreply, socket}
76 +
77 + _ ->
78 + {:noreply,
79 + socket
80 + |> assign(
81 + :results,
82 + Storage.list_gbfs_station_information(get_source_id(socket.assigns.form), value)
83 + )}
69 84 end
70 85 end
71 86
72 87 def handle_event("set-gtfs", %{"val" => stop, "type" => type}, socket) do
73 − IO.inspect(socket.assigns.query)
74 − changeset =
88 + changeset =
75 89 socket.assigns.query
76 − |> Configuration.change_query(%{"query" => %{"stop" => stop, "__type__" => type}, "__type__" => type})
90 + |> Configuration.change_query(%{
91 + "query" => %{"stop" => stop, "__type__" => type},
92 + "__type__" => type
93 + })
77 94 |> Map.put(:action, :validate)
78 − |> IO.inspect
79 − IO.inspect(changeset.data)
80 − {:noreply, socket |> assign(:results, []) |> assign_form(changeset)}
95 +
96 + {:noreply, socket |> assign(:results, []) |> assign_form(changeset)}
81 97 end
82 98
83 99 def handle_event("set-gbfs", %{"val" => stop, "type" => type}, socket) do
84 − IO.inspect(socket.assigns.query)
85 100 changeset =
86 101 socket.assigns.query
87 − |> Configuration.change_query(%{"query" => %{"stop_id" => stop, "__type__" => type}, "__type__" => type})
102 + |> Configuration.change_query(%{
103 + "query" => %{"stop_id" => stop, "__type__" => type},
104 + "__type__" => type
105 + })
88 106 |> Map.put(:action, :validate)
89 − |> IO.inspect
90 − IO.inspect(changeset.data)
91 − {:noreply, socket |> assign(:results, []) |> assign_form(changeset)}
107 +
108 + {:noreply, socket |> assign(:results, []) |> assign_form(changeset)}
92 109 end
93 110
94 111 defp save_query(socket, :edit, query_params) do
@@ -100,7 +117,7 @@ defmodule RoomSanctumWeb.QueryLive.FormComponent do
100 117 :noreply,
101 118 socket
102 119 |> put_flash(:info, "Query updated successfully")
103 − |> push_redirect(to: socket.assigns.patch)
120 + |> push_patch(to: socket.assigns.patch)
104 121 }
105 122
106 123 {:error, %Ecto.Changeset{} = changeset} ->
@@ -117,7 +134,7 @@ defmodule RoomSanctumWeb.QueryLive.FormComponent do
117 134 :noreply,
118 135 socket
119 136 |> put_flash(:info, "Query created successfully")
120 − |> push_redirect(to: socket.assigns.patch)
137 + |> push_patch(to: socket.assigns.patch)
121 138 }
122 139
123 140 {:error, %Ecto.Changeset{} = changeset} ->
@@ -131,31 +148,31 @@ defmodule RoomSanctumWeb.QueryLive.FormComponent do
131 148
132 149 defp notify_parent(msg), do: send(self(), {__MODULE__, msg})
133 150
134 − defp list_cfg_sources(uid) do
135 − Configuration.list_cfg_sources({:user, uid})
136 − end
137 −
138 − defp list_cfg_foci(uid) do
139 − Configuration.list_focis({:user, uid})
140 − end
151 + defp type_for_source_id(_sources, nil), do: nil
141 152
142 − defp get_source_id(form) do
143 − form.source
144 − |> Map.get(:changes)
145 − |> Map.get(:source_id) ||
146 − form.data
147 − |> Map.get(:source_id)
153 + defp type_for_source_id(sources, id) do
154 + case Enum.find(sources, &(&1.id == id)) do
155 + nil -> nil
156 + s -> s.type
157 + end
148 158 end
149 − defp get_current_type(form) do
150 − id = get_source_id(form)
151 159
152 − case id do
153 − nil ->
154 − :ok
160 + defp parse_id(nil), do: nil
161 + defp parse_id(""), do: nil
162 + defp parse_id(id) when is_integer(id), do: id
155 163
156 − _ ->
157 − s = Configuration.get_source!(id)
158 − s.type
164 + defp parse_id(id) when is_binary(id) do
165 + case Integer.parse(id) do
166 + {n, _} -> n
167 + :error -> nil
159 168 end
160 169 end
170 +
171 + defp get_source_id(form) do
172 + form.source
173 + |> Map.get(:changes)
174 + |> Map.get(:source_id) ||
175 + form.data
176 + |> Map.get(:source_id)
177 + end
161 178 end
modified apps/room_sanctum/lib/room_sanctum_web/live/cfg_query_live/form_component.html.heex
+1 −1
@@ -43,7 +43,7 @@
43 43 </div>
44 44 <div class="flex">
45 45 <div class="w-full">
46 − <%= case get_current_type(@form) do %>
46 + <%= case @current_type do %>
47 47 <%= :aqi -> %>
48 48 <%= polymorphic_embed_inputs_for @form, :query, :aqi, fn aqi_form -> %>
49 49 <div class="form-control">

Parents: 49855f9