GrammAcc

GrammAcc

Cartograph - E2E Query Param Patching for LiveView

I created this library to handle relative query patching for Phoenix live patch and navigate events.

This is based on the discussion in Add `:params` opt to JS.patch and JS.navigate, and opt to merge `phx-value-*` and @steffend ‘s feedback on this LiveView PR: WIP: Relative Query Patching by GrammAcc · Pull Request #4067 · phoenixframework/phoenix_live_view · GitHub .

Unlike that PR, this library handles all of the param parsing server-side instead of in the client-side JS.

The library provides Cartograph.LiveViewParams which can be used by a LiveView module to add the event handlers for the library’s relative query patching events.

The main api is in the Cartograph.Component module, which provides a cartograph_patch/1 and cartograph_navigate/2 function. Both functions accept a :query keyword list of operations to apply to the current page’s query params.

For example: cartograph_patch(query: [merge: %{page_no: 2}]) will send a live patch event to the server with the current url and params but with the page_no query param set to 2.

There are several other operations that can be composed together for pretty much any kind of relative query alteration.

This makes it easier to maintain query params in the URL with pure function components that might otherwise clobber each other’s state. For example, a reusable pagination widget and a reusable filters widget.

Another use-case that was discussed in the linked thread and PR is having the complete URL pre-computed on the server so that the user can right-click and copy the link in the browser. This use-case is supported by the parse_patch/2 and parse_navigate/2 functions in the Cartograph.Component module.

These functions take a url and the same :query keyword options and parse them together into a string suitable to be used in LiveView’s patch or navigate respectively. This enables the use-case described above with .link components:

<.link patch={parse_patch(@cartograph_uri, query: [merge: %{page_no: @page_no - 1}])}>Prev</.link>

The @cartograph_uri assign is added by the library to maintain the current URI from the latest call to handle_params/3, so it is always accessible in the LiveView for relative navigation.

The Cartograph.LiveViewParams module also provides some conveniences for reducing boilerplate when parsing query params in the LiveView.

We can declare a @cartograph_parser module attribute that registers a 3-arity function to the handle params lifecycle hook to take care of most of the logic in handle_params/3:

@cartograph_parser [
  handler: &__MODULE__.parse_params/3,
  keys: [:page_no, :page_size],
]

def parse_params(socket, %{"page_no" => page_no}, :page_no) do
  assign(socket, :page_no, page_no)
end

def parse_params(socket, %{}, :page_no), do: assign(socket, :page_no, 1)

def parse_params(socket, %{"page_size" => page_size}, :page_size) do
  assign(socket, :page_size, page_size)
end

def parse_params(socket, %{}, :page_size), do: assign(socket, :page_size, 25)

This is of course entirely optional, and the library doesn’t monkey patch anything or name-clash with any LiveView apis, so it should be easy to try out the library and remove it if you don’t like it. :slight_smile:

The documentation has several examples as well and the api docs are complete.

https://github.com/GrammAcc/cartograph

Where Next?

Popular in Announcing Top

gabrielpoca
Hello everyone! I want to share with you something that I’m really proud of: https://stillstatic.io/ Still is a static site builder for...
New
Crowdhailer
I have been updating a library that allows you to pipe between functions that use the erlang result tuple convention. Assuming you have ...
New
maltoe
Hello! Came here to announce ChromicPDF, a pet project PDF generator I’ve been working on for the past few months. Why another PDF gener...
New
alisinabh
Hey everyone i’ve developed a library for Jalaali calendar for elixir which supports converting Gregorian dates to Jalaali and vice vers...
New
Eiji
ExApi is a library that I’m developing now and hope release soon This library will allow to: list all apis list all api implementation...
New
woutdp
Hi! I wanted to introduce my latest project LiveSvelte. It allows you to render Svelte inside LiveView with end-to-end reactivity. It’s ...
New
aditya7iyengar
Rummage.Ecto and Rummage.Phoenix provide ways to perform Searching, Sorting and Pagination over Ecto queries and Phoenix collections. Fo...
New
Jskalc
Hi! Today, after a couple weeks of development I’ve released v0.1 of LiveVue. It’s a seamless integration of Vue and Phoenix LiveView, i...
New
marcuslankenau
I feel kind of stuck with the absence of a proper xml library for Elixir. Currently I use SweetXML which was ok for me more or less to pa...
New
zoltanszogyenyi
Hey everyone :waving_hand: Excited to join this forum - I am one of the founders and current project maintainers of a popular and open-s...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New

We're in Beta

About us Mission Statement