johmue

johmue

LiveView write to the session while being mounted

Two questions in short version

  1. The :router option of live_render/3 has disappeared from the docs in 0.17.6. In 0.17.10 it still seems to works in the unit tests. In the browser I still get a :not_mounted_at_router issue. How can I pass params to mount/3 when using live_render/3?

  2. How can I pass params to mount/3 from live/2 from LiveViewTest?

Long version

I want to handle requests like /item/<id>?foo=bar in a way that %{"foo" => "bar"} is put to the session like put_session(conn, "foo", "bar") and id is to be passed to a LiveView’s mount/3 callback in the params argument.

That works following the hint from this article and using the :router option of live_render/3.

So I route the GET /item/<id> requests to a function

def show(conn, %{"foo" => bar} = _params) do
  conn
  |> put_session(conn, "foo", bar)
  |> live_render(conn, MyLiveView, router: MyAppWeb.Router)
end

This works with phoenix_live_view 0.17.10 even though the :router option of live_render/3 has disappeared from the docs. So the question if it will work in the future and if not how to do it then.

Second question is how to call this LiveView from a test. If the LiveView is not mounted in the Router like live "/item/:id/", MyLiveView the call live(conn, "/item/1234") does not pass the id to MyLiveView’s mount/3.

What are my options? Maybe a completely different approach?

Marked As Solved

johmue

johmue

As ever so often. A few minutes after posting the question I found the natural solution for my use case. So just for reference to future readers, this is how I finally solved it:

  • The endpoint /item/<id> is routed directly into the LiveView, so detour through the show function with live_render.

  • The foo parameter is caught by a Plug that sits at the end of the browser pipeline. The code of the Plug module looks like this:

defmodule MyAppWeb.Plugs.Foo do
  use MyAppWeb, :controller
  import Plug.Conn

  def init(default), do: default

  def call(%Plug.Conn{params: %{"foo" => foo}} = conn, _default) do
    put_session(conn, "foo", foo)
    |> assign(:foo, foo)
  end

  def call(conn, default) do
    foo = get_session(conn, "foo") || default
    put_session(conn, "foo", foo)
    |> assign(:foo, foo)
  end
end

So if there is a ?foo=bar in the request, it is put to the session and assigned to the assigns of the conn. If not the foo parameter is either fetched from the session or set to default.

That is doing all I need.

Also Liked

andreaseriksson

andreaseriksson

I have another approach. For me, there is a scenarion when people leave the site and go to Stripe. So, I want to store the return to - path in a session so I can redirect them correct.

I basically use a JS-hook for this.

In the template;

<span id={@id} phx-hook="StoreReturnTo" data-return-to={@encoded_return_to}></span>

Note that I encode the param so its not visible for the user:

Phoenix.Token.sign(LiveSaasKitWeb.Endpoint, "return_to", return_to)

ANd then a hook:

export const StoreReturnTo = {
  mounted() {
    const elm = this.el
    const path = elm.dataset.returnTo

    if (path) fetch(`/store_return_to?p=${path}`)
  },
}

And that posts (actually GET) to a normal controller that decodes the path and add it to the session.

But, to be fair, I havent looked into if there is a better way to do this

Where Next?

Popular in Questions Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31586 112
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 40165 209
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement