Possibly related to Liveview form keeps on refreshing every 15s , but my problem is slightly different IMO.
I have a callback (“status”) that should only be firing when the value is changed in the select box on the form. Unfortunately, it is firing when the form is idle and the liveview heartbeat happens. This problem is compounded by the fact that it sends out a notification and spams the mobile user’s app.
I’ve provided the select and callback code blocks below:
<td class="td">
<div class="flex-container <%= get_status_class(row) %>">
<div class="dropdown-wrapper">
<%= f = form_for :f, "#", [class: "form form-group", phx_change: :status] %>
<%= hidden_input f, :sub_id, value: row.subscription_id %>
<%= hidden_input f, :user_id, value: row.user_id %>
<%= hidden_input f, :dep_id, value: row.dependent_id %>
<%= case row do %>
<% %{connection: :inactive} -> %>
<%= select f, :status, @paused_statuses, selected: row.connection, disabled: true, class: "flex-item MR15 dropdown js-status-dropdown", id: "status-#{row.subscription_id}-#{row.id}" %>
<% _ -> %>
<%= select f, :status, @connection_statuses, selected: row.connection, class: "flex-item MR15 dropdown js-status-dropdown", id: "status-#{row.subscription_id}-#{row.id}" %>
<% end %>
</form>
</div>
</div>
</td>
@impl true
def handle_event(
"status",
%{"f" => %{"status" => status, "sub_id" => id, "user_id" => user_id, "dep_id" => ""}},
socket
) do
_ =
Logger.info(
"#{__MODULE__}: event: status: #{inspect(status)}. sub_id: #{inspect(id)}, user_id: #{
inspect(user_id)
}"
)
status = String.to_atom(status)
case Connection.update_status(id, status) do
:error ->
_ =
Logger.info(
"#{__MODULE__}: event: Failed to update: #{inspect(status)}. sub_id: #{inspect(id)}, user_id: #{
inspect(user_id)
}"
)
{:noreply, socket}
:ok ->
# Update the assign data with the new status value to update the view
{:noreply, assign(socket, data: update_view_status(socket.assigns.data, user_id, status))}
end
end
Any thoughts?