From 5f81d8dfe45bb7296975f2ccf706486aff75e2b1 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 8 Jun 2020 23:27:40 -0400 Subject: [PATCH] Add pipeline stage reordering --- lib/frenzy_web/live/edit_pipeline_live.ex | 24 +++++++++++++++++++ .../live/edit_pipeline_live.html.leex | 6 +++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/frenzy_web/live/edit_pipeline_live.ex b/lib/frenzy_web/live/edit_pipeline_live.ex index ae12186..66a90fb 100644 --- a/lib/frenzy_web/live/edit_pipeline_live.ex +++ b/lib/frenzy_web/live/edit_pipeline_live.ex @@ -47,6 +47,30 @@ defmodule FrenzyWeb.EditPipelineLive do {:noreply, assign(socket, pipeline: pipeline)} end + 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 def handle_info({:update_stage_opts, index, new_opts}, socket) do pipeline = socket.assigns.pipeline diff --git a/lib/frenzy_web/live/edit_pipeline_live.html.leex b/lib/frenzy_web/live/edit_pipeline_live.html.leex index 17f1382..c39f493 100644 --- a/lib/frenzy_web/live/edit_pipeline_live.html.leex +++ b/lib/frenzy_web/live/edit_pipeline_live.html.leex @@ -8,13 +8,15 @@

<%= stage["module_name"] %>

+ <%= content_tag :button, "Move Up", [phx_click: :move_up, phx_value_index: index, disabled: index == 0] %> + + <%= content_tag :button, "Move Down", [phx_click: :move_down, phx_value_index: index, disabled: index == length(@pipeline.stages) - 1] %> +
- <%#
<%= Jason.encode!(stage["options"], pretty: true) %1>
%> - <%# <%= live_component(@socket, ) %1> %> <%= component_for(@socket, stage, index) %>