index.ex 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. defmodule VaccinsWeb.IndexLive do
  2. use VaccinsWeb, :live_view
  3. alias Vaccins.LocationStore
  4. @impl true
  5. def mount(_params, %{"is_local?" => is_local?}, socket) do
  6. locations = LocationStore.get_locations()
  7. Phoenix.PubSub.subscribe(Vaccins.PubSub, "locations")
  8. {:ok,
  9. socket
  10. |> assign(
  11. noob_mode: false,
  12. is_local?: is_local?,
  13. locations: locations,
  14. locations_with_early_slots: MapSet.new(),
  15. locations_with_slots: MapSet.new(),
  16. pending: %{},
  17. location_cs: LocationStore.LocationRaw.changeset(%{}),
  18. display_cs: false
  19. )
  20. |> init_area_filters()
  21. |> set_title()}
  22. end
  23. defp set_title(socket = %{assigns: %{locations_with_early_slots: early}}) do
  24. if early |> Enum.empty?(),
  25. do: socket |> assign(page_title: "Disponibilités vaccins"),
  26. else: socket |> assign(page_title: "(!!) Disponibilités vaccins")
  27. end
  28. @impl true
  29. def handle_params(params, _url, socket) when params == %{}, do: {:noreply, socket}
  30. def handle_params(params, _url, socket)
  31. when is_map_key(params, "noob") or is_map_key(params, "geographic_areas") do
  32. all_areas = get_all_areas(socket)
  33. areas = params |> Map.get("geographic_areas", all_areas)
  34. {:noreply,
  35. socket
  36. |> assign(
  37. noob_mode: params |> Map.has_key?("noob"),
  38. area_filters: areas |> MapSet.new() |> MapSet.intersection(all_areas),
  39. params: params
  40. )}
  41. end
  42. @impl true
  43. def handle_event("add_location", %{"location_raw" => params}, socket) do
  44. case params |> LocationStore.add_location() do
  45. :ok -> {:noreply, socket |> push_patch(to: Routes.index_path(socket, :index))}
  46. {:error, cs} -> {:noreply, socket |> assign(location_cs: cs)}
  47. end
  48. end
  49. def handle_event("reload_file", _, socket) do
  50. :ok = LocationStore.reload()
  51. {:noreply, socket |> assign(locations: LocationStore.get_locations())}
  52. end
  53. def handle_event("toggle_form", _, socket = %{assigns: %{display_cs: display}}),
  54. do: {:noreply, socket |> assign(display_cs: not display)}
  55. def handle_event("trigger_noob_mode", _, socket = %{assigns: %{noob_mode: true}}),
  56. do:
  57. {:noreply,
  58. socket |> assign(noob_mode: false) |> push_patch(to: Routes.index_path(socket, :index))}
  59. def handle_event("trigger_noob_mode", _, socket = %{assigns: %{noob_mode: false}}),
  60. do: {:noreply, socket |> push_patch(to: Routes.index_path(socket, :index, noob: true))}
  61. @impl true
  62. def handle_info({:location_has_slots, id, true}, socket) do
  63. {:noreply,
  64. socket |> update(:locations_with_early_slots, &(&1 |> MapSet.put(id))) |> set_title}
  65. end
  66. def handle_info({:location_has_slots, id, false}, socket) do
  67. {:noreply, socket |> update(:locations_with_slots, &(&1 |> MapSet.put(id))) |> set_title}
  68. end
  69. def handle_info({:location_no_more_slots, id}, socket) do
  70. {:noreply,
  71. socket
  72. |> update(:locations_with_slots, &(&1 |> MapSet.delete(id)))
  73. |> update(:locations_with_early_slots, &(&1 |> MapSet.delete(id)))
  74. |> set_title}
  75. end
  76. def handle_info({:location_deleted, _}, socket),
  77. do: socket |> push_patch(to: Routes.index_path(socket, :index))
  78. @impl true
  79. def handle_info({:new_availabilities, id, res}, socket) do
  80. send_update(VaccinsWeb.LocationComponent, id: id, availabilities: res)
  81. {:noreply, socket}
  82. end
  83. defp filter(locations, assigns = %{area_filters: filters}),
  84. do:
  85. locations
  86. |> Enum.filter(&(&1.geographic_area in filters))
  87. defp locations_by_availability(%{
  88. locations: locations,
  89. locations_with_slots: with_slots,
  90. locations_with_early_slots: with_early_slots
  91. }) do
  92. locations
  93. |> Enum.sort(fn e1, e2 ->
  94. e1_has_early_spot? = e1.id in with_early_slots
  95. e1_has_spot? = e1.id in with_slots
  96. e2_has_early_spot? = e2.id in with_early_slots
  97. e2_has_spot? = e2.id in with_slots
  98. cond do
  99. e2_has_early_spot? -> false
  100. e2_has_spot? and not e1_has_early_spot? -> false
  101. e2_has_spot? and e1_has_spot? -> false
  102. true -> true
  103. end
  104. end)
  105. end
  106. defp init_area_filters(socket), do: socket |> assign(area_filters: socket |> get_all_areas)
  107. defp get_all_areas(%{assigns: %{locations: locations}}),
  108. do: locations |> Enum.map(& &1.geographic_area) |> MapSet.new()
  109. end