Skip to content

Commit

Permalink
Backup
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanIvanoff committed Dec 2, 2024
1 parent fee5319 commit 7ca07b7
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 17 deletions.
3 changes: 2 additions & 1 deletion lib/sanbase_web/components/core_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ defmodule SanbaseWeb.CoreComponents do
attr(:id, :string, required: true)
attr(:show, :boolean, default: false)
attr(:on_cancel, JS, default: %JS{})
attr(:max_modal_width, :string, default: "max-w-3xl")
slot(:inner_block, required: true)

def modal(assigns) do
Expand All @@ -60,7 +61,7 @@ defmodule SanbaseWeb.CoreComponents do
tabindex="0"
>
<div class="flex min-h-full items-center justify-center">
<div class="w-full max-w-3xl p-4 sm:p-6 lg:py-8">
<div class={["w-full p-4 sm:p-6 lg:py-8", @max_modal_width]}>
<.focus_wrap
id={"#{@id}-container"}
phx-window-keydown={JS.exec("data-cancel", to: "##{@id}")}
Expand Down
92 changes: 76 additions & 16 deletions lib/sanbase_web/live/metric_registry/metric_registry_index_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule SanbaseWeb.MetricRegistryIndexLive do
show_verified_changes_modal: false,
visible_metrics: metrics,
metrics: metrics,
metrics_verified_updates: %{},
verified_metrics_updates_map: %{},
filter: %{}
)}
end
Expand All @@ -26,9 +26,13 @@ defmodule SanbaseWeb.MetricRegistryIndexLive do
:if={@show_verified_changes_modal}
show
id="verified_changes_modal"
on_cancel={JS.push("update_show_verified_changes_modal", value: %{"bool" => false})}
max_modal_width="max-w-6xl"
on_cancel={JS.push("hide_show_verified_changes_modal")}
>
<.list_metrics_verified_status_changed metrics={@metrics} />
<.list_metrics_verified_status_changed
verified_metrics_updates_map={@verified_metrics_updates_map}
metrics={@metrics}
/>
</.modal>
<div class="flex flex-col items-start justify-evenly">
<div class="text-gray-400 text-sm py-2">
Expand Down Expand Up @@ -97,16 +101,16 @@ defmodule SanbaseWeb.MetricRegistryIndexLive do
)}
end

def handle_event("apply_verified_changes", _params, socket) do
def handle_event("show_verified_changes_modal", _params, socket) do
{:noreply,
socket
|> assign(show_verified_changes_modal: true)}
end

def handle_event("update_show_verified_changes_modal", %{"bool" => value}, socket) do
def handle_event("hide_show_verified_changes_modal", _params, socket) do
{:noreply,
socket
|> assign(show_verified_changes_modal: value)}
|> assign(show_verified_changes_modal: false)}
end

def handle_event(
Expand All @@ -116,7 +120,7 @@ defmodule SanbaseWeb.MetricRegistryIndexLive do
) do
new_map =
Map.update(
socket.assigns.metrics_verified_updates,
socket.assigns.verified_metrics_updates_map,
id,
# This will be invoked only the first time the metric registry is updated
%{old: Enum.find(socket.assigns.metrics, &(&1.id == id)).is_verified, new: bool},
Expand All @@ -125,14 +129,53 @@ defmodule SanbaseWeb.MetricRegistryIndexLive do

{:noreply,
assign(socket,
verified_metrics_update: new_map,
verified_metrics_updates_map: new_map,
metrics: update_metric(socket.assigns.metrics, id, :is_verified, bool),
visible_metrics: update_metric(socket.assigns.visible_metrics, id, :is_verified, bool)
)}
end

defp list_metrics_verified_status_changed(assigns) do
changed_metrics_ids =
assigns.verified_metrics_updates_map
|> Enum.reduce([], fn {id, map}, acc ->
if map.new != map.old, do: [id | acc], else: acc
end)

changed_metrics = Enum.filter(assigns.metrics, &(&1.id in changed_metrics_ids))
assigns = assign(assigns, changed_metrics: changed_metrics)

~H"""
<div :if={@changed_metrics == []}>
No changes
</div>
<div :if={@changed_metrics != []}>
<.table id="uploaded_images" rows={@changed_metrics}>
<:col :let={row} label="Metric">
<.metric_names
metric={row.metric}
internal_metric={row.internal_metric}
human_readable_name={row.human_readable_name}
/>
</:col>
<:col :let={row} label="New Status">
<span :if={row.is_verified} class="ms-3 text-sm font-bold text-green-900">VERIFIED</span>
<span :if={!row.is_verified} class="ms-3 text-sm font-bold text-red-700">UNVERIFIED</span>
</:col>
</.table>
<div class="mt-4">
<.phx_click_button
phx_click="confirm_verified_changes_update"
class="bg-green-500 hover:bg-green-900 text-white"
text="Confirm Changes"
/>
<.phx_click_button
phx_click="close_verified_changes_update"
class="bg-white hover:bg-gray-100 text-gray-800"
text="Close"
/>
</div>
</div>
"""
end

Expand All @@ -152,7 +195,7 @@ defmodule SanbaseWeb.MetricRegistryIndexLive do
<div class="relative w-11 h-6 bg-red-500 rounded-full peer peer-checked:after:translate-x-full rtl:peer-checked:after:-translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-0.5 after:start-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-green-600">
</div>
<span :if={@row.is_verified} class="ms-3 text-sm font-bold text-green-900">VERIFIED</span>
<span :if={!@row.is_verified} class="ms-3 text-sm font-bold text-red-900">UNVERIFIED</span>
<span :if={!@row.is_verified} class="ms-3 text-sm font-bold text-red-700">UNVERIFIED</span>
</label>
"""
end
Expand Down Expand Up @@ -200,12 +243,11 @@ defmodule SanbaseWeb.MetricRegistryIndexLive do
<.filter_not_verified />
</div>
</form>
<.button
phx-click="apply_verified_changes"
class="text-gray-900 bg-white hover:bg-gray-100 border border-gray-200 font-medium rounded-lg text-sm px-5 py-2.5 text-center inline-flex items-center gap-x-2"
>
Apply Verified Status Changes
</.button>
<.phx_click_button
phx_click="show_verified_changes_modal"
class="text-gray-900 bg-white hover:bg-gray-100"
text="Apply Verified Status Changes"
/>
</div>
"""
end
Expand Down Expand Up @@ -256,6 +298,25 @@ defmodule SanbaseWeb.MetricRegistryIndexLive do
"""
end

attr :phx_click, :string, required: true
attr :text, :string, required: true
attr :class, :string, required: true

defp phx_click_button(assigns) do
~H"""
<button
type="button"
phx-click={@phx_click}
class={[
"border border-gray-200 font-medium rounded-lg text-sm px-5 py-2.5 text-center inline-flex items-center gap-x-2",
@class
]}
>
<%= @text %>
</button>
"""
end

defp metric_names(assigns) do
~H"""
<div class="flex flex-col">
Expand All @@ -270,7 +331,6 @@ defmodule SanbaseWeb.MetricRegistryIndexLive do
Enum.map(metrics, fn metric ->
if metric.id == id do
%{metric | key => value}
|> dbg()
else
metric
end
Expand Down

0 comments on commit 7ca07b7

Please sign in to comment.