replace admin_controller with live view, add quick post form

This commit is contained in:
Inhji 2023-06-20 23:10:40 +02:00
parent 98a119a95b
commit f92333716a
8 changed files with 92 additions and 72 deletions

View file

@ -259,7 +259,7 @@ defmodule ChiyaWeb.CoreComponents do
def simple_form(assigns) do def simple_form(assigns) do
~H""" ~H"""
<.form :let={f} for={@for} as={@as} {@rest} class={@class}> <.form :let={f} for={@for} as={@as} {@rest} class={@class}>
<div class="space-y-8 p-3"> <div class="space-y-8">
<%= render_slot(@inner_block, f) %> <%= render_slot(@inner_block, f) %>
<div :for={action <- @actions} class="mt-2 flex items-center justify-between gap-6"> <div :for={action <- @actions} class="mt-2 flex items-center justify-between gap-6">
<%= render_slot(action, f) %> <%= render_slot(action, f) %>
@ -723,41 +723,21 @@ defmodule ChiyaWeb.CoreComponents do
<ul class="sticky top-0 backdrop-blur-sm z-10 flex items-center gap-4 py-3 px-4 sm:px-6 lg:px-8 bg-white/30 dark:bg-black/30"> <ul class="sticky top-0 backdrop-blur-sm z-10 flex items-center gap-4 py-3 px-4 sm:px-6 lg:px-8 bg-white/30 dark:bg-black/30">
<li> <li>
<.link <.link
href={~p"/"} href={~p"/admin"}
class="flex gap-3 text-sm leading-6 font-semibold text-gray-900 dark:text-gray-100 dark:hover:text-gray-300 hover:text-gray-700" class="flex gap-3 text-sm leading-6 font-semibold text-gray-900 dark:text-gray-100 dark:hover:text-gray-300 hover:text-gray-700"
> >
<%= @settings.title %> Dashboard
</.link>
</li>
<li>
<.link
href={~p"/"}
class="text-sm leading-6 text-gray-900 dark:text-gray-100 dark:hover:text-gray-300 hover:text-gray-700"
>
<.icon name="hero-arrow-top-right-on-square" class="w-4 h-4 align-sub" /> Public Site
</.link> </.link>
</li> </li>
<li class="flex-1"></li> <li class="flex-1"></li>
<%= if @current_user do %>
<li>
<.link
href={~p"/admin"}
class="text-sm leading-6 text-gray-900 dark:text-gray-100 dark:hover:text-gray-300 hover:text-gray-700"
>
Admin
</.link>
</li>
<li>
<.link
href={~p"/user/log_out"}
method="delete"
class="text-sm leading-6 text-gray-900 dark:text-gray-100 dark:hover:text-gray-300 hover:text-gray-700"
>
Log out
</.link>
</li>
<% else %>
<li>
<.link
href={~p"/user/log_in"}
class="text-xs leading-6 text-gray-900 dark:text-gray-100 font-semibold dark:hover:text-gray-300 hover:text-gray-700"
>
Log in
</.link>
</li>
<% end %>
<li> <li>
<.darkmode_toggle /> <.darkmode_toggle />
</li> </li>

View file

