tfwright / live_admin

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

Issue with search_select #51

Closed Rio517 closed 1 year ago

Rio517 commented 1 year ago

I'm running into an issue with search_select:

** (KeyError) key :repo not found in: %{__changed__: %{flash: true, options: true}, flash: %{}, myself: %Phoenix.LiveComponent.CID{cid: 4}, options: []}
    (live_admin 0.9.1) lib/live_admin/components/resource/form/search_select.ex:23: LiveAdmin.Components.Container.Form.SearchSelect.update/2
    (phoenix_live_view 0.19.3) lib/phoenix_live_view/utils.ex:487: Phoenix.LiveView.Utils.maybe_call_update!/3
    (phoenix_live_view 0.19.3) lib/phoenix_live_view/diff.ex:653: anonymous fn/5 in Phoenix.LiveView.Diff.render_pending_components/6
    (elixir 1.14.4) lib/enum.ex:2468: Enum."-reduce/3-lists^foldl/2-0-"/3
    (stdlib 4.3) maps.erl:411: :maps.fold_1/3
    (phoenix_live_view 0.19.3) lib/phoenix_live_view/diff.ex:629: Phoenix.LiveView.Diff.render_pending_components/6
    (phoenix_live_view 0.19.3) lib/phoenix_live_view/diff.ex:143: Phoenix.LiveView.Diff.render/3
    (phoenix_live_view 0.19.3) lib/phoenix_live_view/channel.ex:833: Phoenix.LiveView.Channel.render_diff/3
    (phoenix_live_view 0.19.3) lib/phoenix_live_view/channel.ex:469: Phoenix.LiveView.Channel.mount_handle_params_result/3
    (phoenix_live_view 0.19.3) lib/phoenix_live_view/channel.ex:1043: Phoenix.LiveView.Channel.verified_mount/8
    (phoenix_live_view 0.19.3) lib/phoenix_live_view/channel.ex:59: Phoenix.LiveView.Channel.handle_info/2
    (stdlib 4.3) gen_server.erl:1123: :gen_server.try_dispatch/4
    (stdlib 4.3) gen_server.erl:1200: :gen_server.handle_msg/6
    (stdlib 4.3) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
Last message: {Phoenix.Channel, %{"params" => %{"_csrf_token" => "WjIXLQEQDiQZKlMCB3IACTUsTCoSL2UjcxS_tiTmLRdQhEUlfs-YwG4t", "_moun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.g2gDaAJhBXQAAAADZAAKYXNzaWduX25ld2pkAAVmbGFzaHQAAAAAZAACaWRtAAAAFHBoeC1GMjc1NENONzlJVUNDUUJIbgYArQ4eJokBYgABUYA.I6Y0m10SBSuBBTYUVET-Upd22gP-Yf37HsXH5rZbeog", "url" => "http://local.wunderwork.io:4000/admin/min/jobs/edit/1"}, {#PID<0.1117.0>, #Reference<0.1630307316.1626603528.84781>}, %Phoenix.Socket{assigns: %{}, channel: Phoenix.LiveView.Channel, channel_pid: nil, endpoint: MyAppWeb.Endpoint, handler: Phoenix.LiveView.Socket, id: "users_sessions:LDi9TaYhpRXnHJcWZlwusi6DkVpc-_wKR7TAeCjsAwg=", joined: false, join_ref: "4", private: %{connect_info: %{session: %{"_csrf_token" => "9JDruyZIUx7So7UeS_asehQW", "live_socket_id" => "users_sessions:LDi9TaYhpRXnHJcWZlwusi6DkVpc-_wKR7TAeCjsAwg=", "locale" => "en", "user_token" => <<44, 56, 189, 77, 166, 33, 165, 21, 231, 28, 151, 22, 102, 92, 46, 178, 46, 131, 145, 90, 92, 251, 252, 10, 71, 180, 192, 120, 40, 236, 3, ...>>}, user_agent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"}}, pubsub_server: MyApp.PubSub, ref: nil, serializer: Phoenix.Socket.V2.JSONSerializer, topic: "lv:phx-F2754CN79IUCCQBH", transport: :websocket, transport_pid: #PID<0.1117.0>}}
State: #Reference<0.1630307316.1626603522.102768>
[error] an exception was raised:
    ** (KeyError) key :repo not found in: %{__changed__: %{flash: true, options: true}, flash: %{}, myself: %Phoenix.LiveComponent.CID{cid: 4}, options: []}
        (live_admin 0.9.1) lib/live_admin/components/resource/form/search_select.ex:23: LiveAdmin.Components.Container.Form.SearchSelect.update/2
        (phoenix_live_view 0.19.3) lib/phoenix_live_view/utils.ex:487: Phoenix.LiveView.Utils.maybe_call_update!/3
        (phoenix_live_view 0.19.3) lib/phoenix_live_view/diff.ex:653: anonymous fn/5 in Phoenix.LiveView.Diff.render_pending_components/6
        (elixir 1.14.4) lib/enum.ex:2468: Enum."-reduce/3-lists^foldl/2-0-"/3
        (stdlib 4.3) maps.erl:411: :maps.fold_1/3
        (phoenix_live_view 0.19.3) lib/phoenix_live_view/diff.ex:629: Phoenix.LiveView.Diff.render_pending_components/6
        (phoenix_live_view 0.19.3) lib/phoenix_live_view/diff.ex:143: Phoenix.LiveView.Diff.render/3
        (phoenix_live_view 0.19.3) lib/phoenix_live_view/channel.ex:833: Phoenix.LiveView.Channel.render_diff/3
        (phoenix_live_view 0.19.3) lib/phoenix_live_view/channel.ex:469: Phoenix.LiveView.Channel.mount_handle_params_result/3
        (phoenix_live_view 0.19.3) lib/phoenix_live_view/channel.ex:1043: Phoenix.LiveView.Channel.verified_mount/8
        (phoenix_live_view 0.19.3) lib/phoenix_live_view/channel.ex:59: Phoenix.LiveView.Channel.handle_info/2
        (stdlib 4.3) gen_server.erl:1123: :gen_server.try_dispatch/4
        (stdlib 4.3) gen_server.erl:1200: :gen_server.handle_msg/6
        (stdlib 4.3) proc_lib.erl:240: :proc_lib.init_p_do_apply/3

It seems that the issue is here: https://github.com/tfwright/live_admin/blob/main/lib/live_admin/components/resource/form/search_select.ex#L21

It seems that the repo key is present in assigns.repo, not socket.assigns.repo. If I inspect the socket.assigns I see that the repo key is missing.

%{
  __changed__: %{flash: true, options: true},
  flash: %{},
  myself: %Phoenix.LiveComponent.CID{cid: 4},
  options: []
}

If I switch to assigns.repo on line 21, all works smoothly.

tfwright commented 1 year ago

https://github.com/tfwright/live_admin/commit/48c1f4d7bbf999ff2f2bbefccf0377deb936b18c