Skip to content

Commit 8ffa72d

Browse files
committed
Use async connection testing
1 parent 2664414 commit 8ffa72d

3 files changed

Lines changed: 57 additions & 19 deletions

File tree

lib/logflare_web/live/backends/actions/show.html.heex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
</div>
2929
<div>
3030
<.button variant="primary" phx-click="test_connection">Test connection</.button>
31-
<.status_indicator status={@connection_status} />
31+
<.status_indicator :if={@connection_status} status={@connection_status} />
3232
</div>
3333
</section>
3434

lib/logflare_web/live/backends/backends_live.ex

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,18 +141,27 @@ defmodule LogflareWeb.BackendsLive do
141141
end
142142

143143
def handle_event("test_connection", _params, socket) do
144-
backend = socket.assigns.backend
145-
146-
case Backends.test_connection(backend) do
147-
:ok ->
148-
{:noreply, assign(socket, connection_status: :ok)}
144+
status = socket.assigns.connection_status
149145

150-
{:error, reason} ->
146+
socket =
147+
if status && status.loading do
151148
socket
152-
|> assign(connection_status: :error)
153-
|> put_flash(:error, "Connection test failed:\n#{reason}")
154-
|> then(&{:noreply, &1})
155-
end
149+
else
150+
backend = socket.assigns.backend
151+
152+
assign_async(
153+
socket,
154+
:connection_status,
155+
fn ->
156+
with :ok <- Backends.test_connection(backend) do
157+
{:ok, %{connection_status: :ok}}
158+
end
159+
end,
160+
reset: true
161+
)
162+
end
163+
164+
{:noreply, socket}
156165
end
157166

158167
def handle_event("toggle_default_ingest_form", _params, socket) do

lib/logflare_web/live/backends/components.ex

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,50 @@
11
defmodule LogflareWeb.Backends.Components do
22
use Phoenix.Component
33

4+
# TODO: Extract common parts
5+
6+
# def text_input(assigns) do
7+
# ~H"""
8+
# <div class="form-group">
9+
# {label(@form, @id, @label)}
10+
# {text_input(@form, @id, class: "form-control")}
11+
# <small class="form-text text-muted">
12+
# {@description}
13+
# </small>
14+
# </div>
15+
# """
16+
# end
17+
18+
# def select_input(assigns) do
19+
# ~H"""
20+
# <div class="form-group">
21+
# {label(@form, @id, @label)}
22+
# {text_input(@form, @id, class: "form-control")}
23+
# <small class="form-text text-muted">
24+
# {@description}
25+
# </small>
26+
# </div>
27+
# """
28+
# end
29+
430
attr :status, :atom, values: [:ok, :error, :loading]
531

632
def status_indicator(assigns) do
7-
case assigns.status do
8-
:ok -> ~H|<.indicator icon="check" color="green-500" )} />|
9-
:error -> ~H|<.indicator icon="times" color="red-500" )} />|
10-
:loading -> ~H|<.indicator icon="spinner" color="white" animation="tw-animate-spin" )} />|
11-
nil -> ~H""
12-
end
33+
~H"""
34+
<.async_result :let={_ok} assign={@status}>
35+
<:loading><.indicator icon="spinner" color="tw-text-white" animation="tw-animate-spin" )} /></:loading>
36+
<:failed :let={{atom, reason}}>
37+
<% reason = if atom == :error, do: reason, else: "Internal error" %>
38+
<.indicator icon="times" color="tw-text-red-500" )} /> <span class="inline-block tw-mx-15">{reason}</span>
39+
</:failed>
40+
<.indicator icon="check" color="tw-text-green-500" )} />
41+
</.async_result>
42+
"""
1343
end
1444

1545
defp indicator(assigns) do
1646
animation = if assigns[:animation], do: assigns.animation, else: ""
17-
color = if assigns[:color], do: "tw-text-#{assigns.color}", else: ""
18-
assigns = assign(assigns, color: color, animation: animation)
47+
assigns = assign(assigns, animation: animation)
1948

2049
~H"""
2150
<i class={"inline-block fas fa-#{@icon} #{@color} tw-align-middle tw-text-2xl #{@animation}"}></i>

0 commit comments

Comments
 (0)