@ -6,11 +6,6 @@
icon: "hero-document-text-solid", icon: "hero-document-text-solid",
name: "Notes" name: "Notes"
}, },
%{
path: ~p"/admin/comments",
icon: "hero-chat-bubble-oval-left-ellipsis-solid",
name: "Comments"
},
%{ %{
path: ~p"/admin/channels", path: ~p"/admin/channels",
icon: "hero-speaker-wave-solid", icon: "hero-speaker-wave-solid",

View file

@ -48,6 +48,13 @@
<.icon name="hero-hand-raised" /> About <.icon name="hero-hand-raised" /> About
</a> </a>
</li> </li>
<%= if @current_user do %>
<li>
<a href="/admin">
<.icon name="hero-beaker" /> Admin
</a>
</li>
<% end %>
<li class="flex-1"></li> <li class="flex-1"></li>
<li> <li>
<.darkmode_toggle /> <.darkmode_toggle />
@ -76,7 +83,6 @@
<ul class="list-disc list-inside"> <ul class="list-disc list-inside">
<li><a href="#"><del>Wiki</del></a></li> <li><a href="#"><del>Wiki</del></a></li>
<li><a href={~p"/admin"}>Admin</a></li>
</ul> </ul>
</div> </div>

View file

@ -1,7 +0,0 @@
defmodule ChiyaWeb.AdminController do
use ChiyaWeb, :controller
def home(conn, _params) do
render(conn, :home)
end
end

View file

@ -1,5 +0,0 @@
defmodule ChiyaWeb.AdminHTML do
use ChiyaWeb, :html
embed_templates "admin_html/*"
end

View file

@ -1,22 +0,0 @@
<section class="p-4 rounded flex gap-4 bg-gray-100 shadow border border-gray-300 dark:border-gray-700 dark:bg-gray-800">
<img
class="rounded-full w-24"
src={ChiyaWeb.Uploaders.UserImage.url({@current_user.user_image, @current_user}, :thumb)}
/>
<div class="flex-1 dark:text-gray-200">
<p class="text-sm text-gray-600 dark:text-gray-400">Welcome back,</p>
<h2 class="text-xl font-semibold"><%= @current_user.email %></h2>
</div>
<.link href={~p"/user"}>
<.button>Profile</.button>
</.link>
</section>
<section class="mt-4 flex gap-4">
<a
href="/admin/notes/new"
class="p-4 rounded text-gray-700 bg-gray-100 shadow border border-gray-300 dark:border-gray-700 dark:bg-gray-800 dark:text-gray-200 hover:text-gray-700/80 dark:hover:text-gray-200/80 transition hover:shadow-none"
>
<.icon name="hero-document-plus" /> New Note
</a>
</section>

View file

@ -0,0 +1,73 @@
defmodule ChiyaWeb.AdminHomeLive do
use ChiyaWeb, :live_view
@impl true
def mount(_params, _session, socket) do
changeset = Chiya.Notes.change_note(%Chiya.Notes.Note{})
{:ok, socket |> assign(:form, to_form(changeset))}
end
def handle_event("validate", %{"note" => params}, socket) do
form =
%Chiya.Notes.Note{}
|> Chiya.Notes.change_note(params)
|> Map.put(:action, :insert)
|> to_form()
{:noreply, assign(socket, form: form)}
end
def handle_event("save", %{"note" => params}, socket) do
name = Chiya.Notes.Note.note_title(params["content"])
settings = Chiya.Site.get_settings()
params =
params
|> Map.put_new("name", name)
|> Map.put_new("channels", [settings.home_channel])
|> Map.put_new("published_at", NaiveDateTime.local_now())
case Chiya.Notes.create_note(params) do
{:ok, note} ->
{:noreply, socket |> put_flash(:info, "Note created!")}
{:error, %Ecto.Changeset{} = changeset} ->
IO.inspect(changeset)
{:noreply,
socket |> put_flash(:error, "Could not create note!") |> assign(form: to_form(changeset))}
end
end
@impl true
def render(assigns) do
~H"""
<.header>
<.icon name="hero-document-text" /> Welcome back!
<:subtitle>This is the admin area</:subtitle>
<:actions>
<.link href={~p"/user"}>
<.button>Profile</.button>
</.link>
<.link
href={~p"/user/log_out"}
method="delete"
data-confirm="Do you want to logout?"
class="text-sm leading-6 text-gray-900 dark:text-gray-100 dark:hover:text-gray-300 hover:text-gray-700"
>
<.button>Log out</.button>
</.link>
</:actions>
</.header>
<section>
<.simple_form for={@form} phx-change="validate" phx-submit="save">
<.input field={@form[:content]} type="textarea" />
<:actions>
<.button>Save</.button>
</:actions>
</.simple_form>
</section>
"""
end
end

View file

@ -66,7 +66,7 @@ defmodule ChiyaWeb.Router do
scope "/admin", ChiyaWeb do scope "/admin", ChiyaWeb do
pipe_through [:browser, :require_authenticated_user] pipe_through [:browser, :require_authenticated_user]
get "/", AdminController, :home live "/", AdminHomeLive, :index
resources "/channels", ChannelController resources "/channels", ChannelController
resources "/notes", NoteController, except: [:show] resources "/notes", NoteController, except: [:show]