diff --git a/app/assets/stylesheets/components/activities.css b/app/assets/stylesheets/components/activities.css index 45831cd..a75753c 100644 --- a/app/assets/stylesheets/components/activities.css +++ b/app/assets/stylesheets/components/activities.css @@ -56,6 +56,10 @@ color: var(--color-text-secondary); } +.activity-actions { + margin-top: var(--spacing-2); +} + .activity-tags { display: flex; flex-wrap: wrap; diff --git a/app/assets/stylesheets/components/notes.css b/app/assets/stylesheets/components/notes.css index bd249db..a9c1e1f 100644 --- a/app/assets/stylesheets/components/notes.css +++ b/app/assets/stylesheets/components/notes.css @@ -121,6 +121,28 @@ justify-content: flex-end; } +.note-actions-row { + display: flex; + align-items: center; + gap: var(--spacing-3); + margin-top: var(--spacing-2); +} + +.note-delete-button { + padding: 0; + background: none; + border: none; + color: var(--color-text-link); + font-size: var(--font-size-sm); + font-weight: var(--font-weight-medium); + cursor: pointer; + text-decoration: none; + + &:hover { + text-decoration: underline; + } +} + .notes-inline-add { margin: var(--spacing-3) 0; } diff --git a/app/controllers/notes_controller.rb b/app/controllers/notes_controller.rb index 7933fb5..7f79833 100644 --- a/app/controllers/notes_controller.rb +++ b/app/controllers/notes_controller.rb @@ -2,7 +2,7 @@ class NotesController < ApplicationController before_action :require_authentication - before_action :set_note, only: [:update] + before_action :set_note, only: [:update, :destroy] def create topic = Topic.find(note_params[:topic_id]) @@ -39,12 +39,29 @@ def update redirect_back fallback_location: topic_path(@note.topic) end + def destroy + return if performed? + + @note.transaction do + @note.update!(deleted_at: Time.current) + @note.note_mentions.delete_all + @note.note_tags.delete_all + @note.activities.update_all(hidden: true) + end + + redirect_back fallback_location: topic_path(@note.topic), notice: "Note deleted" + end + private def set_note @note = Note.find(params[:id]) + if @note.deleted_at? + redirect_back fallback_location: topic_path(@note.topic), alert: "Note has been deleted" + return + end unless @note.author_id == current_user.id - redirect_back fallback_location: topic_path(@note.topic), alert: "You can only edit your own notes" + redirect_back fallback_location: topic_path(@note.topic), alert: "You can edit or delete your own notes" return end end diff --git a/app/views/activities/index.html.slim b/app/views/activities/index.html.slim index 521eebf..3a1f61e 100644 --- a/app/views/activities/index.html.slim +++ b/app/views/activities/index.html.slim @@ -29,6 +29,9 @@ .activity-tags - note.note_tags.each do |tag| span.note-tag ##{tag.tag} + - if current_user.id == note.author_id + .activity-actions + = button_to "Delete note", note_path(note), method: :delete, class: "note-delete-button", form: { data: { turbo_confirm: "Delete this note?" } } - else .activity-body span.activity-missing Content not available anymore. diff --git a/app/views/notes/_note.html.slim b/app/views/notes/_note.html.slim index 4b9d21c..e068dfb 100644 --- a/app/views/notes/_note.html.slim +++ b/app/views/notes/_note.html.slim @@ -19,6 +19,8 @@ span.note-mention = note_mention_label(mention) - if current_user&.id == note.author_id - details.note-edit-toggle - summary Edit - = render "notes/form", note: note, topic: note.topic, message: note.message, submit_text: "Update note" + .note-actions-row + details.note-edit-toggle + summary Edit + = render "notes/form", note: note, topic: note.topic, message: note.message, submit_text: "Update note" + = button_to "Delete", note_path(note), method: :delete, class: "note-delete-button", form: { data: { turbo_confirm: "Delete this note?" } } diff --git a/config/routes.rb b/config/routes.rb index 6d4a7ad..e8ca12f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -35,7 +35,7 @@ resources :activities, only: [:index] do post :mark_all_read, on: :collection end - resources :notes, only: [:create, :update] + resources :notes, only: [:create, :update, :destroy] # Authentication resource :session, only: [:new, :create, :destroy]