zamith

zamith

I’m creating a search form with filters and want both the query and the filters to change the URL. The filters and query can be set separately or together, and handle_params can already deal with either being empty.

What I’m doing is triggering a push_patch on events (filter click, form submit, clear search). Something like this:

  def handle_event("update_filter", %{"filter" => filter}, socket) do
    {:noreply,
     push_patch(socket,
       to: Routes.live_path(socket, Index, query: socket.assigns.query, filter: filter)
     )}
  end

  def handle_event("search", %{"query" => query}, socket) do
    {:noreply,
     push_patch(socket,
       to: Routes.live_path(socket, Index, query: query, filter: socket.assigns.filter)
     )}
  end

This works, but my “problem” is that I have to set all the url params every time I call push_patch. Is there any way to say append this param to the current path? Alternatively, is there any other design pattern I should be following to set this up?

First Post!

cnck1387

cnck1387

I set up events to do exactly what you’re doing but with a different problem, but I wonder, does it make more sense to use live_patch links from your template with the query params in tact and then use handle_params at the LV level to grab those params?

This means the event handler would go away and having to push_patch from the LV itself since live_patch already pushes it to the URL.

Just asking because the above applies to your example too. I wonder what folks think about this. What would be the way to do this?

What’s interesting is the --live generator from Phoenix 1.5 includes making a search form using the event style but in their case, they are doing an external redirect so maybe the use case is different.

Most Liked

josevalim

josevalim

Creator of Elixir

Here is how I solved it in my app:

  1. I store the params in handle_params as part of the assigns:

     def handle_params(params, url, socket) do
       # You could store the params as is but I like to store
       # only what has been parsed/validated.
       params = parse_params(params)
       {:noreply, assign(socket, :params, params)
     end
    
  2. Create a helper called self_path:

     def self_path(socket, action, extra) do
       Routes.live_path(socket, __MODULE__, action, Enum.into(extra, socket.assign.params))
     end
    
  3. Now I do:

     push_path(socket, to: self_path(socket, :search, %{"sort_by" => "foo"}))
    
19
Post #7
sb8244

sb8244

Author of Real-Time Phoenix

FWIW, you can also directly use the URI rather than calling the Routes helpers. The above code does this as well, but I wanted to provide a more concise version if you don’t need as many bells and whistles.

  def handle_params(_params, uri, socket) do
    {:noreply, assign(socket, :uri, URI.parse(uri))}
  end

  def handle_event("prev_page", _, socket = %{assigns: %{pager: %{page: page}, uri: uri}}) do
    current_params = URI.decode_query(uri.query || "")
    new_params = Map.put(current_params, "page", page - 1)
    to = uri.path <> "?" <> URI.encode_query(new_params)

    {:noreply, push_patch(socket, to: to)}
  end
seva

seva

Worth adding that instead of URI.decode_query/1 and URI.encode_query/1, you might want to use more robust Plug.Conn.Query.decode/1 and Plug.Conn.Query.encode/1, which do better encoding/decoding of lists in URL (e.g. ?ids[]=1&ids[]=2).

I.e.

URI.decode_query("ids[]=1&ids[]=2")
# => %{"ids[]" => "2"}

Plug.Conn.Query.decode("ids[]=1&ids[]=2")
# => %{"ids" => ["1", "2"]}

Last Post!

Sleepful

Sleepful

oh and I hit this issue while updating my deps so watchout if you are in the same boat: Liveview push_patch not working starting from version 3.3.0 · Issue #417 · phoenixframework/phoenix_html · GitHub

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
kszambelanczyk
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
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
aseigo
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews