fix: decode beacons_json before mutating in patch/remove helpers
Some checks failed
Build and Push / Build and Push Docker Image (push) Failing after 13m23s

patch_beacons_json/2 and remove_from_beacons_json/2 were calling
Enum.reject/2 directly on @beacons_json, which is a JSON-encoded
string from Jason.encode!/0 (not a list). This crashed with
Protocol.UndefinedError when no approved beacons existed (string
was "[]") and a PubSub broadcast triggered an update.

Decode with Jason.decode!/1, operate on the list, then re-encode
with Jason.encode!/1 so the template's data-beacons attribute
remains a valid JSON string for the JS hook.
This commit is contained in:
Graham McIntire 2026-08-04 17:10:26 -05:00
parent d35239ba1e
commit 63b2d405b6
No known key found for this signature in database
GPG key ID: F4ABF488E6029E59

View file

@ -139,14 +139,16 @@ defmodule MicrowavepropWeb.BeaconLive.Index do
# Replace or add a beacon in the in-memory JSON list without re-querying the DB.
defp patch_beacons_json(%{assigns: %{beacons_json: json}} = socket, beacon) do
entry = encode_single_beacon(beacon)
updated = Enum.reject(json, &(Map.get(&1, "id") == Map.get(entry, "id"))) ++ [entry]
assign(socket, :beacons_json, updated)
list = Jason.decode!(json)
updated = Enum.reject(list, &(Map.get(&1, "id") == Map.get(entry, "id"))) ++ [entry]
assign(socket, :beacons_json, Jason.encode!(updated))
end
defp remove_from_beacons_json(%{assigns: %{beacons_json: json}} = socket, beacon) do
id_str = beacon.id |> Ecto.UUID.cast!() |> to_string()
updated = Enum.reject(json, &(Map.get(&1, "id") == id_str))
assign(socket, :beacons_json, updated)
list = Jason.decode!(json)
updated = Enum.reject(list, &(Map.get(&1, "id") == id_str))
assign(socket, :beacons_json, Jason.encode!(updated))
end
defp encode_single_beacon(beacon) do