@@ -4,39 +4,48 @@ class NotesController < ApplicationController
44 before_action :require_authentication
55 before_action :set_note , only : [ :update ]
66
7+ def new
8+ topic = Topic . find ( params [ :topic_id ] )
9+ message = resolve_message ( topic )
10+ respond_to do |format |
11+ format . turbo_stream { render_note_stack_stream ( topic :, message :, open_form : true ) }
12+ format . html { redirect_to topic_path ( topic , anchor : message ? note_frame_id ( message ) : "thread-notes" ) }
13+ end
14+ end
15+
16+ def stack
17+ topic = Topic . find ( params [ :topic_id ] )
18+ message = resolve_message ( topic )
19+ respond_to do |format |
20+ format . turbo_stream { render_note_stack_stream ( topic :, message :, open_form : false ) }
21+ format . html { redirect_to topic_path ( topic , anchor : message ? note_frame_id ( message ) : "thread-notes" ) }
22+ end
23+ end
24+
725 def create
826 topic = Topic . find ( note_params [ :topic_id ] )
927 message = resolve_message ( topic )
1028 note = NoteBuilder . new ( author : current_user ) . create! ( topic :, message :, body : note_params [ :body ] )
1129
12- redirect_to topic_path ( topic , anchor : note_anchor ( note ) ) , notice : "Note added"
30+ respond_to do |format |
31+ format . html { redirect_to topic_path ( topic , anchor : note_anchor ( note ) ) , notice : "Note added" }
32+ format . turbo_stream { render_note_stack_stream ( topic :, message :, open_form : false ) }
33+ end
1334 rescue NoteBuilder ::Error , ActiveRecord ::RecordInvalid => e
14- flash [ :alert ] = e . message
15- flash [ :note_error ] = {
16- body : note_params [ :body ] ,
17- message_id : note_params [ :message_id ] . presence ,
18- topic_id : note_params [ :topic_id ] ,
19- error : e . message
20- }
21- redirect_back fallback_location : topic_path ( topic )
35+ handle_note_error ( topic :, message :, error : e )
2236 end
2337
2438 def update
2539 return if performed?
2640
2741 NoteBuilder . new ( author : current_user ) . update! ( note : @note , body : note_params [ :body ] )
2842
29- redirect_to topic_path ( @note . topic , anchor : note_anchor ( @note ) ) , notice : "Note updated"
43+ respond_to do |format |
44+ format . html { redirect_to topic_path ( @note . topic , anchor : note_anchor ( @note ) ) , notice : "Note updated" }
45+ format . turbo_stream { render_note_stack_stream ( topic : @note . topic , message : @note . message , open_form : false ) }
46+ end
3047 rescue NoteBuilder ::Error , ActiveRecord ::RecordInvalid => e
31- flash [ :alert ] = e . message
32- flash [ :note_error ] = {
33- body : note_params [ :body ] ,
34- message_id : note_params [ :message_id ] . presence ,
35- topic_id : note_params [ :topic_id ] ,
36- note_id : @note . id ,
37- error : e . message
38- }
39- redirect_back fallback_location : topic_path ( @note . topic )
48+ handle_note_error ( topic : @note . topic , message : @note . message , error : e , note : @note )
4049 end
4150
4251 private
@@ -54,8 +63,9 @@ def note_params
5463 end
5564
5665 def resolve_message ( topic )
57- return nil if note_params [ :message_id ] . blank?
58- topic . messages . find ( note_params [ :message_id ] )
66+ msg_id = params [ :message_id ] . presence || params . dig ( :note , :message_id )
67+ return nil if msg_id . blank?
68+ topic . messages . find_by ( id : msg_id )
5969 end
6070
6171 def note_anchor ( note )
@@ -65,4 +75,47 @@ def note_anchor(note)
6575 "thread-notes"
6676 end
6777 end
78+
79+ def note_frame_id ( message )
80+ message ? "notes-message-#{ message . id } " : "thread-notes"
81+ end
82+
83+ def note_collection ( topic :, message :)
84+ Note . where ( topic :, message :) . includes ( :author , :note_tags , :note_mentions ) . order ( :created_at )
85+ end
86+
87+ def render_note_stack_stream ( topic :, message :, open_form :, status : :ok )
88+ notes = note_collection ( topic :, message :)
89+ render (
90+ turbo_stream : turbo_stream . replace (
91+ note_frame_id ( message ) ,
92+ partial : "notes/note_stack" ,
93+ locals : { topic :, message :, notes :, open_form : }
94+ ) ,
95+ status : status
96+ )
97+ end
98+
99+ def handle_note_error ( topic :, message :, error :, note : nil )
100+ flash_payload = {
101+ body : note_params [ :body ] ,
102+ message_id : note_params [ :message_id ] . presence ,
103+ topic_id : note_params [ :topic_id ] ,
104+ note_id : note &.id ,
105+ error : error . message
106+ }
107+
108+ respond_to do |format |
109+ format . html do
110+ flash [ :alert ] = error . message
111+ flash [ :note_error ] = flash_payload
112+ redirect_back fallback_location : topic_path ( topic )
113+ end
114+ format . turbo_stream do
115+ flash . now [ :alert ] = error . message
116+ flash . now [ :note_error ] = flash_payload
117+ render_note_stack_stream ( topic :, message :, open_form : true , status : :unprocessable_entity )
118+ end
119+ end
120+ end
68121end
0 commit comments