I’m a bit at the end of my knowhow with how to implement a duplicate feature in liveview. The idea is to click duplicate on the resource, and it opens a new form with most values applied from the original resource. The resource should not be created immediately, because the user needs to adjust some fields before they can create the resource.
My current approach is to use turn the original source into parameters and create a changeset on an empty resource, using these parameters.
params = Resource.duplicate(original)
changeset = Resource.changeset(%Resource{}, params)
For the most part, this works just as expected, but when I try to delete a dynamic resource and then try to add a new one I get a strange behaviour. In this example I have 3 resources
- Click delete on embed #2- embed #2 is deleted
- Click add new embed - embed #3 which moved to position 2 was replaced with a new embed
The parameters during these actions look like this:
# Pressing delete
%{"resource" => %{
"embed_drop" => ["1", ""],
"embed_sort" => ["0", "1", "2"],
"embed" => %{
"0" => embed1,
"1" => embed2,
"2" => embed3
}
}
# Pressing "add new"
%{"resource" => %{
"embed_drop" => ["1", ""],
"embed_sort" => ["0", "1", "on"],
"embed" => %{
"0" => embed1,
"1" => embed3
}
}
HTML form used, reduced to the minimum necessary to reproduce this case (inspired by Phoenix.Component.inputs_for/1)
<.inputs_for :let={fp} field={@form[:embeds]}>
<input type="hidden" name="resource[embeds_sort][]" value={fp.index} />
<%= gettext("embed #") %><%= fp.index + 1 %> <%= input_value(fp, :name) %>
<label>
<input type="checkbox" name="resource[embeds_drop][]" value={fp.index} class="hidden" />
<i class="icon-trash"></i> <%= gettext("Remove") %>
</label>
</.inputs_for>
<input type="hidden" name="resource[embeds_drop][]" />
<label>
<input type="checkbox" name="resource[embeds_sort][]" class="hidden" />
<i class="icon-plus"></i>
<%= gettext("Add embed") %>
</label>
Is there a better way to implement this feature, or did I make a mistake when copying this form from the phoenix docs?
Another approach that I tried was to copy the fields from the original resource into the duplicate and strip the ids of the resource and the embeds, but that results in a similar issue where you cannot delete the embeds in the form.
Thanks in advance