use slugs
This commit is contained in:
@ -38,10 +38,10 @@ defmodule MemexWeb.ContextLive.FormComponent do
|
||||
context_params
|
||||
) do
|
||||
case Contexts.update_context(context, context_params, current_user) do
|
||||
{:ok, %{title: title}} ->
|
||||
{:ok, %{slug: slug}} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, gettext("%{title} saved", title: title))
|
||||
|> put_flash(:info, gettext("%{slug} saved", slug: slug))
|
||||
|> push_navigate(to: return_to)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
@ -55,10 +55,10 @@ defmodule MemexWeb.ContextLive.FormComponent do
|
||||
context_params
|
||||
) do
|
||||
case Contexts.create_context(context_params, current_user) do
|
||||
{:ok, %{title: title}} ->
|
||||
{:ok, %{slug: slug}} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, gettext("%{title} created", title: title))
|
||||
|> put_flash(:info, gettext("%{slug} created", slug: slug))
|
||||
|> push_navigate(to: return_to)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
|
@ -9,11 +9,11 @@
|
||||
phx-debounce="300"
|
||||
class="flex flex-col justify-start items-stretch space-y-4"
|
||||
>
|
||||
<%= text_input(f, :title,
|
||||
<%= text_input(f, :slug,
|
||||
class: "input input-primary",
|
||||
placeholder: gettext("title")
|
||||
placeholder: gettext("slug")
|
||||
) %>
|
||||
<%= error_tag(f, :title) %>
|
||||
<%= error_tag(f, :slug) %>
|
||||
|
||||
<%= textarea(f, :content,
|
||||
id: "context-form-content",
|
||||
|
@ -16,11 +16,11 @@ defmodule MemexWeb.ContextLive.Index do
|
||||
{:noreply, apply_action(socket, live_action, params)}
|
||||
end
|
||||
|
||||
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
|
||||
%{title: title} = context = Contexts.get_context!(id, current_user)
|
||||
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"slug" => slug}) do
|
||||
%{slug: slug} = context = Contexts.get_context_by_slug(slug, current_user)
|
||||
|
||||
socket
|
||||
|> assign(page_title: gettext("edit %{title}", title: title))
|
||||
|> assign(page_title: gettext("edit %{slug}", slug: slug))
|
||||
|> assign(context: context)
|
||||
end
|
||||
|
||||
@ -49,12 +49,12 @@ defmodule MemexWeb.ContextLive.Index do
|
||||
@impl true
|
||||
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)
|
||||
{:ok, %{slug: slug}} = Contexts.delete_context(context, current_user)
|
||||
|
||||
socket =
|
||||
socket
|
||||
|> assign(contexts: Contexts.list_contexts(current_user))
|
||||
|> put_flash(:info, gettext("%{title} deleted", title: title))
|
||||
|> put_flash(:info, gettext("%{slug} deleted", slug: slug))
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
@ -32,7 +32,7 @@
|
||||
<:actions :let={context}>
|
||||
<%= if is_owner?(context, @current_user) do %>
|
||||
<.link
|
||||
patch={Routes.context_index_path(@socket, :edit, context)}
|
||||
patch={Routes.context_index_path(@socket, :edit, context.slug)}
|
||||
data-qa={"context-edit-#{context.id}"}
|
||||
>
|
||||
<%= dgettext("actions", "edit") %>
|
||||
|
@ -10,11 +10,15 @@ defmodule MemexWeb.ContextLive.Show do
|
||||
|
||||
@impl true
|
||||
def handle_params(
|
||||
%{"id" => id},
|
||||
%{"slug" => slug},
|
||||
_,
|
||||
%{assigns: %{live_action: live_action, current_user: current_user}} = socket
|
||||
) do
|
||||
context = Contexts.get_context!(id, current_user)
|
||||
context =
|
||||
case Contexts.get_context_by_slug(slug, current_user) do
|
||||
nil -> raise MemexWeb.NotFoundError, gettext("%{slug} could not be found", slug: slug)
|
||||
context -> context
|
||||
end
|
||||
|
||||
socket =
|
||||
socket
|
||||
@ -30,18 +34,18 @@ defmodule MemexWeb.ContextLive.Show do
|
||||
_params,
|
||||
%{assigns: %{context: context, current_user: current_user}} = socket
|
||||
) do
|
||||
{:ok, %{title: title}} = Contexts.delete_context(context, current_user)
|
||||
{:ok, %{slug: slug}} = Contexts.delete_context(context, current_user)
|
||||
|
||||
socket =
|
||||
socket
|
||||
|> put_flash(:info, gettext("%{title} deleted", title: title))
|
||||
|> put_flash(:info, gettext("%{slug} deleted", slug: slug))
|
||||
|> push_navigate(to: Routes.context_index_path(Endpoint, :index))
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
defp page_title(:show, %{title: title}), do: title
|
||||
defp page_title(:edit, %{title: title}), do: gettext("edit %{title}", title: title)
|
||||
defp page_title(:show, %{slug: slug}), do: slug
|
||||
defp page_title(:edit, %{slug: slug}), do: gettext("edit %{slug}", slug: slug)
|
||||
|
||||
@spec is_owner_or_admin?(Context.t(), User.t()) :: boolean()
|
||||
defp is_owner_or_admin?(%{user_id: user_id}, %{id: user_id}), do: true
|
||||
|
@ -1,6 +1,6 @@
|
||||
<div class="mx-auto flex flex-col justify-center items-stretch space-y-4 max-w-3xl">
|
||||
<h1 class="text-xl">
|
||||
<%= @context.title %>
|
||||
<%= @context.slug %>
|
||||
</h1>
|
||||
|
||||
<p><%= if @context.tags, do: @context.tags |> Enum.join(", ") %></p>
|
||||
@ -23,7 +23,10 @@
|
||||
<%= dgettext("actions", "back") %>
|
||||
</.link>
|
||||
<%= if is_owner?(@context, @current_user) do %>
|
||||
<.link class="btn btn-primary" patch={Routes.context_show_path(@socket, :edit, @context)}>
|
||||
<.link
|
||||
class="btn btn-primary"
|
||||
patch={Routes.context_show_path(@socket, :edit, @context.slug)}
|
||||
>
|
||||
<%= dgettext("actions", "edit") %>
|
||||
</.link>
|
||||
<% end %>
|
||||
@ -42,7 +45,7 @@
|
||||
</div>
|
||||
|
||||
<%= if @live_action in [:edit] do %>
|
||||
<.modal return_to={Routes.context_show_path(@socket, :show, @context)}>
|
||||
<.modal return_to={Routes.context_show_path(@socket, :show, @context.slug)}>
|
||||
<.live_component
|
||||
module={MemexWeb.ContextLive.FormComponent}
|
||||
id={@context.id}
|
||||
@ -50,7 +53,7 @@
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
context={@context}
|
||||
return_to={Routes.context_show_path(@socket, :show, @context)}
|
||||
return_to={Routes.context_show_path(@socket, :show, @context.slug)}
|
||||
/>
|
||||
</.modal>
|
||||
<% end %>
|
||||
|
Reference in New Issue
Block a user