Skip to content

Instantly share code, notes, and snippets.

@benvp
Created July 17, 2020 12:56
Show Gist options
  • Save benvp/73b08d04f32b31a4e4a019efa097e08c to your computer and use it in GitHub Desktop.
Save benvp/73b08d04f32b31a4e4a019efa097e08c to your computer and use it in GitHub Desktop.
def handle_event("select", %{"id" => id}, socket) do
item = Items.get_item(id)
selected = Enum.uniq_by([item] ++ socket.assigns.selected, & &1.id)
suggestions = filter_selected(socket.assigns.suggestions, selected)
socket =
assign(socket,
selected: selected,
suggestions: suggestions
)
{:noreply, socket}
end
defp filter_selected(items, selected) do
Enum.filter(items, fn i -> !Enum.any?(selected, fn s -> i.id == s.id end) end)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment