Hi,
Maybe, let me start with a code snippets:
In the root.html
(non liveview) I have:
<%= live_render(@conn, AppWeb.NotificationsLive,
session: %{"current_user_id" => @current_user_id}
) %>
That AppWeb.NotificationsLive
renders like this:
def render(assigns) do
~H"""
<.modal :if={@show_notifications_list}>
<.live_component
module={AppWeb.NotificationsLiveList}
id="notifications_list"
notifications={@notifications}
current_user_id={@current_user_id}
/>
</.modal>
<a href="#" phx-click="show_notifications">
<i>
...
</i>
</a>
"""
end
and can handle event like this:
def handle_event("click_notification", %{"id" => id} , socket) do
Notifications.set_seen_notifications(socket.assigns.current_user_id)
Notifications.set_visited_notification(id)
...
{:noreply, socket}
end
That AppWeb.NotificationsLiveList
modal is just a list of notifications for user (each notification in a div
).
Problem:
If user clicks on given notification, then such action is noted and database is updated, that for given user, this notification will have e.g. different background color. But also when user clicks on the notification, then such click should also navigate her to different page - lets assume it is a LiveView with live
route in router.ex
and I want to use the existing socket for that push. How to do it? Wrapping div
with notification in a <.link>
does not work for me: the click_notification
event will not be delivered, but the page to which notification is navigating will be loaded, for example:
<.link replace patch={@n.link} phx-click="click_notification" phx-value-id={@n.id}>
<div>
...
</div>
</a>
I’ve also tried push_navigate
from the event handler (passing the link to the server via phx-value-link
) - it is not working as I would expect: it is slow and there is new socket created (but I guess this is caused by some error, but I do not see any).