From 9dc78255cf0bff10a3089f5261fe3513b5a4306e Mon Sep 17 00:00:00 2001 From: Daniel Nitsikopoulos Date: Sat, 16 Sep 2023 13:25:56 +1000 Subject: [PATCH] Allow editing of posts --- slices/admin/actions/posts/update.rb | 14 ++++++++++++++ slices/admin/commands/posts/update.rb | 19 +++++++++++++++++++ slices/admin/config/routes.rb | 1 + slices/admin/repos/post_repo.rb | 2 ++ slices/admin/templates/posts/show.html.slim | 12 ++++++++---- 5 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 slices/admin/actions/posts/update.rb create mode 100644 slices/admin/commands/posts/update.rb diff --git a/slices/admin/actions/posts/update.rb b/slices/admin/actions/posts/update.rb new file mode 100644 index 0000000..042b4a7 --- /dev/null +++ b/slices/admin/actions/posts/update.rb @@ -0,0 +1,14 @@ +module Admin + module Actions + module Posts + class Update < Action + include Deps["commands.posts.update"] + + def handle(req, res) + update.(params: req.params) + res.redirect "/admin/posts/#{req.params[:id]}" + end + end + end + end +end diff --git a/slices/admin/commands/posts/update.rb b/slices/admin/commands/posts/update.rb new file mode 100644 index 0000000..e6b0dcf --- /dev/null +++ b/slices/admin/commands/posts/update.rb @@ -0,0 +1,19 @@ +module Admin + module Commands + module Posts + class Update + include Deps[ + "repos.post_repo", + "renderers.markdown" + ] + + def call(params:) + attrs_to_replace = {} + attrs_to_replace[:content] = markdown.call(content: params[:body]) if params[:body] + + post_repo.update(params[:id], attrs_to_replace) + end + end + end + end +end diff --git a/slices/admin/config/routes.rb b/slices/admin/config/routes.rb index 9ec6391..daea836 100644 --- a/slices/admin/config/routes.rb +++ b/slices/admin/config/routes.rb @@ -36,6 +36,7 @@ module Admin post "/posts/:id/publish", to: Auth.call(action: "posts.publish") get "/posts/:id", to: Auth.call(action: "posts.show") post "/posts/:id/syndicate/:target", to: Auth.call(action: "posts.syndicate") + post "/post/:id/update", to: Auth.call(action: "posts.update") get "/media", to: Auth.call(action: "photos.index") delete "/media/public/media/:year/:path", to: Auth.call(action: "photos.delete") diff --git a/slices/admin/repos/post_repo.rb b/slices/admin/repos/post_repo.rb index 7de312b..8a45a01 100644 --- a/slices/admin/repos/post_repo.rb +++ b/slices/admin/repos/post_repo.rb @@ -3,6 +3,8 @@ require "time_math" module Admin module Repos class PostRepo < Adamantium::Repo[:posts] + commands update: :by_pk + def tag_post(post_id:, tag_id:) return if posts .post_tags diff --git a/slices/admin/templates/posts/show.html.slim b/slices/admin/templates/posts/show.html.slim index b995634..e54b571 100644 --- a/slices/admin/templates/posts/show.html.slim +++ b/slices/admin/templates/posts/show.html.slim @@ -8,8 +8,12 @@ div class="mb-12 prose dark:prose-invert max-w-prose mx-auto text-gray-800 dark: = name button hx-post="/admin/posts/#{post.id}/syndicate/day_one" Send to Day One -// TODO: Add preview, save post, fix sending to DayOne +// TODO: Add preview, fix sending to DayOne article class="mb-12 prose dark:prose-invert max-w-prose mx-auto text-gray-800 dark:text-gray-200 prose-a:text-blue-600 prose-a:no-underline hover:prose-a:underline prose-img:rounded" - h1= post.name - textarea class="text-gray-800 w-full border-blue-200 border-2 rounded p-2" x-data="{ resize: () => { $el.style.height = '5px'; $el.style.height = $el.scrollHeight + 'px' } }" x-init="resize()" @input="resize()" - == markdown_body \ No newline at end of file + a href="/post/#{post.slug}" + h1= post.name + form action="/admin/post/#{post.id}/update" method="POST" + textarea name="body" class="text-gray-800 w-full border-blue-200 border-2 rounded p-2" x-data="{ resize: () => { $el.style.height = '5px'; $el.style.height = $el.scrollHeight + 'px' } }" x-init="resize()" @input="resize()" + == markdown_body + button class="rounded bg-blue-100 hover:bg-blue-200 text-blue-600 px-2 hover:cursor-pointer" type="submit" + = "Update"