work on contexts

This commit is contained in:
2022-11-24 12:44:34 -05:00
parent a0a0697f2d
commit cc11491106
18 changed files with 771 additions and 249 deletions

View File

@ -0,0 +1,135 @@
defmodule MemexWeb.Components.ContextsTableComponent do
@moduledoc """
A component that displays a list of contexts
"""
use MemexWeb, :live_component
alias Ecto.UUID
alias Memex.{Accounts.User, Contexts, Contexts.Context}
alias Phoenix.LiveView.{Rendered, Socket}
@impl true
@spec update(
%{
required(:id) => UUID.t(),
required(:current_user) => User.t(),
required(:contexts) => [Context.t()],
optional(any()) => any()
},
Socket.t()
) :: {:ok, Socket.t()}
def update(%{id: _id, contexts: _contexts, current_user: _current_user} = assigns, socket) do
socket =
socket
|> assign(assigns)
|> assign_new(:actions, fn -> [] end)
|> display_contexts()
{:ok, socket}
end
defp display_contexts(
%{
assigns: %{
contexts: contexts,
current_user: current_user,
actions: actions
}
} = socket
) do
columns =
if actions == [] or current_user |> is_nil() do
[]
else
[%{label: nil, key: :actions, sortable: false}]
end
columns = [
%{label: gettext("title"), key: :title},
%{label: gettext("content"), key: :content},
%{label: gettext("tags"), key: :tags},
%{label: gettext("visibility"), key: :visibility}
| columns
]
rows =
contexts
|> Enum.map(fn context ->
context
|> get_row_data_for_context(%{
columns: columns,
current_user: current_user,
actions: actions
})
end)
socket |> assign(columns: columns, rows: rows)
end
@impl true
def render(assigns) do
~H"""
<div class="w-full">
<.live_component
module={MemexWeb.Components.TableComponent}
id={@id}
columns={@columns}
rows={@rows}
/>
</div>
"""
end
@spec get_row_data_for_context(Context.t(), additional_data :: map()) :: map()
defp get_row_data_for_context(context, %{columns: columns} = additional_data) do
columns
|> Map.new(fn %{key: key} ->
{key, get_value_for_key(key, context, additional_data)}
end)
end
@spec get_value_for_key(atom(), Context.t(), additional_data :: map()) ::
any() | {any(), Rendered.t()}
defp get_value_for_key(:title, %{id: id, title: title}, _additional_data) do
assigns = %{id: id, title: title}
title_block = ~H"""
<.link
navigate={Routes.context_show_path(Endpoint, :show, @id)}
class="link"
data-qa={"context-show-#{@id}"}
>
<%= @title %>
</.link>
"""
{title, title_block}
end
defp get_value_for_key(:content, %{content: content}, _additional_data) do
assigns = %{content: content}
content_block = ~H"""
<div class="truncate max-w-sm">
<%= @content %>
</div>
"""
{content, content_block}
end
defp get_value_for_key(:tags, %{tags: tags}, _additional_data) do
tags |> Contexts.get_tags_string()
end
defp get_value_for_key(:actions, context, %{actions: actions}) do
assigns = %{actions: actions, context: context}
~H"""
<div class="flex justify-center items-center space-x-4">
<%= render_slot(@actions, @context) %>
</div>
"""
end
defp get_value_for_key(key, context, _additional_data), do: context |> Map.get(key)
end

View File

