tfwright / live_admin

Low-config admin UI for Phoenix apps, built on LiveView
MIT License
248 stars 22 forks source link

Can't assign belongs to relationship value via new or edit forms #84

Closed jakespracher closed 6 months ago

jakespracher commented 7 months ago

Describe the bug Thanks for the package. I have two schemas, user and conversation, where conversation belongs to user

defmodule Grayrock.Users.User do
  use Ecto.Schema
  use LiveAdmin.Resource
  import Ecto.Changeset
  @primary_key {:id, :binary_id, autogenerate: true}
  @foreign_key_type :binary_id
  @derive {
    Flop.Schema,
    default_limit: 20,
    filterable: [:search_phrase, :email, :confirmed_at, :inserted_at],
    sortable: [:email, :confirmed_at, :inserted_at],
    compound_fields: [search_phrase: [:email]]
  }
  schema "users" do
    field :email, :string
    ...
  end

(boilerplate from https://livesaaskit.com)

defmodule MyApp.Chatbot.Conversation do
  use Ecto.Schema
  use LiveAdmin.Resource
  import Ecto.Changeset

  @primary_key {:id, :binary_id, autogenerate: true}
  @foreign_key_type :binary_id
  schema "chatbot_conversations" do
    ...
    belongs_to :user, Grayrock.Users.User

I am unable to assign a value to the user field via the conversation form (see video)

https://github.com/tfwright/live_admin/assets/7165897/c2dc3951-0ac3-4ec5-98f5-98e37d76e961

To Reproduce Steps to reproduce the behavior:

  1. Setup app / database with above schemas and panel following readme instructions
  2. Visit conversation form
  3. Go create or edit a conversation
  4. Attempt to set the user field
  5. See video

Expected behavior Something would show in these places:

Screenshot 2023-12-02 at 9 55 58 PM Screenshot 2023-12-02 at 9 56 06 PM

Environment:

Additional Context No errors shown in phoenix server logs or browser js console, everything else seems to work as expected

jakespracher commented 7 months ago

Actually, I lied there is one error in the server logs only when the admin liveview is first loaded


    ** (EXIT) an exception was raised:
        ** (KeyError) key "0a7000ad-4cca-4a81-b5f7-dc1f109e4325" not found in: %{}
            :erlang.map_get("0a7000ad-4cca-4a81-b5f7-dc1f109e4325", %{})
            (live_admin 0.11.2) lib/live_admin/session/agent.ex:23: anonymous fn/2 in LiveAdmin.Session.Agent.load!/1
            (elixir 1.15.7) lib/agent/server.ex:12: Agent.Server.handle_call/3
            (stdlib 5.0.2) gen_server.erl:1113: :gen_server.try_handle_call/4
            (stdlib 5.0.2) gen_server.erl:1142: :gen_server.handle_msg/6
            (stdlib 5.0.2) proc_lib.erl:241: :proc_lib.init_p_do_apply/3```
tfwright commented 7 months ago

Thanks for the report. What version of live_admin are you using?

jakespracher commented 7 months ago

0.11.2

tfwright commented 7 months ago

Sorry for the delay on this. I have verified the issue affecting Safari on Mac, will look into a fix soon.

tfwright commented 6 months ago

So unfortunately this seems to be a Safari quirk that, as least from what I can see, breaks not only the search select but also dropdowns throughout the app. I don't see any easy fix that doesn't involve reimplementing all of these components so I can't promise any resolution in the near term, given my extremely lackluster FE dev skills.

tfwright commented 6 months ago

This should be fixed in main.

It turned out the issue with the dropdowns was relatively simple: https://github.com/tfwright/live_admin/commit/0234a26eb4f487e0994b2eaa19798e7337225990

That still didn't fix the search select, and I wasn't able to figure out exactly why, but reasoning that it must be a slightly different markup used there vs other dropdowns which were now working correctly, I was able to fix it by refactoring the general dropdown component to support the search select UX: https://github.com/tfwright/live_admin/commit/341f8a820a4f50e9379894ace80add992af11cc8

Please let me know if you encounter further issues

jakespracher commented 3 months ago

Confirming this is resolved after update. Thanks!