ソースを参照

Add delete action

theenglishway (time) 4 年 前
コミット
5f751b93f2

+ 5 - 0
assets/css/app.scss

@@ -96,6 +96,11 @@ ul.locations-list {
   list-style: none;
 }
 
+ul.actions-list {
+  display: inline-flex;
+  list-style: none;
+}
+
 dl.location {
   display: grid;
   grid-template-columns: min-content 1fr;

+ 4 - 0
lib/vaccins/location_store.ex

@@ -111,4 +111,8 @@ defmodule Vaccins.LocationStore do
       e = {:error, _} -> e
     end
   end
+
+  def delete_location(id) do
+    :dets.delete(@name, id)
+  end
 end

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

@@ -25,7 +25,7 @@ defmodule VaccinsWeb.IndexLive do
   @impl true
   def handle_event("add_location", %{"location_raw" => params}, socket) do
     case params |> LocationStore.add_location() do
-      :ok -> {:noreply, socket}
+      :ok -> {:noreply, socket |> push_patch(to: Routes.index_path(socket, :index))}
       {:error, cs} -> {:noreply, socket |> assign(location_cs: cs)}
     end
   end
@@ -64,6 +64,9 @@ defmodule VaccinsWeb.IndexLive do
      |> update(:locations_with_early_slots, &(&1 |> MapSet.delete(id)))}
   end
 
+  def handle_info({:location_deleted, _}, socket),
+    do: socket |> push_patch(to: Routes.index_path(socket, :index))
+
   @impl true
   def handle_info(
         {:query_result, ref, res},

+ 12 - 2
lib/vaccins_web/live/location_component.ex

@@ -38,6 +38,11 @@ defmodule VaccinsWeb.LocationComponent do
     {:noreply, socket |> assign(loading: true, last_refresh_date: get_refresh_time())}
   end
 
+  def handle_event("delete", _, socket = %{assigns: %{id: id}}) do
+    if LocationStore.delete_location(id), do: send(self(), {:location_deleted, id})
+    {:noreply, socket}
+  end
+
   @impl true
   def render(assigns) do
     ~L"""
@@ -64,8 +69,13 @@ defmodule VaccinsWeb.LocationComponent do
             <ul><%= for d <- @slots_after do %><li><%= d |> DateTime.to_string %></li><% end %></ul>
           </dd>
         <% end %>
-        <dt>test availability</dt>
-        <dd><button phx-click="trigger_query" phx-target="<%= @myself %>">Trigger</button></dd>
+        <dt>actions</dt>
+        <dd>
+          <ul class="actions-list">
+            <li><button phx-click="trigger_query" phx-target="<%= @myself %>">Trigger</button></li>
+            <li><button class="alert-danger" phx-click="delete" phx-target="<%= @myself %>" data-confirm="Etes-vous sur?">Delete</button></li>
+          </ul>
+        </dd>
       </dl>
     """
   end