I have a select multiple
tag which lets users quickly click on a customer and toggle their assignment to a location, salesperson, etc.
<select class="form-control customer-select select--dual-list" multiple>
<%= for customer <- @unassigned_customers do %>
<option value="<%= customer(customer, :id) %>"
phx-value-customer-name="<%= customer(customer, :name) %>"
phx-click="customer_add<%= is_update?(@update) %>"
phx-target="<%= @myself %>"
class="<%= customer(customer, :added) %>"><%= customer(customer, :name) %></option>
<% end %>
</select>
def handle_event("customer_add", %{"customer-name" => name, "value" => id}, socket),
do: customer_toggle(socket, id, name, :add)
def handle_event("customer_remove", %{"customer-name" => name, "value" => id}, socket),
do: customer_toggle(socket, id, name, :remove)
def handle_event("customer_add-update", %{"customer-name" => name, "value" => id}, socket),
do: update_customer_toggle(socket, id, name, :add)
def customer_toggle(
%{
assigns: %{
assigned_customers: assigned,
unassigned_customers: unassigned
}
} = socket,
id,
name,
direction
) do
{assigned, unassigned} =
case direction do
:add ->
assigned = [{name, id, "added"} | assigned]
unassigned = Enum.filter(unassigned, &(elem(&1, 1) != id))
{Enum.sort_by(assigned, &elem(&1, 0)), Enum.sort_by(unassigned, &elem(&1, 0))}
:remove ->
assigned = Enum.filter(assigned, &(elem(&1, 1) != id))
unassigned = unassigned ++ [{name, id, "removed"}]
{Enum.sort_by(assigned, &elem(&1, 0)), Enum.sort_by(unassigned, &elem(&1, 0))}
_ ->
{assigned, unassigned}
end
socket
|> assign(:unassigned_customers, unassigned)
|> assign(:assigned_customers, assigned)
|> noreply()
end
The desired effect is that customer_add
will will remove the customer from the “unassigned” list and pass them into an “assigned” list.
This was working until I upgrade to Phoenix .16+, however now the updated assigns, specifically customers removed from a list are still showing in the list.
Edit: Phoenix upgrade is not the issue… I just switched branches and saw it’s still happening, but not on production. This is helpful because I only have about half a dozen commits between my current branch and production. When I sort this out I’ll post an explanation just for posterity.
I’ve tried adding inspect statements inside the for statement to see what it the @unassigned_customers
is, and the correct customer is being removed with each click, but the UI isn’t reflecting that. Nothing in the LiveView changed with these updates, and initially nothing changed with the template, although I have played around changing to heex
or extracting the option
tag into it’s own component- neither of which seemed to make a difference.
PS. Please excuse the Quickbooks developer account customer names…