I have a modal with form and live image upload which uploads selected image on form submit. Also on the form a button for submodal can appear which opens another modal on top of form modal and after closing it you get back to the form. All the changes will remain, except for the image preview which is not in changeset but in uploads.
What I want is the selected image preview remains after submodal close. One of my attempts was to store the entry in changeset i put it back on load/change, which worked but it did not solve my issue.
So I dug deeper and as far as I understand, when I call
live_img_preview
which calls
content_tag
with
data_phx_entry_ref
which is used deep in the javascript
live_uploader.js
For uploads LV maintains a lot more state than you get to interact with (including tmp files and such). Therefore you cannot just set values back like with a changeset. If those entries are to be maintained you want to go for a strategy where LV never get’s rid of those entries in the first place. That might be possible by patching between modals and keeping the upload allowed on both, but I’m not sure about that.
def cleanup_previews(socket) do
Enum.reduce upload_names(), socket, fn n, s ->
{_completed, in_progress} = uploaded_entries(s, n)
Enum.reduce in_progress, s, fn e, s ->
cancel_upload(s, n, e.ref)