alexgleason

alexgleason

LiveView: how to create a loading state during form submission?

I’m creating a domain name search form. It could take up to 10 seconds after hitting “search” to get a result. I want to display a “loading” animation in the meantime, something like this:

<section class="textual">
  <div class="wrap">

    <form method="post" phx-submit="search">
      <input type="text" name="q" placeholder="mytribe.com" />
      <button class="submit">Search</button>
    </form>
    
    <%= if @loading do %>
      LOADING . . .
    <% end %>
    
    <pre>
      <%= inspect(@results, pretty: true) %>
    </pre>
  </div>
</section>

What I can’t seem to figure out is how to set the “loading” state while handling the event…

  @impl true
  def handle_event("search", %{"q" => q}, socket) do
    push_event(socket, "set_loading", true) # This does nothing, pretty sure I've misunderstood the purpose of this function

    with {:ok, %{body: %{"data" => data}}} <- Epik.check_domains([q]) do
      {:noreply, assign(socket, :results, data)}
    end
  end

Right when the “search” event is starting to be handled, I want my template to have the loading animation. Then ~10s later when I get the result, I want to display the result.

I really don’t understand how LiveView/GenServer works on a fundamental level yet, but I’m starting to think maybe this isn’t possible.

Marked As Solved

kokolegorille

kokolegorille

You need to decouple a little bit… first set loading status, then run the query.

def handle_event("search", %{"q" => q}, socket) do
  send(self, {:run_search, q})
  {:noreply, assign(socket, :set_loading, true)}
end

def handle_info({:run_search, q}, socket) do
  # do the actual work
  socket
  |> assign(:set_loading, false)
  |> assign(:result, ...)
  {:noreply, socket}
end

Also Liked

kokolegorille

kokolegorille

I would not do the business logic in the component, I would make it dumb, and pass search query to the parent.

I see the parent as the manager of the state. It’s just my way of doing it

Where Next?

Popular in Questions Top

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
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
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
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 Postg...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
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
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement