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: 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
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: 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
maybe7 Replies
๐ So I haven't actually set this up yet, but I think what you want is
AshPhoenix.Form.update_form
๐
actually nvm, this isn't nested
Yeah, so prepare_source
may actually be your best bet.
That or basically hand-rolling this
Hand rolling it looks like this
and then when its time to submit you'd do submit(..., params: Map.put(params, "wallpaper_url", socket.assigns.wallpaper_url))
but you could also do:
the first one is probably better in this case (or perhaps a combination of the two) so that you can use that wallpaper_url
assign to manage the UI state (i.e the upload is complete and this is the url)EDIT: I'm getting
** (KeyError) key :wallpaper_url not found in: %{__changed__: %{}, ...
when I don't change the wallpaper, so I think I might have to assign that to something at mount? But when I upload it persists now, fixed a bug with using an atom instead of a string as a key
Yeah you probably should assign it to
nil
on mount
That looks pretty reasonable to me ๐
At some point I'd like to support file uploads natively in AshPhoenix.Form
so you can just say which params are file uploads or something along those lines.
Would need some workshopping/might not be realistic thoughokay this feels like a breakthrough, excited rn
is there anything devastatingly busted about this approach? it's working :>
still getting used to elixir's scope and mindset of assigning from private functions, instead of the nested if hell i find myself reaching for first. but this is sooo much cleaner ๐ญ
that would be fantastic ๐
Some preliminary research tells me that it should be possible
but not necessarily easy ๐
actually....we could use the form path plus field to make it happen
what does your
consume_uploads
function look like?Yeah so we'd basically just need to get a callback function that gets a path/entry
Sounds interesting. What I'd really like to do is make a file uploading extension for Ash that each API type can use to do certain things
i.e in graphql it would add something like a "presigned_url" action
and in this it would say "hey, gimme a handler for the file", or maybe just do it magically with the extension