add pipelines

This commit is contained in:
2022-07-25 20:12:11 -04:00
parent 3347b26256
commit 0fca0ead12
13 changed files with 569 additions and 0 deletions

View File

@ -0,0 +1,55 @@
defmodule MemexWeb.PipelineLive.FormComponent do
use MemexWeb, :live_component
alias Memex.Pipelines
@impl true
def update(%{pipeline: pipeline} = assigns, socket) do
changeset = Pipelines.change_pipeline(pipeline)
{:ok,
socket
|> assign(assigns)
|> assign(:changeset, changeset)}
end
@impl true
def handle_event("validate", %{"pipeline" => pipeline_params}, socket) do
changeset =
socket.assigns.pipeline
|> Pipelines.change_pipeline(pipeline_params)
|> Map.put(:action, :validate)
{:noreply, assign(socket, :changeset, changeset)}
end
def handle_event("save", %{"pipeline" => pipeline_params}, socket) do
save_pipeline(socket, socket.assigns.action, pipeline_params)
end
defp save_pipeline(socket, :edit, pipeline_params) do
case Pipelines.update_pipeline(socket.assigns.pipeline, pipeline_params) do
{:ok, _pipeline} ->
{:noreply,
socket
|> put_flash(:info, "Pipeline updated successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)}
end
end
defp save_pipeline(socket, :new, pipeline_params) do
case Pipelines.create_pipeline(pipeline_params) do
{:ok, _pipeline} ->
{:noreply,
socket
|> put_flash(:info, "Pipeline created successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)}
end
end
end

View File

@ -0,0 +1,28 @@
<div>
<h2><%= @title %></h2>
<.form
let={f}
for={@changeset}
id="pipeline-form"
phx-target={@myself}
phx-change="validate"
phx-submit="save">
<%= label f, :title %>
<%= text_input f, :title %>
<%= error_tag f, :title %>
<%= label f, :description %>
<%= textarea f, :description %>
<%= error_tag f, :description %>
<%= label f, :visibility %>
<%= select f, :visibility, Ecto.Enum.values(Memex.Pipelines.Pipeline, :visibility), prompt: "Choose a value" %>
<%= error_tag f, :visibility %>
<div>
<%= submit "Save", phx_disable_with: "Saving..." %>
</div>
</.form>
</div>

View File

@ -0,0 +1,46 @@
defmodule MemexWeb.PipelineLive.Index do
use MemexWeb, :live_view
alias Memex.Pipelines
alias Memex.Pipelines.Pipeline
@impl true
def mount(_params, _session, socket) do
{:ok, assign(socket, :pipelines, list_pipelines())}
end
@impl true
def handle_params(params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end
defp apply_action(socket, :edit, %{"id" => id}) do
socket
|> assign(:page_title, "Edit Pipeline")
|> assign(:pipeline, Pipelines.get_pipeline!(id))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Pipeline")
|> assign(:pipeline, %Pipeline{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Pipelines")
|> assign(:pipeline, nil)
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
pipeline = Pipelines.get_pipeline!(id)
{:ok, _} = Pipelines.delete_pipeline(pipeline)
{:noreply, assign(socket, :pipelines, list_pipelines())}
end
defp list_pipelines do
Pipelines.list_pipelines()
end
end

View File

@ -0,0 +1,43 @@
<h1>Listing Pipelines</h1>
<%= if @live_action in [:new, :edit] do %>
<.modal return_to={Routes.pipeline_index_path(@socket, :index)}>
<.live_component
module={MemexWeb.PipelineLive.FormComponent}
id={@pipeline.id || :new}
title={@page_title}
action={@live_action}
pipeline={@pipeline}
return_to={Routes.pipeline_index_path(@socket, :index)}
/>
</.modal>
<% end %>
<table>
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Visibility</th>
<th></th>
</tr>
</thead>
<tbody id="pipelines">
<%= for pipeline <- @pipelines do %>
<tr id={"pipeline-#{pipeline.id}"}>
<td><%= pipeline.title %></td>
<td><%= pipeline.description %></td>
<td><%= pipeline.visibility %></td>
<td>
<span><%= live_redirect "Show", to: Routes.pipeline_show_path(@socket, :show, pipeline) %></span>
<span><%= live_patch "Edit", to: Routes.pipeline_index_path(@socket, :edit, pipeline) %></span>
<span><%= link "Delete", to: "#", phx_click: "delete", phx_value_id: pipeline.id, data: [confirm: "Are you sure?"] %></span>
</td>
</tr>
<% end %>
</tbody>
</table>
<span><%= live_patch "New Pipeline", to: Routes.pipeline_index_path(@socket, :new) %></span>

View File

@ -0,0 +1,21 @@
defmodule MemexWeb.PipelineLive.Show do
use MemexWeb, :live_view
alias Memex.Pipelines
@impl true
def mount(_params, _session, socket) do
{:ok, socket}
end
@impl true
def handle_params(%{"id" => id}, _, socket) do
{:noreply,
socket
|> assign(:page_title, page_title(socket.assigns.live_action))
|> assign(:pipeline, Pipelines.get_pipeline!(id))}
end
defp page_title(:show), do: "Show Pipeline"
defp page_title(:edit), do: "Edit Pipeline"
end

View File

@ -0,0 +1,36 @@
<h1>Show Pipeline</h1>
<%= if @live_action in [:edit] do %>
<.modal return_to={Routes.pipeline_show_path(@socket, :show, @pipeline)}>
<.live_component
module={MemexWeb.PipelineLive.FormComponent}
id={@pipeline.id}
title={@page_title}
action={@live_action}
pipeline={@pipeline}
return_to={Routes.pipeline_show_path(@socket, :show, @pipeline)}
/>
</.modal>
<% end %>
<ul>
<li>
<strong>Title:</strong>
<%= @pipeline.title %>
</li>
<li>
<strong>Description:</strong>
<%= @pipeline.description %>
</li>
<li>
<strong>Visibility:</strong>
<%= @pipeline.visibility %>
</li>
</ul>
<span><%= live_patch "Edit", to: Routes.pipeline_show_path(@socket, :edit, @pipeline), class: "button" %></span> |
<span><%= live_redirect "Back", to: Routes.pipeline_index_path(@socket, :index) %></span>