the_dude

the_dude

Liveview - rerendering nonstop (keeps making api request)

Trying to load user data from the reddit api which is working. However, on mount it appears to be stuck in a rendering loop repeatedly making requests to the api. This causes it to error out and redirect to ‘/’. This is what I have below so far… running latest versions of phoenix and elixir 1.10.0. using Tesla for the api requests. i have also tried moving this logic to handle_params instead but does same thing. Any ideas why this is happening? I thought maybe it was making the call twice possibly since it connects to socket twice on initial mount but doesn’t seem to be it. Is it possible this might be due to the way I implemented my api requests?

Other weird thing is that temporary_assigns will not work either…i get an @saves not available in eex template error. I need this to work b/c plan is to append additional items to list w/ “load more” button using phx-update.

Any help appreciated. been digging at this all day and getting nowhere. Also, if there’s a better/cleaner way do this please let me know. a bit green.

def mount(params, _session, socket) do
      case Reddit.request_token(params["code"]) do
        "invalid_grant" ->
          {:ok,  
            socket
            |> put_flash(:error, "Token Invalid!")
            |> redirect(to: "/")
          }

        token ->
          {:ok, response} = Reddit.get_user_saves(token)
          {:ok, assign(socket, :saves, response.body)}
      end
end

First Post!

jhefreyzz

jhefreyzz

Hi, have you tried using connected? function since liveview is mounted twice.

def mount(params, _session, socket) do
  if connected?(socket) do
    case Reddit.request_token(params["code"]) do
     "invalid_grant" ->
       {:ok,  
         socket
         |> put_flash(:error, "Token Invalid!")
         |> redirect(to: "/")
       }

      token ->
       {:ok, response} = Reddit.get_user_saves(token)
       {:ok, assign(socket, :saves, response.body)}
    end
  end
end

Last Post!

the_dude

the_dude

Wanted to give an update on this:

I moved the case for the token validation into the api wrapper so there was only one call inside mount/3. Still caused the same problems. So I ended up just putting the api request in a regular controller then, called live_render/3 and passed my data through to the liveview w/ session%{}.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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 42716 114
New

We're in Beta

About us Mission Statement