index.ex 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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(%{"noob" => _}, _url, socket),
  31. do: {:noreply, socket |> assign(noob_mode: true)}
  32. def handle_params(%{"geographic_areas" => areas}, _url, socket) when is_list(areas) do
  33. all = get_all_areas(socket)
  34. areas |> MapSet.new() |> MapSet.intersection(all)
  35. {:noreply, socket |> assign(area_filters: areas)}
  36. end
  37. @impl true
  38. def handle_event("add_location", %{"location_raw" => params}, socket) do
  39. case params |> LocationStore.add_location() do
  40. :ok -> {:noreply, socket |> push_patch(to: Routes.index_path(socket, :index))}
  41. {:error, cs} -> {:noreply, socket |> assign(location_cs: cs)}
  42. end
  43. end
  44. def handle_event("reload_file", _, socket) do
  45. :ok = LocationStore.reload()
  46. {:noreply, socket |> assign(locations: LocationStore.get_locations())}
  47. end
  48. def handle_event("toggle_form", _, socket = %{assigns: %{display_cs: display}}),
  49. do: {:noreply, socket |> assign(display_cs: not display)}
  50. def handle_event("trigger_noob_mode", _, socket = %{assigns: %{noob_mode: true}}),
  51. do:
  52. {:noreply,
  53. socket |> assign(noob_mode: false) |> push_patch(to: Routes.index_path(socket, :index))}
  54. def handle_event("trigger_noob_mode", _, socket = %{assigns: %{noob_mode: false}}),
  55. do: {:noreply, socket |> push_patch(to: Routes.index_path(socket, :index, noob: true))}
  56. @impl true
  57. def handle_info({:location_has_slots, id, true}, socket) do
  58. {:noreply,
  59. socket |> update(:locations_with_early_slots, &(&1 |> MapSet.put(id))) |> set_title}
  60. end
  61. def handle_info({:location_has_slots, id, false}, socket) do
  62. {:noreply, socket |> update(:locations_with_slots, &(&1 |> MapSet.put(id))) |> set_title}
  63. end
  64. def handle_info({:location_no_more_slots, id}, socket) do
  65. {:noreply,
  66. socket
  67. |> update(:locations_with_slots, &(&1 |> MapSet.delete(id)))
  68. |> update(:locations_with_early_slots, &(&1 |> MapSet.delete(id)))
  69. |> set_title}
  70. end
  71. def handle_info({:location_deleted, _}, socket),
  72. do: socket |> push_patch(to: Routes.index_path(socket, :index))
  73. @impl true
  74. def handle_info({:new_availabilities, id, res}, socket) do
  75. send_update(VaccinsWeb.LocationComponent, id: id, availabilities: res)
  76. {:noreply, socket}
  77. end
  78. defp filter(locations, assigns = %{area_filters: filters}),
  79. do:
  80. locations
  81. |> Enum.filter(&(&1.geographic_area in filters))
  82. defp locations_by_availability(%{
  83. locations: locations,
  84. locations_with_slots: with_slots,
  85. locations_with_early_slots: with_early_slots
  86. }) do
  87. locations
  88. |> Enum.sort(fn e1, e2 ->
  89. e1_has_early_spot? = e1.id in with_early_slots
  90. e1_has_spot? = e1.id in with_slots
  91. e2_has_early_spot? = e2.id in with_early_slots
  92. e2_has_spot? = e2.id in with_slots
  93. cond do
  94. e2_has_early_spot? -> false
  95. e2_has_spot? and not e1_has_early_spot? -> false
  96. e2_has_spot? and e1_has_spot? -> false
  97. true -> true
  98. end
  99. end)
  100. end
  101. defp init_area_filters(socket), do: socket |> assign(area_filters: socket |> get_all_areas)
  102. defp get_all_areas(%{assigns: %{locations: locations}}),
  103. do: locations |> Enum.map(& &1.geographic_area) |> MapSet.new()
  104. end