index.ex 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. defmodule VaccinsWeb.IndexLive do
  2. use VaccinsWeb, :live_view
  3. alias Vaccins.{LocationStore, Search}
  4. @refresh_period_ms 2 * 1000
  5. @impl true
  6. def mount(_params, _session, socket) do
  7. locations = LocationStore.get_locations()
  8. {:ok,
  9. socket
  10. |> assign(
  11. locations: locations,
  12. locations_with_early_slots: MapSet.new(),
  13. locations_with_slots: MapSet.new(),
  14. pending: %{},
  15. location_cs: LocationStore.LocationRaw.changeset(%{}),
  16. display_cs: false
  17. )
  18. |> trigger_global_refresh
  19. |> trigger_periodic_refresh}
  20. end
  21. @impl true
  22. def handle_params(%{}, _url, socket), do: {:noreply, socket}
  23. @impl true
  24. def handle_event("add_location", %{"location_raw" => params}, socket) do
  25. case params |> LocationStore.add_location() do
  26. :ok -> {:noreply, socket |> push_patch(to: Routes.index_path(socket, :index))}
  27. {:error, cs} -> {:noreply, socket |> assign(location_cs: cs)}
  28. end
  29. end
  30. def handle_event("reload_file", _, socket) do
  31. :ok = LocationStore.reload()
  32. {:noreply, socket |> assign(locations: LocationStore.get_locations())}
  33. end
  34. def handle_event("trigger_all", _, socket) do
  35. {:noreply, socket |> trigger_global_refresh}
  36. end
  37. def handle_event("toggle_form", _, socket = %{assigns: %{display_cs: display}}),
  38. do: {:noreply, socket |> assign(display_cs: not display)}
  39. @impl true
  40. def handle_info({:query_sent, id, ref}, socket = %{assigns: %{pending: pending}}) do
  41. {:noreply, socket |> assign(pending: pending |> Map.put(ref, id))}
  42. end
  43. def handle_info(:periodic_refresh, socket = %{assigns: %{display_cs: false}}),
  44. do:
  45. {:noreply,
  46. socket
  47. |> trigger_global_refresh
  48. |> trigger_periodic_refresh}
  49. def handle_info(:periodic_refresh, socket = %{assigns: %{display_cs: true}}),
  50. do: {:noreply, socket |> trigger_periodic_refresh}
  51. def handle_info({:location_has_slots, id, true}, socket) do
  52. {:noreply, socket |> update(:locations_with_early_slots, &(&1 |> MapSet.put(id)))}
  53. end
  54. def handle_info({:location_has_slots, id, false}, socket) do
  55. {:noreply, socket |> update(:locations_with_slots, &(&1 |> MapSet.put(id)))}
  56. end
  57. def handle_info({:location_no_more_slots, id}, socket) do
  58. {:noreply,
  59. socket
  60. |> update(:locations_with_slots, &(&1 |> MapSet.delete(id)))
  61. |> update(:locations_with_early_slots, &(&1 |> MapSet.delete(id)))}
  62. end
  63. def handle_info({:location_deleted, _}, socket),
  64. do: socket |> push_patch(to: Routes.index_path(socket, :index))
  65. @impl true
  66. def handle_info(
  67. {:query_result, ref, res},
  68. socket = %{assigns: %{locations: valid, pending: pending}}
  69. ) do
  70. id = pending |> Map.get(ref)
  71. send_update(VaccinsWeb.LocationComponent, id: id, availabilities: res)
  72. {:noreply, socket |> assign(pending: pending |> Map.delete(ref))}
  73. end
  74. defp trigger_global_refresh(socket = %{assigns: %{locations: locations}}) do
  75. locations
  76. |> Enum.each(&send_update(VaccinsWeb.LocationComponent, id: &1.id, force_refresh: true))
  77. socket
  78. end
  79. defp trigger_periodic_refresh(socket) do
  80. Process.send_after(self(), :periodic_refresh, @refresh_period_ms)
  81. socket
  82. end
  83. defp locations_by_availability(
  84. assigns = %{
  85. locations: locations,
  86. locations_with_slots: with_slots,
  87. locations_with_early_slots: with_early_slots
  88. }
  89. ) do
  90. locations
  91. |> Enum.sort(fn e1, e2 ->
  92. e1_has_early_spot? = e1.id in with_early_slots
  93. e1_has_spot? = e1.id in with_slots
  94. e2_has_early_spot? = e2.id in with_early_slots
  95. e2_has_spot? = e2.id in with_slots
  96. res =
  97. cond do
  98. e2_has_early_spot? -> false
  99. e2_has_spot? and not e1_has_early_spot? -> false
  100. e2_has_spot? and e1_has_spot? -> false
  101. true -> true
  102. end
  103. end)
  104. end
  105. end