154 lines
3.7 KiB
Elixir
154 lines
3.7 KiB
Elixir
defmodule Web.AdminPostLive do
|
|
use Web, :live_view
|
|
|
|
def mount(_params, _session, socket) do
|
|
{:ok, socket}
|
|
end
|
|
|
|
def handle_params(%{"post_id" => post_id}, _uri, socket) do
|
|
post = Core.Posts.get!(post_id)
|
|
form = Core.Posts.change_post(post, %{}) |> to_form()
|
|
|
|
socket =
|
|
assign(socket,
|
|
post: post,
|
|
form: form,
|
|
page_title: page_title(post, socket.assigns.live_action)
|
|
)
|
|
|
|
{:noreply, socket}
|
|
end
|
|
|
|
def handle_params(%{"kind" => kind}, _uri, %{assigns: %{live_action: :new}} = socket)
|
|
when kind in ~w[blog status] do
|
|
post = %Schema.Post{kind: String.to_existing_atom(kind)}
|
|
form = Core.Posts.change_post(post, %{}) |> to_form()
|
|
|
|
socket =
|
|
assign(socket,
|
|
post: post,
|
|
form: form,
|
|
page_title: page_title(post, socket.assigns.live_action)
|
|
)
|
|
|
|
{:noreply, socket}
|
|
end
|
|
|
|
def handle_event("validate", %{"post" => attrs}, %{assigns: %{post: post}} = socket) do
|
|
attrs =
|
|
if update_slug?(post) and attrs["_unused_slug"] == "" do
|
|
Map.put(attrs, "slug", Slug.slugify(attrs["title"]))
|
|
else
|
|
attrs
|
|
end
|
|
|
|
form = Core.Posts.change_post(post, attrs) |> to_form()
|
|
|
|
socket = assign(socket, form: form)
|
|
|
|
{:noreply, socket}
|
|
end
|
|
|
|
def handle_event("save", %{"post" => attrs}, %{assigns: %{post: post}} = socket) do
|
|
socket =
|
|
case Core.Posts.create_or_update_post(post, attrs) do
|
|
{:ok, post} ->
|
|
form =
|
|
post
|
|
|> Core.Posts.change_post(%{})
|
|
|> to_form()
|
|
|
|
socket
|
|
|> assign(post: post, form: form)
|
|
|> put_flash(:info, "post saved")
|
|
|> push_patch(to: ~p"/admin/posts/#{post}", replace: true)
|
|
|
|
{:error, changest} ->
|
|
assign(socket, form: to_form(changest))
|
|
end
|
|
|
|
{:noreply, socket}
|
|
end
|
|
|
|
def handle_event("publish", _, %{assigns: %{post: post}} = socket) do
|
|
socket =
|
|
case Core.Posts.publish_post(post) do
|
|
{:ok, post} ->
|
|
form =
|
|
post
|
|
|> Core.Posts.change_post(%{})
|
|
|> to_form()
|
|
|
|
assign(socket, post: post, form: form)
|
|
|
|
_ ->
|
|
socket
|
|
end
|
|
|
|
{:noreply, socket}
|
|
end
|
|
|
|
def handle_event("unpublish", _, %{assigns: %{post: post}} = socket) do
|
|
socket =
|
|
case Core.Posts.unpublish_post(post) do
|
|
{:ok, post} ->
|
|
form =
|
|
post
|
|
|> Core.Posts.change_post(%{})
|
|
|> to_form()
|
|
|
|
assign(socket, post: post, form: form)
|
|
|
|
_ ->
|
|
socket
|
|
end
|
|
|
|
{:noreply, socket}
|
|
end
|
|
|
|
def handle_event("delete", _, %{assigns: %{post: post}} = socket) do
|
|
socket =
|
|
case Core.Posts.delete_post(post) do
|
|
{:ok, post} ->
|
|
form =
|
|
post
|
|
|> Core.Posts.change_post(%{})
|
|
|> to_form()
|
|
|
|
assign(socket, post: post, form: form)
|
|
|
|
_ ->
|
|
socket
|
|
end
|
|
|
|
{:noreply, socket}
|
|
end
|
|
|
|
def handle_event("undelete", _, %{assigns: %{post: post}} = socket) do
|
|
socket =
|
|
case Core.Posts.undelete_post(post) do
|
|
{:ok, post} ->
|
|
form =
|
|
post
|
|
|> Core.Posts.change_post(%{})
|
|
|> to_form()
|
|
|
|
assign(socket, post: post, form: form)
|
|
|
|
_ ->
|
|
socket
|
|
end
|
|
|
|
{:noreply, socket}
|
|
end
|
|
|
|
defp page_title(%Schema.Post{kind: :blog}, :new), do: "new blog"
|
|
defp page_title(%Schema.Post{kind: :status}, :new), do: "new status"
|
|
defp page_title(%Schema.Post{kind: :blog}, :edit), do: "edit blog"
|
|
defp page_title(%Schema.Post{kind: :status}, :edit), do: "edit status"
|
|
|
|
defp update_slug?(%Schema.Post{kind: :blog, published_at: published_at}),
|
|
do: is_nil(published_at)
|
|
|
|
defp update_slug?(_), do: true
|
|
end
|