Skip to content

Commit

Permalink
Fix task when run on non list pages
Browse files Browse the repository at this point in the history
  • Loading branch information
tfwright committed Nov 2, 2024
1 parent f168741 commit c13304b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 29 deletions.
34 changes: 34 additions & 0 deletions dist/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,40 @@ select {
--tw-backdrop-sepia: ;
}

.container {
width: 100%;
}

@media (min-width: 640px) {
.container {
max-width: 640px;
}
}

@media (min-width: 768px) {
.container {
max-width: 768px;
}
}

@media (min-width: 1024px) {
.container {
max-width: 1024px;
}
}

@media (min-width: 1280px) {
.container {
max-width: 1280px;
}
}

@media (min-width: 1536px) {
.container {
max-width: 1536px;
}
}

.view__container {
overflow-y: auto;
}
Expand Down
34 changes: 31 additions & 3 deletions lib/live_admin/components/container.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ defmodule LiveAdmin.Components.Container do
resource_title: 2,
route_with_params: 1,
route_with_params: 2,
trans: 1
trans: 1,
trans: 2
]

import LiveAdmin.Components
Expand Down Expand Up @@ -89,6 +90,33 @@ defmodule LiveAdmin.Components.Container do

def handle_params(_, _, socket), do: {:noreply, socket}

@impl true
def handle_event(
"task",
params = %{"name" => task},
socket = %{
assigns: %{session: session, resource: resource, config: config}
}
) do
{_, m, f, _, _} =
LiveAdmin.fetch_function(resource, session, :tasks, String.to_existing_atom(task))

args = [session | Map.get(params, "args", [])]

search = Map.get(socket.assigns, :search)

Task.Supervisor.async_nolink(LiveAdmin.Task.Supervisor, m, f, [
Resource.query(resource, search, config) | args
])

socket =
socket
|> put_flash(:info, trans("%{task} started", inter: [task: task]))
|> push_navigate(to: route_with_params(socket.assigns))

{:noreply, socket}
end

@impl true
def handle_event("set_locale", %{"locale" => locale}, socket) do
new_session = Map.put(socket.assigns.session, :locale, locale)
Expand Down Expand Up @@ -385,7 +413,7 @@ defmodule LiveAdmin.Components.Container do
to: "##{@task}-task-modal",
transition: {"ease-in duration-300", "opacity-0", "opacity-100"}
),
else: JS.push("task", value: %{"name" => @task}, page_loading: true, target: "#list")
else: JS.push("task", value: %{"name" => @task}, page_loading: true)
}
,
data-confirm={if @modalize, do: nil, else: "Are you sure?"}
Expand All @@ -398,7 +426,7 @@ defmodule LiveAdmin.Components.Container do
<%= for {_lang, doc} <- @function_docs do %>
<span class="docs"><%= doc %></span>
<% end %>
<.form for={Phoenix.Component.to_form(%{})} phx-submit="task" phx-target="#list">
<.form for={Phoenix.Component.to_form(%{})} phx-submit="task">
<input type="hidden" name="name" value={@task} />
<%= if @extra_arg_count > 0 do %>
<b>Arguments</b>
Expand Down
26 changes: 0 additions & 26 deletions lib/live_admin/components/resource/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ defmodule LiveAdmin.Components.Container.Index do

import LiveAdmin,
only: [
route_with_params: 1,
route_with_params: 2,
trans: 1,
trans: 2
Expand Down Expand Up @@ -341,31 +340,6 @@ defmodule LiveAdmin.Components.Container.Index do
"""
end

@impl true
def handle_event(
"task",
params = %{"name" => task},
socket = %{
assigns: %{search: search, session: session, resource: resource, config: config}
}
) do
{_, m, f, _, _} =
LiveAdmin.fetch_function(resource, session, :tasks, String.to_existing_atom(task))

args = [session | Map.get(params, "args", [])]

Task.Supervisor.async_nolink(LiveAdmin.Task.Supervisor, m, f, [
Resource.query(resource, search, config) | args
])

socket =
socket
|> put_flash(:info, trans("%{task} started", inter: [task: task]))
|> push_navigate(to: route_with_params(socket.assigns))

{:noreply, socket}
end

@impl true
def handle_event("search", %{"query" => query}, socket = %{assigns: assigns}) do
{:noreply,
Expand Down

0 comments on commit c13304b

Please sign in to comment.