I have tried for days to get this to work and I’ve exhausted the documentation. Could someone help me with this?
ISSUE: handle_event is being called on parent LiveView instead of the Modal Component that should be receiving that callback.
LiveView is called ShowLesson. In the render for ShowLesson, I call a modal called ResourceformLive.ComponentRemoveFromLesson which removes a resource from the Lesson. That code looks like this:
<%= if @live_action in [:remove_resource] do %>
<%= live_modal @socket, MyApp.ResourceformLive.ComponentRemoveFromLesson,
id: @lesson_id,
page_title: @page_title,
action: @live_action,
lesson_id: @lesson_id,
resourceform: @resourceform,
return_to: Routes.lesson_show_lesson_path(@socket, :show_lesson, @lesson_id) %>
<% end %>
That code successfully opens the modal component’s render and populates the assigns.
The render for ComponentRemoveFromLesson is very simple. It did not require a changeset, so I just used an atom instead of a changeset. The user confirms the removal of the resource and presses a button. That button should call handle_event on the modal component. Instead, it keeps going to the LiveView.
def render(assigns) do
~L"""
<%= f = form_for :join, "#",
id: "remove-resource",
phx_target: @myself,
phx_submit: "remove" %>
<h2><%= @page_title %></h2>
<h3>Resource to be Removed</h3>
<span>Name: <%= link( @resourceform.title, to: @resourceform.resource_url) %></span>
<span>Description: <%= @resourceform.description %>
<div>
<span>Are you sure you want to remove this resource from your Lesson Plan?"</span>
<div>
<ul><button phx-click="remove">Remove Resource</button></ul>
</div>
</form>
"""
end
The modal_component handle_event looks like this:
def handle_event("remove", %{"join" => params}, socket) do
IO.puts "RESOURCEFORM_LIVE.component_remove_from_lesson.handle_event :remove"
…
end
But it never gets to that handle_event, because it keeps going to the ShowLesson Liveivew. I keep getting this error (which is driving me mad):
(UndefinedFunctionError) function MyApp.LessonLive.ShowLesson.handle_event/3 is undefined or private
I can’t figure out why it keeps slinging back to the LiveView for the handle_event callback instead of using the one in the component. I set a unique ID in the component call as you can see above. What am I missing?