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

Where Next?

Popular in Questions Top

New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement