Programming Phoenix LiveView: Live Upload Match Error

Book: Programming Phoenix LiveView, page 142 (157/378), file lib/pento_web/live/product_live/form_component.ex, in the function below:

defp handle_progress(:image, entry, socket) do
    :timer.sleep(200)

    if entry.done? do
      # here instead of {:ok, path} I changed to path only, and it worked
      path =
        consume_uploaded_entry(
          socket,
          entry,
          &upload_static_file(&1, socket)
        )

      {:noreply,
       socket
       |> put_flash(:info, "file #{entry.client_name} uploaded")
       |> assign(:image_upload, path)}
    else
      {:noreply, socket}
    end
  end

Let me know if this is correct, because here It only worked when I changed.

Docs:
https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html#consume_uploaded_entry/3-examples

4 Likes

I ran into this too. It looks like the function Phoenix.LiveView.Upload.consume_entries has been changed to return just the results in LiveView 0.17.9.

1 Like

Ran into this issue as well today, it didn’t jump out in the changelog.md that this change had been made.

Adjusting the code to what you wrote above corrected the file upload issue. Thanks.

I ran into this problem today. Changing the code allows for the file to upload and save, but I did still have the following problems.

  1. The file input would show the file name and then it would disappear.
  2. The flash for saving the file wouldn’t show up.

Saving the product would save the file and the path into the database.

Just about to post here too. All working now?

Same here. File name doesn’t stick. Image uploads and saves with drag and drop and file OS picker UI. No flash either.

I’m on page 144 and am getting this not found “upload_image_error”

We’ll correct this in the next release, but in the meantime–the upload_image_error/2 function is a helper function defined in the form component module. You can add it like this:

# lib/pento_web/product_live/form_component.html.heex
def upload_image_error(%{image: %{errors: errors}}, entry) when length(errors) > 0 do
  {_, msg} =
    Enum.find(errors, fn {ref, _} ->
      ref == entry.ref || ref == entry.upload_ref
    end)

  upload_error_msg(msg)
end

def upload_image_error(_, _), do: ""

defp upload_error_msg(:not_accepted) do
  "Invalid file type"
end

defp upload_error_msg(:too_many_files) do
  "Too many files"
end

defp upload_error_msg(:too_large) do
  "File exceeds max size"
end

For the issues that are being described re: the flash message and filename–we’ll dig into these further as we prepare for a non-trivial code upgrade for the latest versions of LiveView once 0.18 is released in the coming weeks and months. Stay tuned for updates in the meantime I recommend moving on to the next chapters.

Thanks all for your valuable input and feedback!

1 Like

Hey @SophieDeBenedetto, I know you’ve mentioned recently that you will be doing an update to the book once LiveView hits 1.0. With your comment above about v0.18, are you planning on updating the code earlier since v0.18 introduced some significant changes?

Hello! We are hard at work on a major revision right now and plan to release a new version of the book before the end of February.

1 Like