Hello! I’m having an issue getting stream_insert
to replace the entire collection. Inside a handle_event/3
function, I’m doing:
{:noreply, stream_insert(socket, :projects, projects, reset: true)}
However, I’m getting an error:
** (ArgumentError) expected stream :projects to be a struct or map with :id key, got: [...]
If you would like to generate custom DOM id's based on other keys, use stream_configure/3 with the :dom_id option beforehand.
I’m using the exact same function to set the initial stream that I’m attempting to use for the replacement, which is: projects = Projects.list_projects()
The handle_event/3
shuffles the order of the projects, so I don’t want to manually delete and insert each project that gets shuffled.
Here’s the mount code:
def mount(_params, _session, socket) do
form = Projects.change_order() |> to_form()
projects = Projects.list_projects()
socket =
socket
|> assign(form: form, project_list: projects)
{:ok, stream(socket, :projects, projects)}
end
and handle_event:
def handle_event(
"re-order",
%{"project" => %{"id" => id, "order" => order, "original_order" => orig}},
socket
) do
id_int = if id != "", do: String.to_integer(id), else: nil
order_int = if order != "", do: String.to_integer(order), else: nil
orig_int = if orig != "", do: String.to_integer(orig), else: nil
if order_int == orig_int || (is_nil(id_int) && is_nil(orig_int)) do
{:noreply, socket}
else
if is_nil(orig_int) do
Projects.update_order(id_int, order_int)
else
Projects.shuffle(orig_int, order_int)
end
form = Projects.change_order() |> to_form()
projects = Projects.list_projects()
socket =
socket
|> assign(form: form, project_list: projects)
{:noreply, stream_insert(socket, :projects, projects, reset: true)}
end
end
Am I missing something obvious?