Add error messages for creating/editing pipeline stages
This commit is contained in:
parent
7f3ff17925
commit
d0c6831d45
|
@ -49,57 +49,93 @@ 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)
|
|
||||||
changeset = PipelineStage.changeset(stage, %{options: options})
|
|
||||||
{:ok, _stage} = Repo.update(changeset)
|
|
||||||
|
|
||||||
conn
|
with {:ok, options} <- Jason.decode(options_json) do
|
||||||
|> put_flash(:info, "Pipeline Stage updated")
|
changeset = PipelineStage.changeset(stage, %{options: options})
|
||||||
|> redirect(to: Routes.feed_path(Endpoint, :edit, feed.id))
|
{:ok, _stage} = Repo.update(changeset)
|
||||||
|
|
||||||
|
conn
|
||||||
|
|> put_flash(:info, "Pipeline Stage updated")
|
||||||
|
|> 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,
|
%{
|
||||||
"module_name" => module_name,
|
"index" => index,
|
||||||
"options" => options_json
|
"module_name" => module_name,
|
||||||
}
|
"options" => options_json
|
||||||
|
} = params
|
||||||
}) do
|
}) do
|
||||||
feed = conn.assigns[:feed]
|
feed = conn.assigns[:feed]
|
||||||
{index, _} = Integer.parse(index)
|
|
||||||
{:ok, options} = Jason.decode(options_json)
|
|
||||||
|
|
||||||
changeset =
|
with {index, _} <- Integer.parse(index),
|
||||||
Ecto.build_assoc(feed, :pipeline_stages, %{
|
{:ok, options} <- Jason.decode(options_json) do
|
||||||
index: index,
|
changeset =
|
||||||
module_name: module_name,
|
Ecto.build_assoc(feed, :pipeline_stages, %{
|
||||||
options: options
|
index: index,
|
||||||
})
|
module_name: module_name,
|
||||||
|
options: options
|
||||||
|
})
|
||||||
|
|
||||||
{:ok, _stage} = Repo.insert(changeset)
|
{:ok, _stage} = Repo.insert(changeset)
|
||||||
|
|
||||||
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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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 %>
|
||||||
|
|
||||||
|
|
|
@ -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">
|
||||||
|
|
Loading…
Reference in New Issue