GrammAcc

GrammAcc

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? Top

Trending in Announcing Top

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
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
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 11030 135
New
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
fuelen
Hi all! I want to present a small library which provides a mix task for generating an Entity-Relationship Diagram for Ecto schemas. You...
New

Other Trending Topics Top

mudasobwa
I am seeing a lot of aplications of Argumentum ad Vericundiam in software discussions. They do link some piece of writing and point us to...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
lawik
I was thinking since Goatmire Elixir turned out pretty good I should maybe do another one. 30th of Sep - 2nd of Oct this year./ The firs...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews