diff --git a/lib/chiya_web/components/core_components.ex b/lib/chiya_web/components/core_components.ex index 8941021..aeef9f9 100644 --- a/lib/chiya_web/components/core_components.ex +++ b/lib/chiya_web/components/core_components.ex @@ -259,7 +259,7 @@ defmodule ChiyaWeb.CoreComponents do def simple_form(assigns) do ~H""" <.form :let={f} for={@for} as={@as} {@rest} class={@class}> -
+
<%= render_slot(@inner_block, f) %>
<%= render_slot(action, f) %> @@ -723,41 +723,21 @@ defmodule ChiyaWeb.CoreComponents do
  • <.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" > - <%= @settings.title %> + Dashboard + +
  • +
  • + <.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
  • - <%= if @current_user do %> -
  • - <.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 - 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 - -
  • - <% else %> -
  • - <.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 - -
  • - <% end %>
  • <.darkmode_toggle />
  • diff --git a/lib/chiya_web/components/layouts/app.html.heex b/lib/chiya_web/components/layouts/app.html.heex index 352e7c6..b50a571 100644 --- a/lib/chiya_web/components/layouts/app.html.heex +++ b/lib/chiya_web/components/layouts/app.html.heex @@ -6,11 +6,6 @@ icon: "hero-document-text-solid", name: "Notes" }, - %{ - path: ~p"/admin/comments", - icon: "hero-chat-bubble-oval-left-ellipsis-solid", - name: "Comments" - }, %{ path: ~p"/admin/channels", icon: "hero-speaker-wave-solid", diff --git a/lib/chiya_web/components/layouts/root_public.html.heex b/lib/chiya_web/components/layouts/root_public.html.heex index da9361e..863d551 100644 --- a/lib/chiya_web/components/layouts/root_public.html.heex +++ b/lib/chiya_web/components/layouts/root_public.html.heex @@ -48,6 +48,13 @@ <.icon name="hero-hand-raised" /> About + <%= if @current_user do %> +
  • + + <.icon name="hero-beaker" /> Admin + +
  • + <% end %>
  • <.darkmode_toggle /> @@ -76,7 +83,6 @@
diff --git a/lib/chiya_web/controllers/admin_controller.ex b/lib/chiya_web/controllers/admin_controller.ex deleted file mode 100644 index 2483655..0000000 --- a/lib/chiya_web/controllers/admin_controller.ex +++ /dev/null @@ -1,7 +0,0 @@ -defmodule ChiyaWeb.AdminController do - use ChiyaWeb, :controller - - def home(conn, _params) do - render(conn, :home) - end -end diff --git a/lib/chiya_web/controllers/admin_html.ex b/lib/chiya_web/controllers/admin_html.ex deleted file mode 100644 index 1f80de2..0000000 --- a/lib/chiya_web/controllers/admin_html.ex +++ /dev/null @@ -1,5 +0,0 @@ -defmodule ChiyaWeb.AdminHTML do - use ChiyaWeb, :html - - embed_templates "admin_html/*" -end diff --git a/lib/chiya_web/controllers/admin_html/home.html.heex b/lib/chiya_web/controllers/admin_html/home.html.heex deleted file mode 100644 index 50d7163..0000000 --- a/lib/chiya_web/controllers/admin_html/home.html.heex +++ /dev/null @@ -1,22 +0,0 @@ -
- -
-

Welcome back,

-

<%= @current_user.email %>

-
- <.link href={~p"/user"}> - <.button>Profile - -
- -
- - <.icon name="hero-document-plus" /> New Note - -
diff --git a/lib/chiya_web/live/admin_home_live.ex b/lib/chiya_web/live/admin_home_live.ex new file mode 100644 index 0000000..86e0b6c --- /dev/null +++ b/lib/chiya_web/live/admin_home_live.ex @@ -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 + <:actions> + <.link href={~p"/user"}> + <.button>Profile + + <.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 + + + + +
+ <.simple_form for={@form} phx-change="validate" phx-submit="save"> + <.input field={@form[:content]} type="textarea" /> + <:actions> + <.button>Save + + +
+ """ + end +end diff --git a/lib/chiya_web/router.ex b/lib/chiya_web/router.ex index e0fde0e..025dc3f 100644 --- a/lib/chiya_web/router.ex +++ b/lib/chiya_web/router.ex @@ -66,7 +66,7 @@ defmodule ChiyaWeb.Router do scope "/admin", ChiyaWeb do pipe_through [:browser, :require_authenticated_user] - get "/", AdminController, :home + live "/", AdminHomeLive, :index resources "/channels", ChannelController resources "/notes", NoteController, except: [:show]