| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- defmodule VaccinsWeb.IndexLive do
- use VaccinsWeb, :live_view
- alias Vaccins.{LocationStore, Search}
- @impl true
- def mount(_params, _session, socket) do
- locations = LocationStore.get_locations()
- {:ok,
- socket
- |> assign(
- locations: locations,
- pending: %{},
- availabilities: locations |> Map.new(&{&1.id, []}),
- location_cs: LocationStore.LocationRaw.changeset(%{})
- )
- |> trigger_global_refresh}
- end
- @impl true
- def handle_event("add_location", %{"location_raw" => params}, socket) do
- case params |> LocationStore.add_location() do
- :ok -> {:noreply, socket}
- {:error, cs} -> {:noreply, socket |> assign(location_cs: cs)}
- end
- end
- @impl true
- def handle_event("trigger_all", _, socket) do
- {:noreply, socket |> trigger_global_refresh}
- end
- @impl true
- def handle_info({:query_sent, id, ref}, socket = %{assigns: %{pending: pending}}) do
- {:noreply, socket |> assign(pending: pending |> Map.put(ref, id))}
- end
- @impl true
- def handle_info(
- {:query_result, ref, res},
- socket = %{assigns: %{locations: valid, pending: pending}}
- ) do
- id = pending |> Map.get(ref)
- send_update(VaccinsWeb.LocationComponent, id: id, availabilities: res)
- {:noreply, socket |> assign(pending: pending |> Map.delete(ref))}
- end
- defp trigger_global_refresh(socket = %{assigns: %{locations: locations}}) do
- locations
- |> Enum.each(&send_update(VaccinsWeb.LocationComponent, id: &1.id, force_refresh: true))
- socket
- end
- end
|