add models, context and liveviews

This commit is contained in:
2021-09-02 23:31:14 -04:00
committed by oliviasculley
parent feba4e1d14
commit d6ddf2a9bb
52 changed files with 2325 additions and 13 deletions

View File

@ -0,0 +1,55 @@
defmodule CanneryWeb.AmmoGroupLive.FormComponent do
use CanneryWeb, :live_component
alias Cannery.Ammo
@impl true
def update(%{ammo_group: ammo_group} = assigns, socket) do
changeset = Ammo.change_ammo_group(ammo_group)
{:ok,
socket
|> assign(assigns)
|> assign(:changeset, changeset)}
end
@impl true
def handle_event("validate", %{"ammo_group" => ammo_group_params}, socket) do
changeset =
socket.assigns.ammo_group
|> Ammo.change_ammo_group(ammo_group_params)
|> Map.put(:action, :validate)
{:noreply, socket |> assign(:changeset, changeset)}
end
def handle_event("save", %{"ammo_group" => ammo_group_params}, socket) do
save_ammo_group(socket, socket.assigns.action, ammo_group_params)
end
defp save_ammo_group(socket, :edit, ammo_group_params) do
case Ammo.update_ammo_group(socket.assigns.ammo_group, ammo_group_params) do
{:ok, _ammo_group} ->
{:noreply,
socket
|> put_flash(:info, "Ammo group updated successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, socket |> assign(:changeset, changeset)}
end
end
defp save_ammo_group(socket, :new, ammo_group_params) do
case Ammo.create_ammo_group(ammo_group_params) do
{:ok, _ammo_group} ->
{:noreply,
socket
|> put_flash(:info, "Ammo group created successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, socket |> assign(changeset: changeset)}
end
end
end

View File

@ -0,0 +1,22 @@
<h2><%= @title %></h2>
<%= f = form_for @changeset, "#",
id: "ammo_group-form",
phx_target: @myself,
phx_change: "validate",
phx_submit: "save" %>
<%= label f, :count %>
<%= number_input f, :count %>
<%= error_tag f, :count %>
<%= label f, :price_paid %>
<%= number_input f, :price_paid, step: "any" %>
<%= error_tag f, :price_paid %>
<%= label f, :notes %>
<%= textarea f, :notes %>
<%= error_tag f, :notes %>
<%= submit "Save", phx_disable_with: "Saving..." %>
</form>

View File

@ -0,0 +1,46 @@
defmodule CanneryWeb.AmmoGroupLive.Index do
use CanneryWeb, :live_view
alias Cannery.Ammo
alias Cannery.Ammo.AmmoGroup
@impl true
def mount(_params, _session, socket) do
{:ok, socket |> assign(:ammo_groups, list_ammo_groups())}
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 Ammo group")
|> assign(:ammo_group, Ammo.get_ammo_group!(id))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Ammo group")
|> assign(:ammo_group, %AmmoGroup{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Ammo groups")
|> assign(:ammo_group, nil)
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
ammo_group = Ammo.get_ammo_group!(id)
{:ok, _} = Ammo.delete_ammo_group(ammo_group)
{:noreply, socket |> assign(:ammo_groups, list_ammo_groups())}
end
defp list_ammo_groups do
Ammo.list_ammo_groups()
end
end

View File

@ -0,0 +1,39 @@
<h1>Listing Ammo groups</h1>
<%= if @live_action in [:new, :edit] do %>
<%= live_modal CanneryWeb.AmmoGroupLive.FormComponent,
id: @ammo_group.id || :new,
title: @page_title,
action: @live_action,
ammo_group: @ammo_group,
return_to: Routes.ammo_group_index_path(@socket, :index) %>
<% end %>
<table>
<thead>
<tr>
<th>Count</th>
<th>Price paid</th>
<th>Notes</th>
<th></th>
</tr>
</thead>
<tbody id="ammo_groups">
<%= for ammo_group <- @ammo_groups do %>
<tr id="ammo_group-<%= ammo_group.id %>">
<td><%= ammo_group.count %></td>
<td><%= ammo_group.price_paid %></td>
<td><%= ammo_group.notes %></td>
<td>
<span><%= live_redirect "Show", to: Routes.ammo_group_show_path(@socket, :show, ammo_group) %></span>
<span><%= live_patch "Edit", to: Routes.ammo_group_index_path(@socket, :edit, ammo_group) %></span>
<span><%= link "Delete", to: "#", phx_click: "delete", phx_value_id: ammo_group.id, data: [confirm: "Are you sure?"] %></span>
</td>
</tr>
<% end %>
</tbody>
</table>
<span><%= live_patch "New Ammo group", to: Routes.ammo_group_index_path(@socket, :new) %></span>

View File

@ -0,0 +1,21 @@
defmodule CanneryWeb.AmmoGroupLive.Show do
use CanneryWeb, :live_view
alias Cannery.Ammo
@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(:ammo_group, Ammo.get_ammo_group!(id))}
end
defp page_title(:show), do: "Show Ammo group"
defp page_title(:edit), do: "Edit Ammo group"
end

View File

@ -0,0 +1,32 @@
<h1>Show Ammo group</h1>
<%= if @live_action in [:edit] do %>
<%= live_modal CanneryWeb.AmmoGroupLive.FormComponent,
id: @ammo_group.id,
title: @page_title,
action: @live_action,
ammo_group: @ammo_group,
return_to: Routes.ammo_group_show_path(@socket, :show, @ammo_group) %>
<% end %>
<ul>
<li>
<strong>Count:</strong>
<%= @ammo_group.count %>
</li>
<li>
<strong>Price paid:</strong>
<%= @ammo_group.price_paid %>
</li>
<li>
<strong>Notes:</strong>
<%= @ammo_group.notes %>
</li>
</ul>
<span><%= live_patch "Edit", to: Routes.ammo_group_show_path(@socket, :edit, @ammo_group), class: "button" %></span>
<span><%= live_redirect "Back", to: Routes.ammo_group_index_path(@socket, :index) %></span>

View File

@ -0,0 +1,55 @@
defmodule CanneryWeb.AmmoTypeLive.FormComponent do
use CanneryWeb, :live_component
alias Cannery.Ammo
@impl true
def update(%{ammo_type: ammo_type} = assigns, socket) do
changeset = Ammo.change_ammo_type(ammo_type)
{:ok,
socket
|> assign(assigns)
|> assign(:changeset, changeset)}
end
@impl true
def handle_event("validate", %{"ammo_type" => ammo_type_params}, socket) do
changeset =
socket.assigns.ammo_type
|> Ammo.change_ammo_type(ammo_type_params)
|> Map.put(:action, :validate)
{:noreply, socket |> assign(:changeset, changeset)}
end
def handle_event("save", %{"ammo_type" => ammo_type_params}, socket) do
save_ammo_type(socket, socket.assigns.action, ammo_type_params)
end
defp save_ammo_type(socket, :edit, ammo_type_params) do
case Ammo.update_ammo_type(socket.assigns.ammo_type, ammo_type_params) do
{:ok, _ammo_type} ->
{:noreply,
socket
|> put_flash(:info, "Ammo type updated successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, socket |> assign(:changeset, changeset)}
end
end
defp save_ammo_type(socket, :new, ammo_type_params) do
case Ammo.create_ammo_type(ammo_type_params) do
{:ok, _ammo_type} ->
{:noreply,
socket
|> put_flash(:info, "Ammo type created successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, socket |> assign(changeset: changeset)}
end
end
end

View File

@ -0,0 +1,34 @@
<h2><%= @title %></h2>
<%= f = form_for @changeset, "#",
id: "ammo_type-form",
phx_target: @myself,
phx_change: "validate",
phx_submit: "save" %>
<%= label f, :name %>
<%= text_input f, :name %>
<%= error_tag f, :name %>
<%= label f, :desc %>
<%= text_input f, :desc %>
<%= error_tag f, :desc %>
<%= label f, :case_material %>
<%= text_input f, :case_material %>
<%= error_tag f, :case_material %>
<%= label f, :bullet_type %>
<%= text_input f, :bullet_type %>
<%= error_tag f, :bullet_type %>
<%= label f, :weight %>
<%= number_input f, :weight, step: "any" %>
<%= error_tag f, :weight %>
<%= label f, :manufacturer %>
<%= text_input f, :manufacturer %>
<%= error_tag f, :manufacturer %>
<%= submit "Save", phx_disable_with: "Saving..." %>
</form>

View File

@ -0,0 +1,46 @@
defmodule CanneryWeb.AmmoTypeLive.Index do
use CanneryWeb, :live_view
alias Cannery.Ammo
alias Cannery.Ammo.AmmoType
@impl true
def mount(_params, _session, socket) do
{:ok, socket |> assign(:ammo_types, list_ammo_types())}
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 Ammo type")
|> assign(:ammo_type, Ammo.get_ammo_type!(id))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Ammo type")
|> assign(:ammo_type, %AmmoType{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Ammo types")
|> assign(:ammo_type, nil)
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
ammo_type = Ammo.get_ammo_type!(id)
{:ok, _} = Ammo.delete_ammo_type(ammo_type)
{:noreply, socket |> assign(:ammo_types, list_ammo_types())}
end
defp list_ammo_types do
Ammo.list_ammo_types()
end
end

View File

@ -0,0 +1,45 @@
<h1>Listing Ammo types</h1>
<%= if @live_action in [:new, :edit] do %>
<%= live_modal CanneryWeb.AmmoTypeLive.FormComponent,
id: @ammo_type.id || :new,
title: @page_title,
action: @live_action,
ammo_type: @ammo_type,
return_to: Routes.ammo_type_index_path(@socket, :index) %>
<% end %>
<table>
<thead>
<tr>
<th>Name</th>
<th>Desc</th>
<th>Case material</th>
<th>Bullet type</th>
<th>Weight</th>
<th>Manufacturer</th>
<th></th>
</tr>
</thead>
<tbody id="ammo_types">
<%= for ammo_type <- @ammo_types do %>
<tr id="ammo_type-<%= ammo_type.id %>">
<td><%= ammo_type.name %></td>
<td><%= ammo_type.desc %></td>
<td><%= ammo_type.case_material %></td>
<td><%= ammo_type.bullet_type %></td>
<td><%= ammo_type.weight %></td>
<td><%= ammo_type.manufacturer %></td>
<td>
<span><%= live_redirect "Show", to: Routes.ammo_type_show_path(@socket, :show, ammo_type) %></span>
<span><%= live_patch "Edit", to: Routes.ammo_type_index_path(@socket, :edit, ammo_type) %></span>
<span><%= link "Delete", to: "#", phx_click: "delete", phx_value_id: ammo_type.id, data: [confirm: "Are you sure?"] %></span>
</td>
</tr>
<% end %>
</tbody>
</table>
<span><%= live_patch "New Ammo type", to: Routes.ammo_type_index_path(@socket, :new) %></span>

View File

@ -0,0 +1,21 @@
defmodule CanneryWeb.AmmoTypeLive.Show do
use CanneryWeb, :live_view
alias Cannery.Ammo
@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(:ammo_type, Ammo.get_ammo_type!(id))}
end
defp page_title(:show), do: "Show Ammo type"
defp page_title(:edit), do: "Edit Ammo type"
end

View File

@ -0,0 +1,47 @@
<h1>Show Ammo type</h1>
<%= if @live_action in [:edit] do %>
<%= live_modal CanneryWeb.AmmoTypeLive.FormComponent,
id: @ammo_type.id,
title: @page_title,
action: @live_action,
ammo_type: @ammo_type,
return_to: Routes.ammo_type_show_path(@socket, :show, @ammo_type) %>
<% end %>
<ul>
<li>
<strong>Name:</strong>
<%= @ammo_type.name %>
</li>
<li>
<strong>Desc:</strong>
<%= @ammo_type.desc %>
</li>
<li>
<strong>Case material:</strong>
<%= @ammo_type.case_material %>
</li>
<li>
<strong>Bullet type:</strong>
<%= @ammo_type.bullet_type %>
</li>
<li>
<strong>Weight:</strong>
<%= @ammo_type.weight %>
</li>
<li>
<strong>Manufacturer:</strong>
<%= @ammo_type.manufacturer %>
</li>
</ul>
<span><%= live_patch "Edit", to: Routes.ammo_type_show_path(@socket, :edit, @ammo_type), class: "button" %></span>
<span><%= live_redirect "Back", to: Routes.ammo_type_index_path(@socket, :index) %></span>

View File

@ -1,12 +1,12 @@
defmodule CanneryWeb.Live.Component.Topbar do
use CanneryWeb, :live_component
alias CanneryWeb.{PageLive}
def mount(socket) do
{:ok, socket |> assign(results: [], title_content: nil)}
end
def update(assigns, socket) do
{:ok, socket |> assign(assigns)}
end
@ -41,7 +41,7 @@ defmodule CanneryWeb.Live.Component.Topbar do
</form>
<%# user settings %>
<%= if assigns |> Map.has_key?(:current_user) do %>
<%= if @current_user do %>
<li>
<%= @current_user.email %></li>
<li>
@ -89,4 +89,4 @@ defmodule CanneryWeb.Live.Component.Topbar do
</header>
"""
end
end
end

View File

@ -0,0 +1,55 @@
defmodule CanneryWeb.ContainerLive.FormComponent do
use CanneryWeb, :live_component
alias Cannery.Containers
@impl true
def update(%{container: container} = assigns, socket) do
changeset = Containers.change_container(container)
{:ok,
socket
|> assign(assigns)
|> assign(:changeset, changeset)}
end
@impl true
def handle_event("validate", %{"container" => container_params}, socket) do
changeset =
socket.assigns.container
|> Containers.change_container(container_params)
|> Map.put(:action, :validate)
{:noreply, socket |> assign(:changeset, changeset)}
end
def handle_event("save", %{"container" => container_params}, socket) do
save_container(socket, socket.assigns.action, container_params)
end
defp save_container(socket, :edit, container_params) do
case Containers.update_container(socket.assigns.container, container_params) do
{:ok, _container} ->
{:noreply,
socket
|> put_flash(:info, "Container updated successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, socket |> assign(:changeset, changeset)}
end
end
defp save_container(socket, :new, container_params) do
case Containers.create_container(container_params) do
{:ok, _container} ->
{:noreply,
socket
|> put_flash(:info, "Container created successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, socket |> assign(changeset: changeset)}
end
end
end

View File

@ -0,0 +1,26 @@
<h2><%= @title %></h2>
<%= f = form_for @changeset, "#",
id: "container-form",
phx_target: @myself,
phx_change: "validate",
phx_submit: "save" %>
<%= label f, :name %>
<%= text_input f, :name %>
<%= error_tag f, :name %>
<%= label f, :desc %>
<%= text_input f, :desc %>
<%= error_tag f, :desc %>
<%= label f, :type %>
<%= text_input f, :type %>
<%= error_tag f, :type %>
<%= label f, :location %>
<%= text_input f, :location %>
<%= error_tag f, :location %>
<%= submit "Save", phx_disable_with: "Saving..." %>
</form>

View File

@ -0,0 +1,46 @@
defmodule CanneryWeb.ContainerLive.Index do
use CanneryWeb, :live_view
alias Cannery.Containers
alias Cannery.Containers.Container
@impl true
def mount(_params, _session, socket) do
{:ok, socket |> assign(:containers, list_containers())}
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 Container")
|> assign(:container, Containers.get_container!(id))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Container")
|> assign(:container, %Container{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Containers")
|> assign(:container, nil)
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
container = Containers.get_container!(id)
{:ok, _} = Containers.delete_container(container)
{:noreply, socket |> assign(:containers, list_containers())}
end
defp list_containers do
Containers.list_containers()
end
end

View File

@ -0,0 +1,41 @@
<h1>Listing Containers</h1>
<%= if @live_action in [:new, :edit] do %>
<%= live_modal CanneryWeb.ContainerLive.FormComponent,
id: @container.id || :new,
title: @page_title,
action: @live_action,
container: @container,
return_to: Routes.container_index_path(@socket, :index) %>
<% end %>
<table>
<thead>
<tr>
<th>Name</th>
<th>Desc</th>
<th>Type</th>
<th>Location</th>
<th></th>
</tr>
</thead>
<tbody id="containers">
<%= for container <- @containers do %>
<tr id="container-<%= container.id %>">
<td><%= container.name %></td>
<td><%= container.desc %></td>
<td><%= container.type %></td>
<td><%= container.location %></td>
<td>
<span><%= live_redirect "Show", to: Routes.container_show_path(@socket, :show, container) %></span>
<span><%= live_patch "Edit", to: Routes.container_index_path(@socket, :edit, container) %></span>
<span><%= link "Delete", to: "#", phx_click: "delete", phx_value_id: container.id, data: [confirm: "Are you sure?"] %></span>
</td>
</tr>
<% end %>
</tbody>
</table>
<span><%= live_patch "New Container", to: Routes.container_index_path(@socket, :new) %></span>

View File

@ -0,0 +1,21 @@
defmodule CanneryWeb.ContainerLive.Show do
use CanneryWeb, :live_view
alias Cannery.Containers
@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(:container, Containers.get_container!(id))}
end
defp page_title(:show), do: "Show Container"
defp page_title(:edit), do: "Edit Container"
end

View File

@ -0,0 +1,37 @@
<h1>Show Container</h1>
<%= if @live_action in [:edit] do %>
<%= live_modal CanneryWeb.ContainerLive.FormComponent,
id: @container.id,
title: @page_title,
action: @live_action,
container: @container,
return_to: Routes.container_show_path(@socket, :show, @container) %>
<% end %>
<ul>
<li>
<strong>Name:</strong>
<%= @container.name %>
</li>
<li>
<strong>Desc:</strong>
<%= @container.desc %>
</li>
<li>
<strong>Type:</strong>
<%= @container.type %>
</li>
<li>
<strong>Location:</strong>
<%= @container.location %>
</li>
</ul>
<span><%= live_patch "Edit", to: Routes.container_show_path(@socket, :edit, @container), class: "button" %></span>
<span><%= live_redirect "Back", to: Routes.container_index_path(@socket, :index) %></span>

View File

@ -0,0 +1,36 @@
defmodule CanneryWeb.LiveHelpers do
import Phoenix.LiveView.Helpers
import Phoenix.LiveView, only: [assign_new: 3]
alias Cannery.{Accounts}
@doc """
Renders a component inside the `CanneryWeb.ModalComponent` component.
The rendered modal receives a `:return_to` option to properly update
the URL when the modal is closed.
## Examples
<%= live_modal CanneryWeb.TagLive.FormComponent,
id: @tag.id || :new,
action: @live_action,
tag: @tag,
return_to: Routes.tag_index_path(@socket, :index) %>
"""
def live_modal(component, opts) do
path = Keyword.fetch!(opts, :return_to)
modal_opts = [id: :modal, return_to: path, component: component, opts: opts]
live_component(CanneryWeb.ModalComponent, modal_opts)
end
def assign_defaults(socket, %{"user_token" => user_token} = _session) do
socket
|> assign_new(:current_user, fn ->
Accounts.get_user_by_session_token(user_token)
end)
end
def assign_defaults(socket, _session) do
socket
end
end

View File

@ -0,0 +1,26 @@
defmodule CanneryWeb.ModalComponent do
use CanneryWeb, :live_component
@impl true
def render(assigns) do
~L"""
<div id="<%= @id %>" class="phx-modal"
phx-capture-click="close"
phx-window-keydown="close"
phx-key="escape"
phx-target="#<%= @id %>"
phx-page-loading>
<div class="phx-modal-content">
<%= live_patch raw("&times;"), to: @return_to, class: "phx-modal-close" %>
<%= live_component @socket, @component, @opts %>
</div>
</div>
"""
end
@impl true
def handle_event("close", _, socket) do
{:noreply, push_patch(socket, to: socket.assigns.return_to)}
end
end

View File

@ -2,13 +2,13 @@ defmodule CanneryWeb.PageLive do
use CanneryWeb, :live_view
@impl true
def mount(_params, _session, socket) do
{:ok, assign(socket, query: "", results: %{})}
def mount(_params, session, socket) do
{:ok, socket |> assign_defaults(session) |> assign(query: "", results: %{})}
end
@impl true
def handle_event("suggest", %{"q" => query}, socket) do
{:noreply, assign(socket, results: search(query), query: query)}
{:noreply, socket |> assign(results: search(query), query: query)}
end
@impl true

View File

@ -0,0 +1,55 @@
defmodule CanneryWeb.TagLive.FormComponent do
use CanneryWeb, :live_component
alias Cannery.Tags
@impl true
def update(%{tag: tag} = assigns, socket) do
changeset = Tags.change_tag(tag)
{:ok,
socket
|> assign(assigns)
|> assign(:changeset, changeset)}
end
@impl true
def handle_event("validate", %{"tag" => tag_params}, socket) do
changeset =
socket.assigns.tag
|> Tags.change_tag(tag_params)
|> Map.put(:action, :validate)
{:noreply, socket |> assign(:changeset, changeset)}
end
def handle_event("save", %{"tag" => tag_params}, socket) do
save_tag(socket, socket.assigns.action, tag_params)
end
defp save_tag(socket, :edit, tag_params) do
case Tags.update_tag(socket.assigns.tag, tag_params) do
{:ok, _tag} ->
{:noreply,
socket
|> put_flash(:info, "Tag updated successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, socket |> assign(:changeset, changeset)}
end
end
defp save_tag(socket, :new, tag_params) do
case Tags.create_tag(tag_params) do
{:ok, _tag} ->
{:noreply,
socket
|> put_flash(:info, "Tag created successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, socket |> assign(changeset: changeset)}
end
end
end

View File

@ -0,0 +1,22 @@
<h2><%= @title %></h2>
<%= f = form_for @changeset, "#",
id: "tag-form",
phx_target: @myself,
phx_change: "validate",
phx_submit: "save" %>
<%= label f, :name %>
<%= text_input f, :name %>
<%= error_tag f, :name %>
<%= label f, :"bg-color" %>
<%= text_input f, :"bg-color" %>
<%= error_tag f, :"bg-color" %>
<%= label f, :"text-color" %>
<%= text_input f, :"text-color" %>
<%= error_tag f, :"text-color" %>
<%= submit "Save", phx_disable_with: "Saving..." %>
</form>

View File

@ -0,0 +1,46 @@
defmodule CanneryWeb.TagLive.Index do
use CanneryWeb, :live_view
alias Cannery.Tags
alias Cannery.Tags.Tag
@impl true
def mount(_params, _session, socket) do
{:ok, socket |> assign(:tags, list_tags())}
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 Tag")
|> assign(:tag, Tags.get_tag!(id))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Tag")
|> assign(:tag, %Tag{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Tags")
|> assign(:tag, nil)
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
tag = Tags.get_tag!(id)
{:ok, _} = Tags.delete_tag(tag)
{:noreply, socket |> assign(:tags, list_tags())}
end
defp list_tags do
Tags.list_tags()
end
end

View File

@ -0,0 +1,39 @@
<h1>Listing Tags</h1>
<%= if @live_action in [:new, :edit] do %>
<%= live_modal CanneryWeb.TagLive.FormComponent,
id: @tag.id || :new,
title: @page_title,
action: @live_action,
tag: @tag,
return_to: Routes.tag_index_path(@socket, :index) %>
<% end %>
<table>
<thead>
<tr>
<th>Name</th>
<th>Bg-color</th>
<th>Text-color</th>
<th></th>
</tr>
</thead>
<tbody id="tags">
<%= for tag <- @tags do %>
<tr id="tag-<%= tag.id %>">
<td><%= tag.name %></td>
<td><%= tag.bg_color %></td>
<td><%= tag.text_color %></td>
<td>
<span><%= live_redirect "Show", to: Routes.tag_show_path(@socket, :show, tag) %></span>
<span><%= live_patch "Edit", to: Routes.tag_index_path(@socket, :edit, tag) %></span>
<span><%= link "Delete", to: "#", phx_click: "delete", phx_value_id: tag.id, data: [confirm: "Are you sure?"] %></span>
</td>
</tr>
<% end %>
</tbody>
</table>
<span><%= live_patch "New Tag", to: Routes.tag_index_path(@socket, :new) %></span>

View File

@ -0,0 +1,21 @@
defmodule CanneryWeb.TagLive.Show do
use CanneryWeb, :live_view
alias Cannery.Tags
@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(:tag, Tags.get_tag!(id))}
end
defp page_title(:show), do: "Show Tag"
defp page_title(:edit), do: "Edit Tag"
end

View File

@ -0,0 +1,32 @@
<h1>Show Tag</h1>
<%= if @live_action in [:edit] do %>
<%= live_modal CanneryWeb.TagLive.FormComponent,
id: @tag.id,
title: @page_title,
action: @live_action,
tag: @tag,
return_to: Routes.tag_show_path(@socket, :show, @tag) %>
<% end %>
<ul>
<li>
<strong>Name:</strong>
<%= @tag.name %>
</li>
<li>
<strong>Bg-color:</strong>
<%= @tag.bg_color %>
</li>
<li>
<strong>Text-color:</strong>
<%= @tag.text_color %>
</li>
</ul>
<span><%= live_patch "Edit", to: Routes.tag_show_path(@socket, :edit, @tag), class: "button" %></span>
<span><%= live_redirect "Back", to: Routes.tag_index_path(@socket, :index) %></span>