Просмотр исходного кода

Handle all params in the same function

theenglishway (time) 4 лет назад
Родитель
Сommit
08b0974c26
1 измененных файлов с 11 добавлено и 7 удалено
  1. 11 7
      lib/vaccins_web/live/index.ex

+ 11 - 7
lib/vaccins_web/live/index.ex

@@ -32,14 +32,18 @@ defmodule VaccinsWeb.IndexLive do
   @impl true
   def handle_params(params, _url, socket) when params == %{}, do: {:noreply, socket}
 
-  def handle_params(%{"noob" => _}, _url, socket),
-    do: {:noreply, socket |> assign(noob_mode: true)}
+  def handle_params(params, _url, socket)
+      when is_map_key(params, "noob") or is_map_key(params, "geographic_areas") do
+    all_areas = get_all_areas(socket)
+    areas = params |> Map.get("geographic_areas", all_areas)
 
-  def handle_params(%{"geographic_areas" => areas}, _url, socket) when is_list(areas) do
-    all = get_all_areas(socket)
-    areas |> MapSet.new() |> MapSet.intersection(all)
-
-    {:noreply, socket |> assign(area_filters: areas)}
+    {:noreply,
+     socket
+     |> assign(
+       noob_mode: params |> Map.has_key?("noob"),
+       area_filters: areas |> MapSet.new() |> MapSet.intersection(all_areas),
+       params: params
+     )}
   end
 
   @impl true