From f1f0b779545367ab567e88f90dd36090c8f1bddd Mon Sep 17 00:00:00 2001 From: KKlochko Date: Fri, 25 Apr 2025 21:28:24 +0300 Subject: [PATCH] Add the sync button to Server Index LiveView. --- .../live/dbi_server_live/index.ex | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/lib/decentralised_book_index_web/live/dbi_server_live/index.ex b/lib/decentralised_book_index_web/live/dbi_server_live/index.ex index db5ef3e..df9c291 100644 --- a/lib/decentralised_book_index_web/live/dbi_server_live/index.ex +++ b/lib/decentralised_book_index_web/live/dbi_server_live/index.ex @@ -2,12 +2,21 @@ defmodule DecentralisedBookIndexWeb.DbiServerLive.Index do use DecentralisedBookIndexWeb, :live_view alias DecentralisedBookIndex.Metadata + alias DecentralisedBookIndex.SyncTasks.SyncServerTask @impl true def render(assigns) do ~H""" <.header> Listing Servers + + <:actions> +
+ <.primary_button phx-click="sync"> + Sync now + +
+
@@ -177,4 +186,32 @@ defmodule DecentralisedBookIndexWeb.DbiServerLive.Index do {:noreply, push_patch(socket, to: ~p"/servers?#{params}")} end + + @impl true + def handle_event("sync", _params, socket) do + Task.async(fn -> + SyncServerTask.sync_all() + send(self(), :sync_completed) + end) + + socket = + socket + |> put_flash(:info, "The sync is started") + + {:noreply, socket} + end + + @impl true + def handle_info({_pid, :sync_completed}, socket) do + socket = + socket + |> put_flash(:info, "The sync is done") + + {:noreply, socket} + end + + @impl true + def handle_info(_params, socket) do + {:noreply, socket} + end end