Skip to content

Commit

Permalink
Hide the other options and leave default as Run now
Browse files Browse the repository at this point in the history
  • Loading branch information
jyeshe committed Feb 3, 2025
1 parent 7453bf6 commit 882914f
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 137 deletions.
101 changes: 38 additions & 63 deletions lib/lightning_web/live/job_live/cron_run_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ defmodule LightningWeb.JobLive.CronRunButton do
"""
use LightningWeb, :live_component

import Ecto.Query

alias Lightning.Repo
alias Lightning.Run
alias Lightning.WorkOrder

require Run

Expand All @@ -15,9 +19,8 @@ defmodule LightningWeb.JobLive.CronRunButton do
<div>
<.button
id={@id}
phx-hook="DefaultRunViaCtrlEnter"
type="button"
phx-click={@selected_option}
phx-hook="DefaultRunViaCtrlEnter"
class={cron_trigger_bt_classes(assigns)}
disabled={@disabled}
>
Expand All @@ -31,102 +34,74 @@ defmodule LightningWeb.JobLive.CronRunButton do
<% end %>
</.button>
</div>
<div class="relative -ml-px block">
<.dropdown_action {assigns} />
</div>
</div>
"""
end

@impl true
def update(
assigns,
%{follow_run: follow_run} = assigns,
socket
) do
follow_run = Map.get(assigns, :follow_run)
snapshot_version_tag = "latest"
disabled = processing(follow_run) or snapshot_version_tag != "latest"
selected_option = Map.get(assigns, :selected_option, "clear_and_run")

{:ok,
socket
|> assign(assigns)
|> assign(
disabled: disabled,
follow_run: follow_run,
selected_option: selected_option
follow_run: nil,
selected_option: "clear_and_run"
)}
end

@impl true
def handle_event("clear_and_run", _params, socket) do
{:noreply, assign(socket, selected_option: "clear_and_run")}
end
cron_trigger =
Enum.find(socket.assigns.workflow.triggers, &(&1.type == :cron))

def handle_event("run_last_state", _params, socket) do
{:noreply, assign(socket, selected_option: "run_last_state")}
end
dataclip_id =
Repo.one(
from(wo in WorkOrder,
where: wo.trigger_id == ^cron_trigger.id,
select: wo.dataclip_id,
limit: 1,
order_by: [desc: :inserted_at]
)
)

send(
self(),
{__MODULE__, "cron_trigger_manual_run", %{dataclip_id: dataclip_id}}
)

def handle_event("run_custom_state", _params, socket) do
{:noreply, assign(socket, selected_option: "run_custom_state")}
{:noreply, socket}
end

# @impl true
# def handle_info(
# %RunUpdated{run: run},
# %{assigns: %{follow_run: %{id: follow_run_id}}} = socket
# )
# when run.id === follow_run_id do
# {:noreply,
# socket
# |> assign(follow_run: run)}
# end

defp cron_trigger_bt_classes(_assigns) do
["relative inline-flex rounded-r-none"]
["relative inline-flex"]
end

def button_text(selected) do
case selected do
"clear_and_run" -> "Clear state and run"
"clear_and_run" -> "Run now"
"run_last_state" -> "Run with last state"
"run_custom_state" -> "Run with custom state"
end
end

defp processing(%{state: state}), do: state not in Run.final_states()
defp processing(_run), do: false

defp dropdown_action(assigns) do
~H"""
<.button
type="button"
class="h-full rounded-l-none pr-1 pl-1 focus:ring-inset"
id="option-menu-button"
aria-expanded="true"
aria-haspopup="true"
disabled={@disabled}
phx-click={show_dropdown("dropdown-bt")}
>
<span class="sr-only">Open options</span>
<.icon name="hero-chevron-down" class="w-4 h-4" />
</.button>
<div
role="menu"
aria-orientation="vertical"
aria-labelledby="option-menu-button"
tabindex="-1"
>
<button
id="dropdown-bt"
phx-target={@myself}
phx-click-away={hide_dropdown("dropdown-bt")}
phx-hook="AltRunViaCtrlShiftEnter"
type="submit"
class={[
"flex justify-start hidden absolute right-0 bottom-9 z-10 mb-2 w-max",
"rounded-md bg-white px-4 py-2 text-sm font-semibold",
"text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50"
]}
disabled={@disabled}
>
<div phx-click="run_last_state">
<.icon name="hero-play-solid" class="w-4 h-4 mr-1" /> Run with last state
</div>
<div phx-click="run_custom_state" class="mt-2">
<.icon name="hero-play-solid" class="w-4 h-4 mr-1" /> Run with custom state
</div>
</button>
</div>
"""
end
end
166 changes: 92 additions & 74 deletions lib/lightning_web/live/workflow_live/edit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,9 @@ defmodule LightningWeb.WorkflowLive.Edit do
<.live_component
id="cron-run-button"
module={LightningWeb.JobLive.CronRunButton}
workflow={@workflow}
follow_run={@follow_run}
snapshot_version_tag={@snapshot_version_tag}
/>
</div>
</div>
Expand Down Expand Up @@ -1670,80 +1673,7 @@ defmodule LightningWeb.WorkflowLive.Edit do
# The manual_run_submit event is for create a new work order from a dataclip and
# a job.
def handle_event("manual_run_submit", params, socket) do
%{
project: project,
selected_job: selected_job,
current_user: current_user,
workflow_params: workflow_params,
has_presence_edit_priority: has_presence_edit_priority,
workflow: workflow,
manual_run_form: form
} = socket.assigns

manual_params = Map.get(params, "manual", %{})

params =
case form do
nil -> manual_params
%{params: form_params} -> Map.merge(form_params, manual_params)
end

socket = socket |> apply_params(workflow_params, :workflow)

workflow_or_changeset =
if has_presence_edit_priority do
socket.assigns.changeset
else
get_workflow_by_id(workflow.id)
end

with :ok <- check_user_can_manual_run_workflow(socket) do
case Helpers.run_workflow(
workflow_or_changeset,
params,
project: project,
selected_job: selected_job,
created_by: current_user
) do
{:ok, %{workorder: workorder, workflow: workflow}} ->
%{runs: [run]} = workorder

Runs.subscribe(run)

snapshot = snapshot_by_version(workflow.id, workflow.lock_version)

{:noreply,
socket
|> assign_workflow(workflow, snapshot)
|> follow_run(run)
|> push_event("push-hash", %{"hash" => "log"})}

{:error, %Ecto.Changeset{data: %WorkOrders.Manual{}} = changeset} ->
{:noreply,
socket
|> assign_manual_run_form(changeset)}

{:error, %Ecto.Changeset{data: %Workflow{}} = changeset} ->
{
:noreply,
socket
|> assign_changeset(changeset)
|> mark_validated()
|> put_flash(:error, "Workflow could not be saved")
}

{:error, %{text: message}} ->
{:noreply, put_flash(socket, :error, message)}

{:error, :workflow_deleted} ->
{:noreply,
put_flash(
socket,
:error,
"Oops! You cannot modify a deleted workflow"
)}
end
end
handle_manual_run_submit(socket, params)
end

def handle_event("toggle_workflow_state", %{"workflow_state" => state}, socket) do
Expand Down Expand Up @@ -1800,6 +1730,15 @@ defmodule LightningWeb.WorkflowLive.Edit do
end
end

def handle_info(
{LightningWeb.JobLive.CronRunButton, "cron_trigger_manual_run",
dataclip_id},
socket
) do
params = %{"manual" => %{"dataclip_id" => dataclip_id}}
handle_manual_run_submit(socket, params)
end

def handle_info({"form_changed", %{"workflow" => params}}, socket) do
{:noreply, handle_new_params(socket, params, :workflow)}
end
Expand Down Expand Up @@ -2637,4 +2576,83 @@ defmodule LightningWeb.WorkflowLive.Edit do
end

defp loaded?(%Workflow{} = workflow), do: workflow.__meta__.state == :loaded

defp handle_manual_run_submit(socket, params) do
IO.inspect(params)

%{
project: project,
selected_job: selected_job,
current_user: current_user,
workflow_params: workflow_params,
has_presence_edit_priority: has_presence_edit_priority,
workflow: workflow,
manual_run_form: form
} = socket.assigns

manual_params = Map.get(params, "manual", %{})

params =
case form do
nil -> manual_params
%{params: form_params} -> Map.merge(form_params, manual_params)
end

socket = socket |> apply_params(workflow_params, :workflow)

workflow_or_changeset =
if has_presence_edit_priority do
socket.assigns.changeset
else
get_workflow_by_id(workflow.id)
end

with :ok <- check_user_can_manual_run_workflow(socket) do
case Helpers.run_workflow(
workflow_or_changeset,
params,
project: project,
selected_job: selected_job,
created_by: current_user
) do
{:ok, %{workorder: workorder, workflow: workflow}} ->
%{runs: [run]} = workorder

Runs.subscribe(run)

snapshot = snapshot_by_version(workflow.id, workflow.lock_version)

{:noreply,
socket
|> assign_workflow(workflow, snapshot)
|> follow_run(run)
|> push_event("push-hash", %{"hash" => "log"})}

{:error, %Ecto.Changeset{data: %WorkOrders.Manual{}} = changeset} ->
{:noreply,
socket
|> assign_manual_run_form(changeset)}

{:error, %Ecto.Changeset{data: %Workflow{}} = changeset} ->
{
:noreply,
socket
|> assign_changeset(changeset)
|> mark_validated()
|> put_flash(:error, "Workflow could not be saved")
}

{:error, %{text: message}} ->
{:noreply, put_flash(socket, :error, message)}

{:error, :workflow_deleted} ->
{:noreply,
put_flash(
socket,
:error,
"Oops! You cannot modify a deleted workflow"
)}
end
end
end
end

0 comments on commit 882914f

Please sign in to comment.