@ -4,8 +4,8 @@ defmodule MemexWeb.ContextLive.FormComponent do
alias Memex.Contexts
@impl true
def update(%{context: context} = assigns, socket) do
changeset = Contexts.change_context(context)
def update(%{context: context, current_user: current_user} = assigns, socket) do
changeset = Contexts.change_context(context, current_user)
{:ok,
socket
@ -14,39 +14,52 @@ defmodule MemexWeb.ContextLive.FormComponent do
end
@impl true
def handle_event("validate", %{"context" => context_params}, socket) do
def handle_event(
"validate",
%{"context" => context_params},
%{assigns: %{context: context, current_user: current_user}} = socket
) do
changeset =
socket.assigns.context
|> Contexts.change_context(context_params)
context
|> Contexts.change_context(context_params, current_user)
|> 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)
def handle_event("save", %{"context" => context_params}, %{assigns: %{action: action}} = socket) do
save_context(socket, action, context_params)
end
defp save_context(socket, :edit, context_params) do
case Contexts.update_context(socket.assigns.context, context_params) do
{:ok, _context} ->
defp save_context(
%{assigns: %{context: context, return_to: return_to, current_user: current_user}} =
socket,
:edit,
context_params
) do
case Contexts.update_context(context, context_params, current_user) do
{:ok, %{title: title}} ->
{:noreply,
socket
|> put_flash(:info, "context updated successfully")
|> push_navigate(to: socket.assigns.return_to)}
|> put_flash(:info, gettext("%{title} saved", title: title))
|> push_navigate(to: 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} ->
defp save_context(
%{assigns: %{return_to: return_to, current_user: current_user}} = socket,
:new,
context_params
) do
case Contexts.create_context(context_params, current_user) do
{:ok, %{title: title}} ->
{:noreply,
socket
|> put_flash(:info, "context created successfully")
|> push_navigate(to: socket.assigns.return_to)}
|> put_flash(:info, gettext("%{title} created", title: title))
|> push_navigate(to: return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)}

View File

