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

wmnnd
Hi there, for my project DBLSQD, I needed a file storage solution that is a bit more flexible than Arc. Because I thought others might f...
New
handnot2
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application. This library uses Erlang esaml to provide plug enabl...
New
pkrawat1
Hey guyz We at @aviabird are working on a payment library in elixir/phoenix. We are targeting March 2018 to add 56 Gateways to it. Have...
New
anshuman23
Hello all, I have been working on my proposed project called Tensorflex as part of Google Summer of Code 2018.. Tensorflex can be used f...
New
tmbb
PhoenixWS - Websockets over Phoenix Channels Source code on Github here: GitHub - tmbb/phoenix_ws: Websockets implemented over Phoenix Ch...
New
wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New

Other popular topics Top

hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New

We're in Beta

About us Mission Statement