Merge pull request 'devel' (#138) from devel into main

Reviewed-on: #138
This commit is contained in:
inhji 2023-06-20 23:11:42 +02:00
commit 97d3f49af3
11 changed files with 104 additions and 75 deletions

View file

@ -16,7 +16,7 @@ defmodule Chiya.Notes.Note do
field :content, :string
field :kind, Ecto.Enum,
values: [:post, :bookmark, :recipe],
values: [:post, :bookmark, :recipe, :like, :reply],
default: :post
field :name, :string
@ -66,6 +66,10 @@ defmodule Chiya.Notes.Note do
end
end
def note_title(note_content) do
String.slice(note_content, 0..25)
end
@doc false
def changeset(note, attrs) do
# if you need to have a preloaded note here,

View file

@ -259,7 +259,7 @@ defmodule ChiyaWeb.CoreComponents do
def simple_form(assigns) do
~H"""
<.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) %>
<div :for={action <- @actions} class="mt-2 flex items-center justify-between gap-6">
<%= 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">
<li>
<.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>
</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>
</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>
<.darkmode_toggle />
</li>

View file

@ -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",

View file

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

@ -173,7 +173,7 @@ defmodule ChiyaWeb.Indie.MicropubHandler do
defp get_note_attrs(p, default_channel) do
content = Props.get_content(p)
name = Props.get_title(p) || String.slice(content, 0..15)
name = Props.get_title(p) || Chiya.Notes.Note.note_title(content)
tags = Props.get_tags(p) |> Enum.join(",")
published_at =

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

@ -4,6 +4,8 @@ defmodule ChiyaWeb.NoteShowLive do
alias Chiya.Notes
alias Chiya.Notes.NoteImage
@accepted_extensions ~w(.jpg .jpeg .gif .png .webp)
@impl true
def render(assigns) do
channels = Enum.map_join(assigns.note.channels, ", ", fn c -> c.name end)
@ -115,7 +117,10 @@ defmodule ChiyaWeb.NoteShowLive do
|> assign(:uploaded_files, [])
|> assign(:image_edit_form, to_form(image_changeset))
|> assign(:image_form, to_form(image_changeset))
|> allow_upload(:note_images, accept: ~w(.jpg .jpeg .gif .png), max_entries: 100)}
|> allow_upload(:note_images,
accept: @accepted_extensions,
max_entries: 100
)}
end
def handle_event("validate_image", _params, socket) do

View file

@ -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]