@ -1,6 +1,4 @@
<div>
<h2><%= @title %></h2>
<div class="h-full flex flex-col justify-start items-stretch space-y-4">
<.form
:let={f}
for={@changeset}
@ -8,27 +6,44 @@
phx-target={@myself}
phx-change="validate"
phx-submit="save"
phx-debounce="300"
class="flex flex-col justify-start items-stretch space-y-4"
>
<%= label(f, :title) %>
<%= text_input(f, :title) %>
<%= text_input(f, :title,
class: "input input-primary",
placeholder: gettext("title")
) %>
<%= error_tag(f, :title) %>
<%= label(f, :content) %>
<%= textarea(f, :content) %>
<%= textarea(f, :content,
id: "context-form-content",
class: "input input-primary h-64 min-h-64",
phx_hook: "MaintainAttrs",
phx_update: "ignore",
placeholder: gettext("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"
<%= text_input(f, :tags_string,
id: "tags-input",
class: "input input-primary",
placeholder: gettext("tag1,tag2"),
phx_update: "ignore",
value: Contexts.get_tags_string(@changeset)
) %>
<%= error_tag(f, :visibility) %>
<%= error_tag(f, :tags_string) %>
<div>
<%= submit("Save", phx_disable_with: "Saving...") %>
<div class="flex justify-center items-stretch space-x-4">
<%= select(f, :visibility, Ecto.Enum.values(Memex.Contexts.Context, :visibility),
class: "grow input input-primary",
prompt: gettext("select privacy")
) %>
<%= submit(dgettext("actions", "save"),
phx_disable_with: gettext("saving..."),
class: "mx-auto btn btn-primary"
) %>
</div>
<%= error_tag(f, :visibility) %>
</.form>
</div>

View File

@ -1,46 +1,80 @@
defmodule MemexWeb.ContextLive.Index do
use MemexWeb, :live_view
alias Memex.Contexts
alias Memex.Contexts.Context
alias Memex.{Contexts, Contexts.Context}
@impl true
def mount(%{"search" => search}, _session, socket) do
{:ok, socket |> assign(search: search) |> display_contexts()}
end
def mount(_params, _session, socket) do
{:ok, assign(socket, :contexts, list_contexts())}
{:ok, socket |> assign(search: nil) |> display_contexts()}
end
@impl true
def handle_params(params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
{:noreply, apply_action(socket, live_action, params)}
end
defp apply_action(socket, :edit, %{"id" => id}) do
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
%{title: title} = context = Contexts.get_context!(id, current_user)
socket
|> assign(:page_title, "edit context")
|> assign(:context, Contexts.get_context!(id))
|> assign(page_title: gettext("edit %{title}", title: title))
|> assign(context: context)
end
defp apply_action(socket, :new, _params) do
defp apply_action(%{assigns: %{current_user: %{id: current_user_id}}} = socket, :new, _params) do
socket
|> assign(:page_title, "new context")
|> assign(:context, %Context{})
|> assign(page_title: gettext("new context"))
|> assign(context: %Context{user_id: current_user_id})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "listing contexts")
|> assign(:context, nil)
|> assign(page_title: gettext("contexts"))
|> assign(search: nil)
|> assign(context: nil)
|> display_contexts()
end
defp apply_action(socket, :search, %{"search" => search}) do
socket
|> assign(page_title: gettext("contexts"))
|> assign(search: search)
|> assign(context: nil)
|> display_contexts()
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
context = Contexts.get_context!(id)
{:ok, _} = Contexts.delete_context(context)
def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
context = Contexts.get_context!(id, current_user)
{:ok, %{title: title}} = Contexts.delete_context(context, current_user)
{:noreply, assign(socket, :contexts, list_contexts())}
socket =
socket
|> assign(contexts: Contexts.list_contexts(current_user))
|> put_flash(:info, gettext("%{title} deleted", title: title))
{:noreply, socket}
end
defp list_contexts do
Contexts.list_contexts()
@impl true
def handle_event("search", %{"search" => %{"search_term" => ""}}, socket) do
{:noreply, socket |> push_patch(to: Routes.context_index_path(Endpoint, :index))}
end
def handle_event("search", %{"search" => %{"search_term" => search_term}}, socket) do
{:noreply,
socket |> push_patch(to: Routes.context_index_path(Endpoint, :search, search_term))}
end
defp display_contexts(%{assigns: %{current_user: current_user, search: search}} = socket)
when not (current_user |> is_nil()) do
socket |> assign(contexts: Contexts.list_contexts(search, current_user))
end
defp display_contexts(%{assigns: %{search: search}} = socket) do
socket |> assign(contexts: Contexts.list_public_contexts(search))
end
end

View File

@ -1,10 +1,69 @@
<h1>listing contexts</h1>
<div class="mx-auto flex flex-col justify-center items-start space-y-4 max-w-3xl">
<h1 class="text-xl">
<%= gettext("contexts") %>
</h1>
<.form
:let={f}
for={:search}
phx-change="search"
phx-submit="search"
class="self-stretch flex flex-col items-stretch"
>
<%= text_input(f, :search_term,
class: "input input-primary",
value: @search,
phx_debounce: 300,
placeholder: gettext("search")
) %>
</.form>
<%= if @contexts |> Enum.empty?() do %>
<h1 class="self-center text-primary-500">
<%= gettext("no contexts found") %>
</h1>
<% else %>
<.live_component
module={MemexWeb.Components.ContextsTableComponent}
id="contexts-index-table"
current_user={@current_user}
contexts={@contexts}
>
<:actions :let={context}>
<%= if @current_user do %>
<.link
patch={Routes.context_index_path(@socket, :edit, context)}
data-qa={"context-edit-#{context.id}"}
>
<%= dgettext("actions", "edit") %>
</.link>
<.link
href="#"
phx-click="delete"
phx-value-id={context.id}
data-confirm={dgettext("prompts", "are you sure?")}
data-qa={"delete-context-#{context.id}"}
>
<%= dgettext("actions", "delete") %>
</.link>
<% end %>
</:actions>
</.live_component>
<% end %>
<%= if @current_user do %>
<.link patch={Routes.context_index_path(@socket, :new)} class="self-end btn btn-primary">
<%= dgettext("actions", "new context") %>
</.link>
<% end %>
</div>
<%= 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}
current_user={@current_user}
title={@page_title}
action={@live_action}
context={@context}
@ -12,55 +71,3 @@
/>
</.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>
<.link navigate={Routes.context_show_path(@socket, :show, context)}>
<%= dgettext("actions", "show") %>
</.link>
</span>
<span>
<.link patch={Routes.context_index_path(@socket, :edit, context)}>
<%= dgettext("actions", "edit") %>
</.link>
</span>
<span>
<.link
href="#"
phx-click="delete"
phx-value-id={context.id}
data-confirm={dgettext("prompts", "are you sure?")}
>
<%= dgettext("actions", "delete") %>
</.link>
</span>
</td>
</tr>
<% end %>
</tbody>
</table>
<span>
<.link patch={Routes.context_index_path(@socket, :new)}>
<%= dgettext("actions", "new context") %>
</.link>
</span>

View File

@ -9,13 +9,33 @@ defmodule MemexWeb.ContextLive.Show do
end
@impl true
def handle_params(%{"id" => id}, _, socket) do
def handle_params(
%{"id" => id},
_,
%{assigns: %{live_action: live_action, current_user: current_user}} = socket
) do
{:noreply,
socket
|> assign(:page_title, page_title(socket.assigns.live_action))
|> assign(:context, Contexts.get_context!(id))}
|> assign(:page_title, page_title(live_action))
|> assign(:context, Contexts.get_context!(id, current_user))}
end
defp page_title(:show), do: "show context"
defp page_title(:edit), do: "edit context"
@impl true
def handle_event(
"delete",
_params,
%{assigns: %{context: context, current_user: current_user}} = socket
) do
{:ok, %{title: title}} = Contexts.delete_context(context, current_user)
socket =
socket
|> put_flash(:info, gettext("%{title} deleted", title: title))
|> push_navigate(to: Routes.context_index_path(Endpoint, :index))
{:noreply, socket}
end
defp page_title(:show), do: gettext("show context")
defp page_title(:edit), do: gettext("edit context")
end

