Add error messages for creating/editing pipeline stages

This commit is contained in:
Shadowfacts 2019-07-20 20:06:23 -04:00
parent 7f3ff17925
commit d0c6831d45
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
4 changed files with 68 additions and 32 deletions

View File

@ -49,45 +49,70 @@ defmodule FrenzyWeb.PipelineController do
stage = conn.assigns[:stage] stage = conn.assigns[:stage]
{:ok, options_json} = Jason.encode(stage.options, pretty: true) {:ok, options_json} = Jason.encode(stage.options, pretty: true)
changeset =
PipelineStage.changeset(stage, %{
options: options_json
})
render(conn, "edit.html", %{ render(conn, "edit.html", %{
feed: feed, feed: feed,
stage: stage, stage: stage,
options: options_json changeset: changeset
}) })
end end
def update(conn, %{"options" => options_json}) do def update(conn, %{"pipeline_stage" => %{"options" => options_json}}) do
feed = conn.assigns[:feed] feed = conn.assigns[:feed]
stage = conn.assigns[:stage] stage = conn.assigns[:stage]
{:ok, options} = Jason.decode(options_json)
with {:ok, options} <- Jason.decode(options_json) do
changeset = PipelineStage.changeset(stage, %{options: options}) changeset = PipelineStage.changeset(stage, %{options: options})
{:ok, _stage} = Repo.update(changeset) {:ok, _stage} = Repo.update(changeset)
conn conn
|> put_flash(:info, "Pipeline Stage updated") |> put_flash(:info, "Pipeline Stage updated")
|> redirect(to: Routes.feed_path(Endpoint, :edit, feed.id)) |> redirect(to: Routes.feed_path(Endpoint, :edit, feed.id))
else
result ->
error_changeset = PipelineStage.changeset(stage, %{options: options_json})
conn
|> put_flash(:error, "Unable to update pipeline stage: #{inspect(result)}")
|> render("edit.html", %{
feed: feed,
stage: stage,
changeset: error_changeset
})
end
end end
def new(conn, _params) do def new(conn, _params) do
changeset = PipelineStage.changeset(%PipelineStage{}, %{}) feed = conn.assigns[:feed]
changeset =
PipelineStage.changeset(%PipelineStage{}, %{
index: feed.pipeline_stages |> Enum.count(),
options: "{}"
})
render(conn, "new.html", %{ render(conn, "new.html", %{
feed: conn.assigns[:feed], feed: feed,
changeset: changeset changeset: changeset
}) })
end end
def create(conn, %{ def create(conn, %{
"pipeline_stage" => %{ "pipeline_stage" =>
%{
"index" => index, "index" => index,
"module_name" => module_name, "module_name" => module_name,
"options" => options_json "options" => options_json
} } = params
}) do }) do
feed = conn.assigns[:feed] feed = conn.assigns[:feed]
{index, _} = Integer.parse(index)
{:ok, options} = Jason.decode(options_json)
with {index, _} <- Integer.parse(index),
{:ok, options} <- Jason.decode(options_json) do
changeset = changeset =
Ecto.build_assoc(feed, :pipeline_stages, %{ Ecto.build_assoc(feed, :pipeline_stages, %{
index: index, index: index,
@ -100,6 +125,17 @@ defmodule FrenzyWeb.PipelineController do
conn conn
|> put_flash(:info, "Pipeline Stage created") |> put_flash(:info, "Pipeline Stage created")
|> redirect(to: Routes.feed_path(Endpoint, :edit, feed.id)) |> redirect(to: Routes.feed_path(Endpoint, :edit, feed.id))
else
result ->
error_changeset = PipelineStage.changeset(%PipelineStage{}, params)
conn
|> put_flash(:error, "Unable to create pipeline stage: #{inspect(result)}")
|> render("new.html", %{
feed: feed,
changeset: error_changeset
})
end
end end
def delete(conn, _params) do def delete(conn, _params) do

View File

@ -53,7 +53,7 @@ defmodule FrenzyWeb.Router do
# resources "/pipelines", PipelineController, only: [:edit, :update, :new, :create] # resources "/pipelines", PipelineController, only: [:edit, :update, :new, :create]
get "/feeds/:feed_id/pipelines/:stage_id/edit", PipelineController, :edit get "/feeds/:feed_id/pipelines/:stage_id/edit", PipelineController, :edit
post "/feeds/:feed_id/pipelines/:stage_id/update", PipelineController, :update put "/feeds/:feed_id/pipelines/:stage_id/update", PipelineController, :update
delete "/feeds/:feed_id/pipelines/:stage_id/delete", PipelineController, :delete delete "/feeds/:feed_id/pipelines/:stage_id/delete", PipelineController, :delete
get "/feeds/:feed_id/pipelines/new", PipelineController, :new get "/feeds/:feed_id/pipelines/new", PipelineController, :new
post "/feeds/:feed_id/pipelines/create", PipelineController, :create post "/feeds/:feed_id/pipelines/create", PipelineController, :create

View File

@ -2,8 +2,8 @@
<h3>Feed: <%= @feed.title %></h3> <h3>Feed: <%= @feed.title %></h3>
<h3>Module: <code><%= @stage.module_name %></code></h3> <h3>Module: <code><%= @stage.module_name %></code></h3>
<%= form_for @conn, Routes.pipeline_path(@conn, :update, @feed.id, @stage.id), fn f -> %> <%= form_for @changeset, Routes.pipeline_path(@conn, :update, @feed.id, @stage.id), fn f -> %>
<%= textarea f, :options, value: @options, class: "form-control", rows: 15, style: "font-family: monospace;" %> <%= textarea f, :options, class: "form-control", rows: 15, style: "font-family: monospace;" %>
<%= submit "Update", class: "btn btn-primary mt-2" %> <%= submit "Update", class: "btn btn-primary mt-2" %>
<% end %> <% end %>

View File

@ -10,13 +10,13 @@
<div class="form-group row"> <div class="form-group row">
<label class="col-sm-2 col-form-label" for="index">Index</label> <label class="col-sm-2 col-form-label" for="index">Index</label>
<div class="col-sm-10"> <div class="col-sm-10">
<%= number_input f, :index, value: "#{@feed.pipeline_stages |> Enum.count()}", class: "form-control" %> <%= number_input f, :index, class: "form-control" %>
</div> </div>
</div> </div>
<div class="form-group row"> <div class="form-group row">
<label class="col-sm-2 col-form-label" for="options">Options</label> <label class="col-sm-2 col-form-label" for="options">Options</label>
<div class="col-sm-10"> <div class="col-sm-10">
<%= textarea f, :options, value: "{}", class: "form-control", rows: 15, style: "font-family: monospace;" %> <%= textarea f, :options, class: "form-control", rows: 15, style: "font-family: monospace;" %>
</div> </div>
</div> </div>
<div class="form-group row"> <div class="form-group row">