Ash FrameworkAF
Ash Framework3y ago
29 replies
axdc

LiveView uploads with AshPhoenix.Form

Hello,

I've got a form that allows the user to select from a few example images, or to upload their own and use that.

Right now it's a set of radio buttons plus a live_file_input. I've gotten it ~working but it feels pretty dirty and hacky with some showstopping bugs so I'm looking for prior art and best practices when it comes to the Ash Way to do live uploads.

I've searched here and it doesn't look like there's a specific upload extension or type yet, but in the meantime, is there a better or recommended way to do what I'm trying to do?

Specifically this:
  defp maybe_put_uploaded_wallpaper_url(socket, form) do
    wallpaper_url = consume_uploads(socket) |> List.first()

    if(wallpaper_url) do
      # a wallpaper was uploaded
      Logger.info("handling uploaded wallpaper")
      form = form |> AshPhoenix.Form.set_data(%{form.data | wallpaper_url: wallpaper_url})
      form = %{form | params: %{form.params | "wallpaper_url" => wallpaper_url}}

      form = %{
        form
        | source: %{
            form.source
            | params: %{form.source.params | "wallpaper_url" => wallpaper_url}
          }
      }

      IO.inspect(form)
    else
      # pass through form unchanged
      form
    end
  end


This feels like sin to me. The liveview file upload examples have you patching params but I don't have params, I have an AshPhoenix.Form.

One bug example: If I change the image, save, and then change it back to what it originally was, it looks like it persists but actually doesn't. I suspect this may have something to do with how I'm setting the checked attribute?

my index.ex: https://gitlab.com/avoh-labs/panacea/-/blob/main/lib/panacea_web/live/rosegarden/configuration_live/index.ex

my index.html.heex: https://gitlab.com/avoh-labs/panacea/-/blob/main/lib/panacea_web/live/rosegarden/configuration_live/index.html.heex#L597

Thank you. Posting here is always clarifying. chasing down some more threads now. prepare_source maybe
Was this page helpful?