When using the new LiveView stream feature, I want to disable the actions of an item (in a table) which is already being edited by another user.
For now, the only way I managed to do this is by calling stream_insert/4 even though this item didn’t really change yet. Otherwise, the actions won’t be updated because they are in the <tbody phx-update="stream">
Here is an example of the table component where Presence.is_entity_used(@presence_list, fruit.id)
allows me to know if this item is being edited by another user, and if so, returns the user.
# fruit_live/index.html.heex
<.table id="fruits" rows={@streams.fruits}>
<:col :let={{_id, fruit}} label="Name"><%= fruit.name %></:col>
<:col :let={{_id, fruit}} label="Color"><%= fruit.color %></:col>
<:col :let={{_id, fruit}} label="Quantity"><%= fruit.quantity %></:col>
<:action :let={{_id, fruit}}>
<%= if user = Presence.is_entity_used(@presence_list, fruit.id) -> %>
<%= "This fruit is being edited by #{user.email}" %>
<% else %>
<.link patch={~p"/fruits/#{fruit}/edit"}>Edit</.link>
<.link
phx-click={JS.push("delete", value: %{id: fruit.id})}
data-confirm="Are you sure?"
>
Delete
</.link>
<% end %>
</:action>
</.table>
Here is a sample project implementing this feature.
My guess is I shouldn’t use streams in this case, what do you think?