orsinium
I have 2 live components on the same page, one for pagination and another for search. Both write their state into URL query params which are later handled by the live controller.
def handle_event("pagination", %{"page" => _} = params, socket) do
path = ITJWeb.Router.Helpers.live_path(socket, ITJWeb.OffersLive, params)
socket = push_patch(socket, to: path)
{:noreply, socket}
end
def handle_event("offer_search", %{"search" => search}, socket) do
path = ITJWeb.Router.Helpers.live_path(socket, ITJWeb.OffersLive, search)
socket = push_patch(socket, to: path)
{:noreply, socket}
end
def mount(params, _session, socket) do
{:noreply, socket} = handle_params(params, nil, socket)
{:ok, socket}
end
def handle_params(params, _uri, socket) do
...
{:noreply, socket}
end
The issue is that each component resets all existing query params. In other words, when you change the page, the search gets reset. There is a live demo: itj.orsinium.dev/offers.
The issue is that live_path requires all query params to be explicitly passed as a third argument but the existing query params (if I understand correctly) are not passed into handle_event.
- Is there a way to get current URL query params inside
handle_event? - Is there a way to do
live_path+push_patchwith preserving the current URL query params? - Should I approach the whole problem differently? Is
handle_eventof a live component a good place to modify URL or should I delegate it to the controller somehow?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted”
Version...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #blog-post
- #ai
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
URLs are essentially global/shared state for all liveviews/components on a single page, so my suggestion is to consolidate modification of that state in a single place. A.k.a instead of your search or pagination component doing a push_patch let them trigger something on the parent liveview, which is aware of the current state of the URL and can “add” the changes on top.
cmo
What’s with calling
handle_paramsfrom withinmount?handle_paramsis called by the system. If you add someIO.inspectorLogger.debugcommands inmountandhandle_paramsyou’ll see when and in what order it all happens.orsinium
Thank you for your reply! By sending messages as described in LiveView as the source of truth? It seems like a good idea and matching what docs say. They also explicitly say I can do live patches there, though, which confused me.
orsinium
Thank you, good catch. Somehow I thought
handle_paramsis only executed when you update params, not when you load the page. Now I see my assumption was wrong. I’ll update the code.cmo
Or writing them as Function Components. Then you handle the event in the liveview. None of your components are managing their own state so could be Function Components.
orsinium
That also might be a good idea. My motivation was to encapsulate into components handling of events generated by the components and live to the controller only dealing with URL queries. It seems like a good separation but now I’m not sure if it’s worth it. There are just a few lines of code in components anyway, not a big deal. I’m not sure if it will solve the issue, though, since
LiveView.handle_eventaccepts all the same arguments asLiveComponent.handle_eventand so also can’t preserve query params on URL patches.orsinium
I still can’t wrap my head around how to update the existing URL query.
LiveView.handle_eventaccepts all the same params asLiveComponent.handle_eventLiveView.handle_info(which I can trigger withsendfromLiveComponent) also doesn’t know anything about the current URL query.The best I have in mind is to pass the query params in
assignsof all components but then, I suppose, it will re-render the whole page, and then what’s even the point of having LiveView. And also it’s messy and easy to get out of sync. I’m wondering why the current URL isn’t in the socket in the first place.UPD: what I mean is that I see many ways how to update the LiveView state for different kinds of events but I can’t see a clean way to keep the URL query in sync with these changes.
cmo
That’s certainly not the case. I have a LiveView that I set myriad URL query params on and they don’t get overridden. Though I keep the
paramsinassignsso maybe I’m telling a fibWhen you’re using the URL query params, you use
handle_paramsto handle assigning anything that is associated with them. You don’tassignorupdatethem elsewhere (i.e. inhandle_eventorhandle_info). If you want to change them, you usepush_patch.You might find Sophie’s article on the topic useful, though she doesn’t seem to use
push_patch.Sleepful
Just the friendly reminder to use
mix phx.routesto figure out theRoutes.function for your path; instead ofRoutes.live_path.Related:
cmo
You’d probably use verified routes once you hit phoenix 1.7.