mindwendel icon indicating copy to clipboard operation
mindwendel copied to clipboard

question: proper usage of .inputs_for

Open JannikStreek opened this issue 1 year ago • 4 comments

Further Notes

Please see the documentation at: https://hexdocs.pm/phoenix_live_view/Phoenix.Component.html#inputs_for/1

In the current code in the admin/edit live_view there is a lot of custom code how to delete the labels. I am not sure we are using the best practice here.

See from the docs this example

schema "mailing_lists" do
        field :title, :string
        embeds_many :emails, EmailNotification, on_replace: :delete do
          field :email, :string
          field :name, :string
        end
      end

JannikStreek avatar Sep 25 '24 13:09 JannikStreek

inputs_for is just as grand of a fun as I remember from Rails, the entire phoenix liveview book doesn't make a single mention of it :sweat_smile:

PragTob avatar Nov 03 '24 14:11 PragTob

Alrighty, took some notes around the code and I think it can be done and simplify things - but I'm too tired to do it today :sweat_smile:

Most importantly:

  • they talk about sort_param and drop_param - I believe we'll only need to handling for drop_param. We only ever add to the end (iirc) and order doesn't particularly super matter.
  • We need to set on_replace: :delete which is its own can of worms
  • thorough testing, so that nothing gets deleted by accident

that all said, the last times I did this (long time ago in both Phoenix and Rails) it was a constant source of bugs and frustrations. There, I often just ended up handling the data myself at it was easier.

That said, our example is fairly small/simple and relatively close to the one displayed so it may just work and remove some duplication.

PragTob avatar Nov 03 '24 14:11 PragTob

I have some stuff up on inputs-for-thinking - gotta fix some test failures but it seems to be working in general. Also, some details to iron out.

PragTob avatar Nov 08 '24 18:11 PragTob

Code for this is on #466 but ran into a limitation of ecto with keeping constraints around that is less trivial to fix than I assumed. I'm still working a bit on that in my personal time but need to get back to the ecto team.

PragTob avatar Nov 24 '24 09:11 PragTob