index.ex 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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("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),
  44. do:
  45. {:noreply,
  46. socket
  47. |> trigger_global_refresh
  48. |> trigger_periodic_refresh}
  49. def handle_info({:location_has_slots, id, true}, socket) do
  50. {:noreply, socket |> update(:locations_with_early_slots, &(&1 |> MapSet.put(id)))}
  51. end
  52. def handle_info({:location_has_slots, id, false}, socket) do
  53. {:noreply, socket |> update(:locations_with_slots, &(&1 |> MapSet.put(id)))}
  54. end
  55. def handle_info({:location_no_more_slots, id}, socket) do
  56. {:noreply,
  57. socket
  58. |> update(:locations_with_slots, &(&1 |> MapSet.delete(id)))
  59. |> update(:locations_with_early_slots, &(&1 |> MapSet.delete(id)))}
  60. end
  61. def handle_info({:location_deleted, _}, socket),
  62. do: socket |> push_patch(to: Routes.index_path(socket, :index))
  63. @impl true
  64. def handle_info(
  65. {:query_result, ref, res},
  66. socket = %{assigns: %{locations: valid, pending: pending}}
  67. ) do
  68. id = pending |> Map.get(ref)
  69. send_update(VaccinsWeb.LocationComponent, id: id, availabilities: res)
  70. {:noreply, socket |> assign(pending: pending |> Map.delete(ref))}
  71. end
  72. defp trigger_global_refresh(socket = %{assigns: %{locations: locations}}) do
  73. locations
  74. |> Enum.each(&send_update(VaccinsWeb.LocationComponent, id: &1.id, force_refresh: true))
  75. socket
  76. end
  77. defp trigger_periodic_refresh(socket) do
  78. Process.send_after(self(), :periodic_refresh, @refresh_period_ms)
  79. socket
  80. end
  81. defp locations_by_availability(
  82. assigns = %{
  83. locations: locations,
  84. locations_with_slots: with_slots,
  85. locations_with_early_slots: with_early_slots
  86. }
  87. ) do
  88. locations
  89. |> Enum.sort(fn e1, e2 ->
  90. e1_has_early_spot? = e1.id in with_early_slots
  91. e1_has_spot? = e1.id in with_slots
  92. e2_has_early_spot? = e2.id in with_early_slots
  93. e2_has_spot? = e2.id in with_slots
  94. res =
  95. cond do
  96. e2_has_early_spot? -> false
  97. e2_has_spot? and not e1_has_early_spot? -> false
  98. e2_has_spot? and e1_has_spot? -> false
  99. true -> true
  100. end
  101. end)
  102. end
  103. end