From df27f03db17bfc11a3f13d4336ae430e34d49d88 Mon Sep 17 00:00:00 2001 From: Inhji Date: Sun, 7 May 2023 10:30:32 +0200 Subject: [PATCH 1/6] add note_list component --- lib/chiya_web/components/public_components.ex | 64 +++++++++++++++++++ .../controllers/page_html/channel.html.heex | 10 +-- .../controllers/page_html/home.html.heex | 51 +-------------- 3 files changed, 66 insertions(+), 59 deletions(-) diff --git a/lib/chiya_web/components/public_components.ex b/lib/chiya_web/components/public_components.ex index 4a364b5..d0d3342 100644 --- a/lib/chiya_web/components/public_components.ex +++ b/lib/chiya_web/components/public_components.ex @@ -6,6 +6,8 @@ defmodule ChiyaWeb.PublicComponents do router: ChiyaWeb.Router, statics: ChiyaWeb.static_paths() + import ChiyaWeb.Format + @doc """ Renders a horizontal line """ @@ -46,4 +48,66 @@ defmodule ChiyaWeb.PublicComponents do """ end + + attr :layout, :atom, default: :list + attr :notes, :list, required: true + + def note_list(assigns) do + case assigns.layout do + :gallery -> + ~H""" + + """ + + _ -> + ~H""" +
+ <%= for note <- assigns.notes do %> + + + <%= note.name %> + + <%= pretty_date(note.published_at) %> + + <% end %> +
+ """ + end + end + + defp gallery_name(note), do: "gallery-#{note.id}" end diff --git a/lib/chiya_web/controllers/page_html/channel.html.heex b/lib/chiya_web/controllers/page_html/channel.html.heex index 668ed3a..48743a0 100644 --- a/lib/chiya_web/controllers/page_html/channel.html.heex +++ b/lib/chiya_web/controllers/page_html/channel.html.heex @@ -7,14 +7,6 @@

- <%= for note <- @channel.notes do %> - - <%= note.name %> - <%= pretty_date(note.published_at) %> - - <% end %> + <.note_list notes={@channel.notes} layout={@channel.layout} />
diff --git a/lib/chiya_web/controllers/page_html/home.html.heex b/lib/chiya_web/controllers/page_html/home.html.heex index 80a7851..b2b39b5 100644 --- a/lib/chiya_web/controllers/page_html/home.html.heex +++ b/lib/chiya_web/controllers/page_html/home.html.heex @@ -23,55 +23,6 @@ <%= if @channel do %> - <%= if @channel.layout == :default do %> -
- <%= for note <- @channel.notes do %> - - - <%= note.name %> - - <%= pretty_date(note.published_at) %> - - <% end %> -
- <% end %> - - <%= if @channel.layout == :gallery do %> - - <% end %> + <.note_list notes={@channel.notes} layout={@channel.layout} /> <% end %> From 0c27f1808e311451b4954a28245ba0d1705c0e9b Mon Sep 17 00:00:00 2001 From: Inhji Date: Sun, 7 May 2023 10:30:58 +0200 Subject: [PATCH 2/6] show channel layout in admin channel show page --- lib/chiya_web/controllers/channel_html/show.html.heex | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/chiya_web/controllers/channel_html/show.html.heex b/lib/chiya_web/controllers/channel_html/show.html.heex index f76f99e..7955474 100644 --- a/lib/chiya_web/controllers/channel_html/show.html.heex +++ b/lib/chiya_web/controllers/channel_html/show.html.heex @@ -16,6 +16,7 @@ <:item title="Content"><%= @channel.content %> <:item title="Visibility"><%= @channel.visibility %> <:item title="Slug"><%= @channel.slug %> + <:item title="Layout"><%= @channel.layout %> <.back navigate={~p"/admin/channels"}>Back to channels From d24ee9e69fa9b2e9d0c313a30136158e25f8468a Mon Sep 17 00:00:00 2001 From: Inhji Date: Sun, 7 May 2023 10:32:21 +0200 Subject: [PATCH 3/6] make default channel configurable --- .../controllers/setting_html/setting_form.html.heex | 6 ++++++ lib/chiya_web/controllers/setting_html/show.html.heex | 7 ++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/chiya_web/controllers/setting_html/setting_form.html.heex b/lib/chiya_web/controllers/setting_html/setting_form.html.heex index 9ddd482..7256399 100644 --- a/lib/chiya_web/controllers/setting_html/setting_form.html.heex +++ b/lib/chiya_web/controllers/setting_html/setting_form.html.heex @@ -15,6 +15,12 @@ <.input field={f[:custom_css]} type="textarea" label="Custom css" class="font-mono" /> <.input field={f[:custom_html]} type="textarea" label="Custom html" class="font-mono" /> <.input field={f[:home_channel_id]} type="select" label="Home Channel" options={@channels} /> + <.input + field={f[:default_channel_id]} + type="select" + label="Default Channel" + options={@channels} + /> <:actions> <.button>Save Setting diff --git a/lib/chiya_web/controllers/setting_html/show.html.heex b/lib/chiya_web/controllers/setting_html/show.html.heex index 0169207..9dcea5a 100644 --- a/lib/chiya_web/controllers/setting_html/show.html.heex +++ b/lib/chiya_web/controllers/setting_html/show.html.heex @@ -31,6 +31,11 @@ <:item title="User agent"><%= @setting.user_agent %> <:item title="Custom css"><%= @setting.custom_css %> <:item title="Custom html"><%= @setting.custom_html %> - <:item title="Home Channel"><%= @setting.home_channel_id %> + <:item title="Home Channel"> + <%= if @setting.home_channel, do: @setting.home_channel.name %> + + <:item title="Default Channel"> + <%= if @setting.default_channel, do: @setting.default_channel.name %> + <% end %> From 13a6eddf1b8df7a6e5598255f9c891c21ba60c4a Mon Sep 17 00:00:00 2001 From: Inhji Date: Sun, 7 May 2023 10:32:26 +0200 Subject: [PATCH 4/6] mix format --- .../controllers/page_html/note.html.heex | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/lib/chiya_web/controllers/page_html/note.html.heex b/lib/chiya_web/controllers/page_html/note.html.heex index 3bf35b2..ab21d5b 100644 --- a/lib/chiya_web/controllers/page_html/note.html.heex +++ b/lib/chiya_web/controllers/page_html/note.html.heex @@ -5,9 +5,9 @@

<%= if @note.published_at do %> - Published + Published <% else %> - Unpublished + Unpublished <% end %> ยท @@ -55,20 +55,20 @@ <.line />

<%= Enum.count(@note.comments) %> Comments

- + <% end %> - + <.line /> <.simple_form :let={f} for={@changeset} action={~p"/#{@note.slug}/comment"}> @@ -76,11 +76,10 @@ Oops, something went wrong! Please check the errors below. <.input field={f[:author_name]} type="text" placeholder="Name" /> - <.input field={f[:content]} type="textarea" placeholder="Content" rows="3" /> + <.input field={f[:content]} type="textarea" placeholder="Content" rows="3" /> <.input field={f[:note_id]} type="hidden" /> <:actions> <.button>Submit Comment - From 18b8a4969599e38b1b10a1c784051c06bc47eb3c Mon Sep 17 00:00:00 2001 From: Inhji Date: Tue, 9 May 2023 16:18:10 +0200 Subject: [PATCH 5/6] list and show comments in admin --- lib/chiya/notes.ex | 11 ++++++++ .../components/layouts/app.html.heex | 6 +++++ .../controllers/comment_controller.ex | 12 +++++++-- .../controllers/comment_html/index.html.heex | 25 +++++++++++++++++++ .../controllers/comment_html/show.html.heex | 20 +++++++++++++++ lib/chiya_web/router.ex | 1 + 6 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 lib/chiya_web/controllers/comment_html/index.html.heex create mode 100644 lib/chiya_web/controllers/comment_html/show.html.heex diff --git a/lib/chiya/notes.ex b/lib/chiya/notes.ex index e9fd096..f066cf1 100644 --- a/lib/chiya/notes.ex +++ b/lib/chiya/notes.ex @@ -278,6 +278,17 @@ defmodule Chiya.Notes do Repo.delete(note_tag) end + def list_note_comments() do + NoteComment + |> order_by(:inserted_at) + |> Repo.all() + |> Repo.preload(:note) + end + + def get_note_comment!(id) do + Repo.get!(NoteComment, id) |> Repo.preload(:note) + end + def create_note_comment(attrs \\ %{}) do %NoteComment{} |> NoteComment.changeset(attrs) diff --git a/lib/chiya_web/components/layouts/app.html.heex b/lib/chiya_web/components/layouts/app.html.heex index 7c67fd8..10b4e61 100644 --- a/lib/chiya_web/components/layouts/app.html.heex +++ b/lib/chiya_web/components/layouts/app.html.heex @@ -1,6 +1,12 @@
+ <.link + href={~p"/admin/comments"} + class="text-xs font-semibold leading-6 text-gray-900 hover:text-gray-700 dark:text-gray-200" + > + <.icon name="hero-document-text" class="w-4 h-4" /> Comments + <.link href={~p"/admin/notes"} class="text-xs font-semibold leading-6 text-gray-900 hover:text-gray-700 dark:text-gray-200" diff --git a/lib/chiya_web/controllers/comment_controller.ex b/lib/chiya_web/controllers/comment_controller.ex index 5559a96..0fbbb8b 100644 --- a/lib/chiya_web/controllers/comment_controller.ex +++ b/lib/chiya_web/controllers/comment_controller.ex @@ -1,11 +1,19 @@ defmodule ChiyaWeb.CommentController do use ChiyaWeb, :controller + def index(conn, _params) do + comments = Chiya.Notes.list_note_comments() + render(conn, comments: comments) + end + + def show(conn, %{"id" => comment_id}) do + comment = Chiya.Notes.get_note_comment!(comment_id) + render(conn, comment: comment) + end + def create(conn, %{"slug" => note_slug, "note_comment" => comment_params}) do note = Chiya.Notes.get_note_by_slug_preloaded!(note_slug) - IO.inspect(comment_params) - case Chiya.Notes.create_note_comment(comment_params) do {:ok, _comment} -> redirect(conn, to: ~p"/#{note_slug}?error=0") diff --git a/lib/chiya_web/controllers/comment_html/index.html.heex b/lib/chiya_web/controllers/comment_html/index.html.heex new file mode 100644 index 0000000..01f9447 --- /dev/null +++ b/lib/chiya_web/controllers/comment_html/index.html.heex @@ -0,0 +1,25 @@ +<.header> + <.icon name="hero-document-text" /> Comments + <:subtitle>Comments are attached to notes + + +<.table id="comments" rows={@comments} row_click={&JS.navigate(~p"/admin/comments/#{&1}")}> + <:col :let={comment} label="Author"><%= comment.author_name %> + <:col :let={comment} label="Inserted at"><%= from_now(comment.inserted_at) %> + <:col :let={comment} label="Approved at"><%= from_now(comment.approved_at) %> + <:action :let={comment}> +
+ <.link navigate={~p"/admin/comments/#{comment}"}>Show +
+ + <:action :let={comment}> + <.link href={~p"/admin/comments/#{comment}"} method="delete" data-confirm="Are you sure?"> + Delete + + + <:action :let={comment}> +
+ <.link navigate={~p"/admin/comments/#{comment}/approve"}>Approve +
+ + diff --git a/lib/chiya_web/controllers/comment_html/show.html.heex b/lib/chiya_web/controllers/comment_html/show.html.heex new file mode 100644 index 0000000..bf98b2b --- /dev/null +++ b/lib/chiya_web/controllers/comment_html/show.html.heex @@ -0,0 +1,20 @@ +<.header> + Comment <%= @comment.id %> + <:subtitle>This is a comment record from your database. + <:actions> + <.link href={~p"/admin/notes/#{@comment}/approve"}> + <.button>Approve note + + + + +<.list> + <:item title="Name"><%= @comment.author_name %> + <:item title="Content"><%= @comment.content %> + <:item title="Inserted at"><%= @comment.inserted_at %> + <:item title="Approved at"><%= @comment.approved_at %> + <:item title="Kind"><%= @comment.kind %> + <:item title="Note"><%= @comment.note.name %> + + +<.back navigate={~p"/admin/comments"}>Back to comments diff --git a/lib/chiya_web/router.ex b/lib/chiya_web/router.ex index 3af5fff..db4038c 100644 --- a/lib/chiya_web/router.ex +++ b/lib/chiya_web/router.ex @@ -60,6 +60,7 @@ defmodule ChiyaWeb.Router do resources "/notes", NoteController, except: [:show] resources "/settings", SettingController, singleton: true resources "/identities", IdentityController + resources "/comments", CommentController, only: [:index, :show] get "/notes/import", NoteController, :import_prepare post "/notes/import", NoteController, :import_run From a4370735eb291766cb4ad5dd107bbe9f6809e322 Mon Sep 17 00:00:00 2001 From: Inhji Date: Tue, 9 May 2023 16:24:46 +0200 Subject: [PATCH 6/6] mix format --- lib/chiya_web/controllers/comment_html/index.html.heex | 2 +- lib/chiya_web/controllers/comment_html/show.html.heex | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/chiya_web/controllers/comment_html/index.html.heex b/lib/chiya_web/controllers/comment_html/index.html.heex index 01f9447..f791e13 100644 --- a/lib/chiya_web/controllers/comment_html/index.html.heex +++ b/lib/chiya_web/controllers/comment_html/index.html.heex @@ -17,7 +17,7 @@ Delete - <:action :let={comment}> + <:action :let={comment}>
<.link navigate={~p"/admin/comments/#{comment}/approve"}>Approve
diff --git a/lib/chiya_web/controllers/comment_html/show.html.heex b/lib/chiya_web/controllers/comment_html/show.html.heex index bf98b2b..962d56e 100644 --- a/lib/chiya_web/controllers/comment_html/show.html.heex +++ b/lib/chiya_web/controllers/comment_html/show.html.heex @@ -9,12 +9,14 @@ <.list> - <:item title="Name"><%= @comment.author_name %> + <:item title="Name"><%= @comment.author_name %> <:item title="Content"><%= @comment.content %> <:item title="Inserted at"><%= @comment.inserted_at %> <:item title="Approved at"><%= @comment.approved_at %> <:item title="Kind"><%= @comment.kind %> - <:item title="Note"><%= @comment.note.name %> + <:item title="Note"> + <%= @comment.note.name %> + <.back navigate={~p"/admin/comments"}>Back to comments