index.ex 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. |> set_title()}
  21. end
  22. defp set_title(socket = %{assigns: %{locations_with_early_slots: early}}) do
  23. if early |> Enum.empty?(),
  24. do: socket |> assign(page_title: "Disponibilités vaccins"),
  25. else: socket |> assign(page_title: "(!!) Disponibilités vaccins")
  26. end
  27. @impl true
  28. def handle_params(params, _url, socket) when params == %{}, do: {:noreply, socket}
  29. def handle_params(%{"noob" => _}, _url, socket),
  30. do: {:noreply, socket |> assign(noob_mode: true)}
  31. @impl true
  32. def handle_event("add_location", %{"location_raw" => params}, socket) do
  33. case params |> LocationStore.add_location() do
  34. :ok -> {:noreply, socket |> push_patch(to: Routes.index_path(socket, :index))}
  35. {:error, cs} -> {:noreply, socket |> assign(location_cs: cs)}
  36. end
  37. end
  38. def handle_event("reload_file", _, socket) do
  39. :ok = LocationStore.reload()
  40. {:noreply, socket |> assign(locations: LocationStore.get_locations())}
  41. end
  42. def handle_event("toggle_form", _, socket = %{assigns: %{display_cs: display}}),
  43. do: {:noreply, socket |> assign(display_cs: not display)}
  44. def handle_event("trigger_noob_mode", _, socket = %{assigns: %{noob_mode: true}}),
  45. do:
  46. {:noreply,
  47. socket |> assign(noob_mode: false) |> push_patch(to: Routes.index_path(socket, :index))}
  48. def handle_event("trigger_noob_mode", _, socket = %{assigns: %{noob_mode: false}}),
  49. do: {:noreply, socket |> push_patch(to: Routes.index_path(socket, :index, noob: true))}
  50. @impl true
  51. def handle_info({:location_has_slots, id, true}, socket) do
  52. {:noreply,
  53. socket |> update(:locations_with_early_slots, &(&1 |> MapSet.put(id))) |> set_title}
  54. end
  55. def handle_info({:location_has_slots, id, false}, socket) do
  56. {:noreply, socket |> update(:locations_with_slots, &(&1 |> MapSet.put(id))) |> set_title}
  57. end
  58. def handle_info({:location_no_more_slots, id}, socket) do
  59. {:noreply,
  60. socket
  61. |> update(:locations_with_slots, &(&1 |> MapSet.delete(id)))
  62. |> update(:locations_with_early_slots, &(&1 |> MapSet.delete(id)))
  63. |> set_title}
  64. end
  65. def handle_info({:location_deleted, _}, socket),
  66. do: socket |> push_patch(to: Routes.index_path(socket, :index))
  67. @impl true
  68. def handle_info({:new_availabilities, id, res}, socket) do
  69. send_update(VaccinsWeb.LocationComponent, id: id, availabilities: res)
  70. {:noreply, socket}
  71. end
  72. defp locations_by_availability(%{
  73. locations: locations,
  74. locations_with_slots: with_slots,
  75. locations_with_early_slots: with_early_slots
  76. }) do
  77. locations
  78. |> Enum.sort(fn e1, e2 ->
  79. e1_has_early_spot? = e1.id in with_early_slots
  80. e1_has_spot? = e1.id in with_slots
  81. e2_has_early_spot? = e2.id in with_early_slots
  82. e2_has_spot? = e2.id in with_slots
  83. cond do
  84. e2_has_early_spot? -> false
  85. e2_has_spot? and not e1_has_early_spot? -> false
  86. e2_has_spot? and e1_has_spot? -> false
  87. true -> true
  88. end
  89. end)
  90. end
  91. end