View File

@ -1,10 +1,51 @@
<h1>show context</h1>
<div class="mx-auto flex flex-col justify-center items-stretch space-y-4 max-w-3xl">
<h1 class="text-xl">
<%= @context.title %>
</h1>
<p><%= if @context.tags, do: @context.tags |> Enum.join(", ") %></p>
<textarea
id="show-context-content"
class="input input-primary h-128 min-h-128"
phx-hook="MaintainAttrs"
phx-update="ignore"
readonly
phx-no-format
><%= @context.content %></textarea>
<p class="self-end">
<%= gettext("Visibility: %{visibility}", visibility: @context.visibility) %>
</p>
<div class="self-end flex space-x-4">
<.link class="btn btn-primary" patch={Routes.context_index_path(@socket, :index)}>
<%= dgettext("actions", "back") %>
</.link>
<%= if @current_user do %>
<.link class="btn btn-primary" patch={Routes.context_show_path(@socket, :edit, @context)}>
<%= dgettext("actions", "edit") %>
</.link>
<button
type="button"
class="btn btn-primary"
phx-click="delete"
data-confirm={dgettext("prompts", "are you sure?")}
data-qa={"delete-context-#{@context.id}"}
>
<%= dgettext("actions", "delete") %>
</button>
<% end %>
</div>
</div>
<%= 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}
current_user={@current_user}
title={@page_title}
action={@live_action}
context={@context}
@ -12,37 +53,3 @@
/>
</.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>
<.link patch={Routes.context_show_path(@socket, :edit, @context)} class="button">
<%= dgettext("actions", "edit") %>
</.link>
</span>
|
<span>
<.link navigate={Routes.context_index_path(@socket, :index)}>
<%= dgettext("actions", "Back") %>
</.link>
</span>

View File

@ -63,7 +63,7 @@ defmodule MemexWeb.Router do
live "/contexts/new", ContextLive.Index, :new
live "/contexts/:id/edit", ContextLive.Index, :edit
live "/contexts/:id/show/edit", ContextLive.Show, :edit
live "/context/:id/edit", ContextLive.Show, :edit
live "/pipelines/new", PipelineLive.Index, :new
live "/pipelines/:id/edit", PipelineLive.Index, :edit
@ -83,7 +83,8 @@ defmodule MemexWeb.Router do
live "/note/:id", NoteLive.Show, :show
live "/contexts", ContextLive.Index, :index
live "/contexts/:id", ContextLive.Show, :show
live "/contexts/:search", ContextLive.Index, :search
live "/context/:id", ContextLive.Show, :show
live "/pipelines", PipelineLive.Index, :index
live "/pipelines/:id", PipelineLive.Show, :show