# `MishkaGervaz.Table.Web.State.UrlSync`
[🔗](https://github.com/mishka-group/mishka_gervaz/blob/v0.0.5/lib/mishka_gervaz/table/web/state/url_sync.ex#L1)

Handles URL synchronization for table state.

## Overridable Functions

- `apply_url_state/2` - Apply URL state to table state
- `bidirectional?/1` - Check if bidirectional sync is enabled

## The template from the URL

With `:template` in the table's `url_sync` params, the URL names the template the table shows,
and the table opens in it when it is one of its `switchable_templates` (`apply_url_template/2`).
A name it does not offer leaves the table in its own template.

## The page from the URL

The table opens the URL's page when the database can read it: its offset, `(page - 1)` times
the page size the table reads with, fits a signed 64-bit integer, the range Postgres takes for
`OFFSET`. A table of 20 rows a page opens any page up to 461,168,601,842,738,791. A page past that
opens page 1, as a URL without a page does. A page past the last row is opened, and shows no rows.
The check is `MishkaGervaz.Table.Web.State.Helpers.readable_page/2`, which a `go_to_page` event
passes through too.

## The sort from the URL

The table sorts by the URL's fields that it sorts by itself: a sortable column's `sort_field` (or
its name) or a field of its default sort, through
`MishkaGervaz.Table.Web.State.Helpers.readable_sort/2`. Any other field, one the resource does not
have or a column that is not sortable, is left out. A URL whose sort keeps no field opens the
table's own sort, as a URL without a sort does, and a `:bidirectional` table writes the URL
without it.

## User Override

    defmodule MyApp.Table.UrlSync do
      use MishkaGervaz.Table.Web.State.UrlSync

      def apply_url_state(state, url_state) do
        state
        |> super(url_state)
        |> apply_custom_url_params(url_state)
      end
    end

See `MishkaGervaz.Table.Web.State`,
`MishkaGervaz.Table.Web.State.Helpers`,
`MishkaGervaz.Table.Web.UrlSync` (the outer-LiveView URL bridge),
and the sibling builders `ColumnBuilder`, `FilterBuilder`,
`ActionBuilder`, `Presentation`, `Access`.

# `apply_url_template`

```elixir
@spec apply_url_template(map(), map()) :: map()
```

Opens the table in the template the URL names: one of its `switchable_templates`, by `name/0`
or by module, as the template switcher does. A name the table does not offer leaves the
template as it is.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
