VERSIONS: phoenix 1.6.6 phoenix_live_view 0.17.9
CONTEXT: I have a child component that collects country, state_region and city data and returns a geo_point (with timezone, latlong, etc). The parent can either be a LiveView OR another component.
ISSUE: Using send self() always sends the data up to the LiveView, but I need it to sometimes go to another component. So now I have the code below to switch between send self() and send_update depending on the parent_type. It also means that to use this component, the parent liveview or component must set a “parent_type” assign of either “component” or “liveview.”
This works but seems a bit clunky to me. Am I doing this right?
defp notify_parent(socket) do
data = { %{"geo_status" => socket.assigns.geo_status,
"geo_point" => socket.assigns.geo_point,
"country" => socket.assigns.country,
"state" => socket.assigns.state,
"city" => socket.assigns.city }}
case socket.assigns.parent_type do
"liveview" -> send self(), {:geo_data, data}
"component" -> send_update(socket.assigns.module,
id: socket.assigns.parent_id,
update: :geo_data, data: data)
end
socket
end