Browse Source

Keep track of form changes on server-side

theenglishway (time) 4 years ago
parent
commit
d78b4f3cd9

+ 3 - 1
lib/vaccins/location_store.ex

@@ -99,6 +99,8 @@ defmodule Vaccins.LocationStore do
     GenServer.call(@name, :get_locations)
   end
 
+  def change_location(params), do: params |> LocationRaw.changeset()
+
   def add_location(params) do
     GenServer.call(@name, {:add_location, params})
   end
@@ -116,7 +118,7 @@ defmodule Vaccins.LocationStore do
 
   @impl true
   def handle_call({:add_location, params}, _, state = %{locations: locations}) do
-    cs = params |> LocationRaw.changeset()
+    cs = change_location(params)
 
     with {:ok, raw_location} <- cs |> apply_action(:insert),
          processed <-

+ 4 - 0
lib/vaccins_web/live/index.ex

@@ -75,6 +75,10 @@ defmodule VaccinsWeb.IndexLive do
     end
   end
 
+  def handle_event("add_location_change", %{"location_raw" => params}, socket) do
+    {:noreply, socket |> assign(location_cs: params |> LocationStore.change_location())}
+  end
+
   def handle_event("reload_file", _, socket) do
     :ok = LocationStore.reload()
     {:noreply, socket |> assign(all_locations: LocationStore.get_locations())}

+ 1 - 1
lib/vaccins_web/live/index_live.html.leex

@@ -36,7 +36,7 @@
 <button phx-click="toggle_form">Montrer le formulaire (ou pas)</button><% end %>
 
 <%= if @display_cs do %>
-  <%= f = form_for @location_cs, "#", [phx_submit: :add_location] %>
+  <%= f = form_for @location_cs, "#", [phx_submit: :add_location, phx_change: :add_location_change] %>
     <%= label f, :name %>
     <%= text_input f, :name %>
     <%= error_tag f, :name %>