4VGHWZGPJLZLDYEQQZIT37MIICFM34LDTW6C3TPV5DESZ7P2EWIQC
<h1>Show Layout</h1>
<%= if @live_action in [:edit] do %>
<.modal return_to={Routes.layout_show_path(@socket, :show, @layout)}>
<.live_component
module={CakeWeb.LayoutLive.FormComponent}
id={@layout.id}
title={@page_title}
action={@live_action}
layout={@layout}
return_to={Routes.layout_show_path(@socket, :show, @layout)}
/>
</.modal>
<% end %>
<ul>
<li>
<strong>Name:</strong>
<%= @layout.name %>
</li>
<li>
<strong>Version:</strong>
<%= @layout.version %>
</li>
</ul>
<span><%= live_patch "Edit", to: Routes.layout_show_path(@socket, :edit, @layout), class: "button" %></span> |
<span><%= live_redirect "Back", to: Routes.layout_index_path(@socket, :index) %></span>
defmodule CakeWeb.LayoutLive.Show do
use CakeWeb, :live_view
alias Cake.Layouts
@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(:layout, Layouts.get_layout!(id))}
end
defp page_title(:show), do: "Show Layout"
defp page_title(:edit), do: "Edit Layout"
end
<h1>Listing Layouts</h1>
<%= if @live_action in [:new, :edit] do %>
<.modal return_to={Routes.layout_index_path(@socket, :index)}>
<.live_component
module={CakeWeb.LayoutLive.FormComponent}
id={@layout.id || :new}
title={@page_title}
action={@live_action}
layout={@layout}
return_to={Routes.layout_index_path(@socket, :index)}
/>
</.modal>
<% end %>
<table>
<thead>
<tr>
<th>Name</th>
<th>Version</th>
<th></th>
</tr>
</thead>
<tbody id="layouts">
<%= for layout <- @layouts do %>
<tr id={"layout-#{layout.id}"}>
<td><%= layout.name %></td>
<td><%= layout.version %></td>
<td>
<span><%= live_redirect "Show", to: Routes.layout_show_path(@socket, :show, layout) %></span>
<span><%= live_patch "Edit", to: Routes.layout_index_path(@socket, :edit, layout) %></span>
<span><%= link "Delete", to: "#", phx_click: "delete", phx_value_id: layout.id, data: [confirm: "Are you sure?"] %></span>
</td>
</tr>
<% end %>
</tbody>
</table>
<span><%= live_patch "New Layout", to: Routes.layout_index_path(@socket, :new) %></span>
defmodule CakeWeb.LayoutLive.Index do
use CakeWeb, :live_view
alias Cake.Layouts
alias Cake.Layouts.Layout
@impl true
def mount(_params, _session, socket) do
{:ok, assign(socket, :layouts, list_layouts())}
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 Layout")
|> assign(:layout, Layouts.get_layout!(id))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Layout")
|> assign(:layout, %Layout{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Layouts")
|> assign(:layout, nil)
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
layout = Layouts.get_layout!(id)
{:ok, _} = Layouts.delete_layout(layout)
{:noreply, assign(socket, :layouts, list_layouts())}
end
defp list_layouts do
Layouts.list_layouts()
end
end
<div>
<h2><%= @title %></h2>
<.form
let={f}
for={@changeset}
id="layout-form"
phx-target={@myself}
phx-change="validate"
phx-submit="save">
<%= label f, :name %>
<%= text_input f, :name %>
<%= error_tag f, :name %>
<%= label f, :version %>
<%= number_input f, :version %>
<%= error_tag f, :version %>
<div>
<%= submit "Save", phx_disable_with: "Saving..." %>
</div>
</.form>
</div>
defmodule CakeWeb.LayoutLive.FormComponent do
use CakeWeb, :live_component
alias Cake.Layouts
@impl true
def update(%{layout: layout} = assigns, socket) do
changeset = Layouts.change_layout(layout)
{:ok,
socket
|> assign(assigns)
|> assign(:changeset, changeset)}
end
@impl true
def handle_event("validate", %{"layout" => layout_params}, socket) do
changeset =
socket.assigns.layout
|> Layouts.change_layout(layout_params)
|> Map.put(:action, :validate)
{:noreply, assign(socket, :changeset, changeset)}
end
def handle_event("save", %{"layout" => layout_params}, socket) do
save_layout(socket, socket.assigns.action, layout_params)
end
defp save_layout(socket, :edit, layout_params) do
case Layouts.update_layout(socket.assigns.layout, layout_params) do
{:ok, _layout} ->
{:noreply,
socket
|> put_flash(:info, "Layout updated successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)}
end
end
defp save_layout(socket, :new, layout_params) do
case Layouts.create_layout(layout_params) do
{:ok, _layout} ->
{:noreply,
socket
|> put_flash(:info, "Layout created successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)}
end
end
end