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>