index.ex 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. defmodule VaccinsWeb.IndexLive do
  2. use VaccinsWeb, :live_view
  3. alias Vaccins.{LocationStore, Search}
  4. @refresh_period_ms 30 * 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("trigger_all", _, socket) do
  31. {:noreply, socket |> trigger_global_refresh}
  32. end
  33. def handle_event("toggle_form", _, socket = %{assigns: %{display_cs: display}}),
  34. do: {:noreply, socket |> assign(display_cs: not display)}
  35. @impl true
  36. def handle_info({:query_sent, id, ref}, socket = %{assigns: %{pending: pending}}) do
  37. {:noreply, socket |> assign(pending: pending |> Map.put(ref, id))}
  38. end
  39. def handle_info(:periodic_refresh, socket),
  40. do:
  41. {:noreply,
  42. socket
  43. |> trigger_global_refresh
  44. |> trigger_periodic_refresh}
  45. def handle_info({:location_has_slots, id, true}, socket) do
  46. {:noreply, socket |> update(:locations_with_early_slots, &(&1 |> MapSet.put(id)))}
  47. end
  48. def handle_info({:location_has_slots, id, false}, socket) do
  49. {:noreply, socket |> update(:locations_with_slots, &(&1 |> MapSet.put(id)))}
  50. end
  51. def handle_info({:location_no_more_slots, id}, socket) do
  52. {:noreply,
  53. socket
  54. |> update(:locations_with_slots, &(&1 |> MapSet.delete(id)))
  55. |> update(:locations_with_early_slots, &(&1 |> MapSet.delete(id)))}
  56. end
  57. def handle_info({:location_deleted, _}, socket),
  58. do: socket |> push_patch(to: Routes.index_path(socket, :index))
  59. @impl true
  60. def handle_info(
  61. {:query_result, ref, res},
  62. socket = %{assigns: %{locations: valid, pending: pending}}
  63. ) do
  64. id = pending |> Map.get(ref)
  65. send_update(VaccinsWeb.LocationComponent, id: id, availabilities: res)
  66. {:noreply, socket |> assign(pending: pending |> Map.delete(ref))}
  67. end
  68. defp trigger_global_refresh(socket = %{assigns: %{locations: locations}}) do
  69. locations
  70. |> Enum.each(&send_update(VaccinsWeb.LocationComponent, id: &1.id, force_refresh: true))
  71. socket
  72. end
  73. defp trigger_periodic_refresh(socket) do
  74. Process.send_after(self(), :periodic_refresh, @refresh_period_ms)
  75. socket
  76. end
  77. defp locations_by_availability(
  78. assigns = %{
  79. locations: locations,
  80. locations_with_slots: with_slots,
  81. locations_with_early_slots: with_early_slots
  82. }
  83. ) do
  84. locations
  85. |> Enum.sort(fn e1, e2 ->
  86. e1_has_early_spot? = e1.id in with_early_slots
  87. e1_has_spot? = e1.id in with_slots
  88. e2_has_early_spot? = e2.id in with_early_slots
  89. e2_has_spot? = e2.id in with_slots
  90. res =
  91. cond do
  92. e2_has_early_spot? -> false
  93. e2_has_spot? and not e1_has_early_spot? -> false
  94. e2_has_spot? and e1_has_spot? -> false
  95. true -> true
  96. end
  97. end)
  98. end
  99. end