frenzy/lib/frenzy_web/live/edit_pipeline_live.ex

124 lines
3.2 KiB
Elixir
Raw Normal View History

defmodule FrenzyWeb.EditPipelineLive do
use FrenzyWeb, :live_view
use Phoenix.HTML
alias Frenzy.{Repo, Pipeline}
2021-08-28 15:58:22 +00:00
def title(%{pipeline: %Pipeline{name: name}}) do
"Edit #{name}"
end
2020-06-10 23:05:37 +00:00
@stages [
{"Filter Stage", "Frenzy.Pipeline.FilterStage"},
{"Scrape Stage", "Frenzy.Pipeline.ScrapeStage"},
2020-07-18 23:50:41 +00:00
{"Conditional Stage", "Frenzy.Pipeline.ConditionalStage"},
{"Gemini Scrape Stage", "Frenzy.Pipeline.GeminiScrapeStage"},
{"Render Gemini Stage", "Frenzy.Pipeline.RenderGeminiStage"}
2020-06-10 23:05:37 +00:00
]
2020-07-18 14:25:43 +00:00
def stages, do: @stages
@impl true
2020-07-18 14:44:14 +00:00
def mount(_, %{"pipeline_id" => pipeline_id}, socket) do
pipeline = Repo.get(Pipeline, pipeline_id)
{:ok,
assign(socket,
pipeline: pipeline,
2020-06-10 23:05:37 +00:00
stages: @stages
)}
end
@impl true
def handle_event("add_stage", %{"stage" => %{"module_name" => module}}, socket) do
pipeline = socket.assigns[:pipeline]
default_options = apply(String.to_existing_atom("Elixir." <> module), :default_opts, [])
changeset =
Pipeline.changeset(pipeline, %{
stages:
pipeline.stages ++
[%{"module_name" => module, "options" => default_options}]
})
{:ok, pipeline} = Repo.update(changeset)
{:noreply, assign(socket, pipeline: pipeline)}
end
def handle_event("delete_stage", %{"index" => index}, socket) do
pipeline = socket.assigns.pipeline
index = String.to_integer(index)
changeset =
Pipeline.changeset(pipeline, %{
stages: List.delete_at(pipeline.stages, index)
})
{:ok, pipeline} = Repo.update(changeset)
{:noreply, assign(socket, pipeline: pipeline)}
end
2020-06-09 03:27:40 +00:00
def handle_event(event, %{"index" => index}, socket) when event in ["move_up", "move_down"] do
index = String.to_integer(index)
offset =
case event do
"move_up" -> -1
"move_down" -> 1
end
pipeline = socket.assigns.pipeline
stage = Enum.at(pipeline.stages, index)
changeset =
Pipeline.changeset(pipeline, %{
stages:
pipeline.stages
|> List.delete_at(index)
|> List.insert_at(index + offset, stage)
})
{:ok, pipeline} = Repo.update(changeset)
{:noreply, assign(socket, pipeline: pipeline)}
end
@impl true
2020-06-10 23:05:37 +00:00
def handle_info({:update_stage, index, new_stage}, socket) do
pipeline = socket.assigns.pipeline
2020-06-10 23:05:37 +00:00
new_stages = List.replace_at(pipeline.stages, index, new_stage)
changeset = Pipeline.changeset(pipeline, %{stages: new_stages})
{:ok, pipeline} = Repo.update(changeset)
{:noreply, assign(socket, pipeline: pipeline)}
end
def component_module(module_name) do
case module_name do
"Frenzy.Pipeline.ScrapeStage" ->
FrenzyWeb.ConfigureStage.ScrapeStageLive
"Frenzy.Pipeline.FilterStage" ->
FrenzyWeb.ConfigureStage.FilterStageLive
2020-07-18 14:25:43 +00:00
"Frenzy.Pipeline.ConditionalStage" ->
FrenzyWeb.ConfigureStage.ConditionalStageLive
2020-06-10 23:05:37 +00:00
_ ->
nil
end
end
2023-06-25 20:45:59 +00:00
def component_for(%{"module_name" => module} = stage, index) do
case component_module(module) do
nil ->
nil
component ->
2023-06-25 20:45:59 +00:00
live_component(component,
2020-06-10 23:05:37 +00:00
index: index,
id: "stage-#{index}",
stage: stage,
keypath: ["options"]
)
end
end
end