add contexts
This commit is contained in:
55
lib/memex_web/live/context_live/form_component.ex
Normal file
55
lib/memex_web/live/context_live/form_component.ex
Normal file
@ -0,0 +1,55 @@
|
||||
defmodule MemexWeb.ContextLive.FormComponent do
|
||||
use MemexWeb, :live_component
|
||||
|
||||
alias Memex.Contexts
|
||||
|
||||
@impl true
|
||||
def update(%{context: context} = assigns, socket) do
|
||||
changeset = Contexts.change_context(context)
|
||||
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(assigns)
|
||||
|> assign(:changeset, changeset)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("validate", %{"context" => context_params}, socket) do
|
||||
changeset =
|
||||
socket.assigns.context
|
||||
|> Contexts.change_context(context_params)
|
||||
|> Map.put(:action, :validate)
|
||||
|
||||
{:noreply, assign(socket, :changeset, changeset)}
|
||||
end
|
||||
|
||||
def handle_event("save", %{"context" => context_params}, socket) do
|
||||
save_context(socket, socket.assigns.action, context_params)
|
||||
end
|
||||
|
||||
defp save_context(socket, :edit, context_params) do
|
||||
case Contexts.update_context(socket.assigns.context, context_params) do
|
||||
{:ok, _context} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Context updated successfully")
|
||||
|> push_redirect(to: socket.assigns.return_to)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, assign(socket, :changeset, changeset)}
|
||||
end
|
||||
end
|
||||
|
||||
defp save_context(socket, :new, context_params) do
|
||||
case Contexts.create_context(context_params) do
|
||||
{:ok, _context} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Context created successfully")
|
||||
|> push_redirect(to: socket.assigns.return_to)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, assign(socket, changeset: changeset)}
|
||||
end
|
||||
end
|
||||
end
|
32
lib/memex_web/live/context_live/form_component.html.heex
Normal file
32
lib/memex_web/live/context_live/form_component.html.heex
Normal file
@ -0,0 +1,32 @@
|
||||
<div>
|
||||
<h2><%= @title %></h2>
|
||||
|
||||
<.form
|
||||
let={f}
|
||||
for={@changeset}
|
||||
id="context-form"
|
||||
phx-target={@myself}
|
||||
phx-change="validate"
|
||||
phx-submit="save">
|
||||
|
||||
<%= label f, :title %>
|
||||
<%= text_input f, :title %>
|
||||
<%= error_tag f, :title %>
|
||||
|
||||
<%= label f, :content %>
|
||||
<%= textarea f, :content %>
|
||||
<%= error_tag f, :content %>
|
||||
|
||||
<%= label f, :tag %>
|
||||
<%= multiple_select f, :tag, ["Option 1": "option1", "Option 2": "option2"] %>
|
||||
<%= error_tag f, :tag %>
|
||||
|
||||
<%= label f, :visibility %>
|
||||
<%= select f, :visibility, Ecto.Enum.values(Memex.Contexts.Context, :visibility), prompt: "Choose a value" %>
|
||||
<%= error_tag f, :visibility %>
|
||||
|
||||
<div>
|
||||
<%= submit "Save", phx_disable_with: "Saving..." %>
|
||||
</div>
|
||||
</.form>
|
||||
</div>
|
46
lib/memex_web/live/context_live/index.ex
Normal file
46
lib/memex_web/live/context_live/index.ex
Normal file
@ -0,0 +1,46 @@
|
||||
defmodule MemexWeb.ContextLive.Index do
|
||||
use MemexWeb, :live_view
|
||||
|
||||
alias Memex.Contexts
|
||||
alias Memex.Contexts.Context
|
||||
|
||||
@impl true
|
||||
def mount(_params, _session, socket) do
|
||||
{:ok, assign(socket, :contexts, list_contexts())}
|
||||
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 Context")
|
||||
|> assign(:context, Contexts.get_context!(id))
|
||||
end
|
||||
|
||||
defp apply_action(socket, :new, _params) do
|
||||
socket
|
||||
|> assign(:page_title, "New Context")
|
||||
|> assign(:context, %Context{})
|
||||
end
|
||||
|
||||
defp apply_action(socket, :index, _params) do
|
||||
socket
|
||||
|> assign(:page_title, "Listing Contexts")
|
||||
|> assign(:context, nil)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("delete", %{"id" => id}, socket) do
|
||||
context = Contexts.get_context!(id)
|
||||
{:ok, _} = Contexts.delete_context(context)
|
||||
|
||||
{:noreply, assign(socket, :contexts, list_contexts())}
|
||||
end
|
||||
|
||||
defp list_contexts do
|
||||
Contexts.list_contexts()
|
||||
end
|
||||
end
|
45
lib/memex_web/live/context_live/index.html.heex
Normal file
45
lib/memex_web/live/context_live/index.html.heex
Normal file
@ -0,0 +1,45 @@
|
||||
<h1>Listing Contexts</h1>
|
||||
|
||||
<%= if @live_action in [:new, :edit] do %>
|
||||
<.modal return_to={Routes.context_index_path(@socket, :index)}>
|
||||
<.live_component
|
||||
module={MemexWeb.ContextLive.FormComponent}
|
||||
id={@context.id || :new}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
context={@context}
|
||||
return_to={Routes.context_index_path(@socket, :index)}
|
||||
/>
|
||||
</.modal>
|
||||
<% end %>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Title</th>
|
||||
<th>Content</th>
|
||||
<th>Tag</th>
|
||||
<th>Visibility</th>
|
||||
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="contexts">
|
||||
<%= for context <- @contexts do %>
|
||||
<tr id={"context-#{context.id}"}>
|
||||
<td><%= context.title %></td>
|
||||
<td><%= context.content %></td>
|
||||
<td><%= context.tag %></td>
|
||||
<td><%= context.visibility %></td>
|
||||
|
||||
<td>
|
||||
<span><%= live_redirect "Show", to: Routes.context_show_path(@socket, :show, context) %></span>
|
||||
<span><%= live_patch "Edit", to: Routes.context_index_path(@socket, :edit, context) %></span>
|
||||
<span><%= link "Delete", to: "#", phx_click: "delete", phx_value_id: context.id, data: [confirm: "Are you sure?"] %></span>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<span><%= live_patch "New Context", to: Routes.context_index_path(@socket, :new) %></span>
|
21
lib/memex_web/live/context_live/show.ex
Normal file
21
lib/memex_web/live/context_live/show.ex
Normal file
@ -0,0 +1,21 @@
|
||||
defmodule MemexWeb.ContextLive.Show do
|
||||
use MemexWeb, :live_view
|
||||
|
||||
alias Memex.Contexts
|
||||
|
||||
@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(:context, Contexts.get_context!(id))}
|
||||
end
|
||||
|
||||
defp page_title(:show), do: "Show Context"
|
||||
defp page_title(:edit), do: "Edit Context"
|
||||
end
|
41
lib/memex_web/live/context_live/show.html.heex
Normal file
41
lib/memex_web/live/context_live/show.html.heex
Normal file
@ -0,0 +1,41 @@
|
||||
<h1>Show Context</h1>
|
||||
|
||||
<%= if @live_action in [:edit] do %>
|
||||
<.modal return_to={Routes.context_show_path(@socket, :show, @context)}>
|
||||
<.live_component
|
||||
module={MemexWeb.ContextLive.FormComponent}
|
||||
id={@context.id}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
context={@context}
|
||||
return_to={Routes.context_show_path(@socket, :show, @context)}
|
||||
/>
|
||||
</.modal>
|
||||
<% end %>
|
||||
|
||||
<ul>
|
||||
|
||||
<li>
|
||||
<strong>Title:</strong>
|
||||
<%= @context.title %>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<strong>Content:</strong>
|
||||
<%= @context.content %>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<strong>Tag:</strong>
|
||||
<%= @context.tag %>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<strong>Visibility:</strong>
|
||||
<%= @context.visibility %>
|
||||
</li>
|
||||
|
||||
</ul>
|
||||
|
||||
<span><%= live_patch "Edit", to: Routes.context_show_path(@socket, :edit, @context), class: "button" %></span> |
|
||||
<span><%= live_redirect "Back", to: Routes.context_index_path(@socket, :index) %></span>
|
Reference in New Issue
Block a user