diff --git a/app/javascript/controllers/chatroom_subscription_controller.js b/app/javascript/controllers/chatroom_subscription_controller.js index 702a124..c85b340 100644 --- a/app/javascript/controllers/chatroom_subscription_controller.js +++ b/app/javascript/controllers/chatroom_subscription_controller.js @@ -3,6 +3,7 @@ import { createConsumer } from "@rails/actioncable" export default class extends Controller { static values = { chatroomId: Number } + static targets = ["messages", "form"] connect() { this.channel = createConsumer().subscriptions.create( @@ -15,4 +16,19 @@ export default class extends Controller { this.element.insertAdjacentHTML("beforeend", data) this.element.scrollTo(0, this.element.scrollHeight) } + + + + submitForm(e) { + e.preventDefault(); + // console.log(e.currentTarget); + try { + console.log("triggering form submit event"); + this.formTarget.submit(); + this.messagesTarget.scrollTo(0, this.messagesTarget.scrollHeight) + } catch (error) { + console.log(error.message); + } + + } } diff --git a/app/views/chatrooms/show.html.erb b/app/views/chatrooms/show.html.erb index e260b06..d1cb1c7 100644 --- a/app/views/chatrooms/show.html.erb +++ b/app/views/chatrooms/show.html.erb @@ -71,24 +71,24 @@ <% end %> -
+
- <% @chatroom.messages.each do |message| %> - <%= render "messages/message", message: message %> - <% end %> +
+ <% @chatroom.messages.each do |message| %> + <%= render "messages/message", message: message %> + <% end %> +
-
- <%= simple_form_for([@chatroom, @message]) do |f| %> -
- <%= f.input :content, label: "New Message", input_html: { class: "form-control", style: "width: 100%; height: 370px;" } %> -
- <%= f.submit "Send", class: "btn btn-flat" %> -<% end %> - +
+ <%= simple_form_for([@chatroom, @message], html: { data: { chatroom_subscription_target: "form" } }) do |f| %> +
+ <%= f.input :content, label: "New Message (hold 'shift' for new line)", input_html: { data: { action: "keydown.enter->chatroom-subscription#submitForm"}, class: "form-control", style: "width: 100%; height: 370px;" } %> +
+ <%= f.submit "Send", class: "btn btn-flat" %> + <% end %>