Skip to content

Commit

Permalink
Cleanup from pr review
Browse files Browse the repository at this point in the history
  • Loading branch information
mattlindsey committed Dec 10, 2024
1 parent 8d89c79 commit 7b92818
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 74 deletions.
12 changes: 2 additions & 10 deletions app/controllers/conversations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class ConversationsController < ApplicationController
before_action :set_conversation, only: [:show, :edit, :update, :destroy]
before_action :set_query, only: [:index]
before_action :set_nav_conversations
before_action :set_nav_assistants

def index
@query = params[:query]
@nav_conversations = Conversation.grouped_by_increasing_time_interval_for_user(Current.user, @query)
end

def show
Expand Down Expand Up @@ -32,18 +32,10 @@ def destroy

private

def set_nav_conversations
@nav_conversations = Conversation.grouped_by_increasing_time_interval_for_user(Current.user, @query)
end

def set_nav_assistants
@nav_assistants = Current.user.assistants.ordered
end

def set_query
@query = params[:query]
end

def set_conversation
@conversation = Current.user.conversations.find(params[:id])
end
Expand Down
13 changes: 6 additions & 7 deletions app/javascript/stimulus/search_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@ export default class extends Controller {
static targets = [ "input", "clear" ]

connect() {
this.cursorToEnd()
this.setSearchClearIcon()
}

cursorToEnd() {
this.inputTarget.selectionStart =
this.inputTarget.selectionEnd =
this.inputTarget.value.length
disconnect() {
clearTimeout(this.timeout)
}

clear() {
this.inputTarget.value = ""
this.element.requestSubmit()
}

unfocus() {
this.inputTarget.autofocus = false
}

search() {
clearTimeout(this.timeout)
this.timeout = setTimeout(() => {
Expand All @@ -29,10 +30,8 @@ export default class extends Controller {
setSearchClearIcon() {
if (this.inputTarget.value.length > 0) {
this.clearTarget.classList.remove("hidden")
this.clearTarget.classList.add("text-gray-800")
} else {
this.clearTarget.classList.add("hidden")
this.clearTarget.classList.remove("text-gray-800")
}
}
}
53 changes: 0 additions & 53 deletions app/views/conversations/_nav_conversations.html.erb

This file was deleted.

59 changes: 58 additions & 1 deletion app/views/conversations/index.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,58 @@
<%= render "nav_conversations" %>
<turbo-frame data-turbo-permanent id="nav-conversations">
<section
id="search"
class="pt-6 text-gray-950 dark:text-gray-100 select-none"
>
<%= form_with(url: conversations_path,
data: { turbo_frame: "nav_conversations", controller: "search" },
class: "contents",
method: :get
) do |form| %>
<div class="relative ml-2 mr-5">
<%= form.text_field :query,
value: @query,
placeholder: "Search",
id: "search-input",
autofocus: @query.nil? ? false : true,
class: %|
w-full
p-2 py-1
border border-gray-200 rounded-lg
text-black dark:text-gray-800
|,
data: {
search_target: "input",
turbo_permanent: true,
action: %|
blur->search#unfocus
input->search#search
%|
}
%>
<%= icon "x-circle",
variant: :mini,
size: 18,
class: "cursor-pointer text-gray-800 absolute right-1 top-1/2 transform -translate-y-1/2",
data: { search_target: "clear", action: "click->search#clear" }
%>
</div>
<% end %>
</section>

<section class="text-gray-950 dark:text-gray-100 select-none">
<turbo-frame id="conversations"
target="conversation"
data-controller="radio-behavior"
data-radio-behavior-selected-class="relationship"
class="block mb-24" <%# ensures the last conversation's menu has room to open %>
>
<% @nav_conversations.each do |named_time_span, conversations| %>
<header class="mx-2 mt-6 mb-2 text-xs font-semibold text-gray-300 text-opacity-1 dark:text-gray-500">
<%= named_time_span %>
</header>

<%= render conversations %>
<% end %>
</turbo-frame>
</section>
</turbo-frame>
4 changes: 1 addition & 3 deletions app/views/messages/_nav_column.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,4 @@
<% end %>
</section>

<turbo-frame id="nav-conversations" src="<%= conversations_path %>" loading="lazy" refresh="morph">
Placeholder content
</turbo-frame>
<turbo-frame id="nav-conversations" src="<%= conversations_path %>" loading="lazy"/>
30 changes: 30 additions & 0 deletions test/models/conversation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,34 @@ class ConversationTest < ActiveSupport::TestCase
assert_equal 3, grouped_conversations["Older"].count
end
end

test "#grouped_by_increasing_time_interval_for_user with a query returning a single conversation title" do
user = users(:keith)
query = "Ruby"

grouped_conversations = Conversation.grouped_by_increasing_time_interval_for_user(user, query)

# From prior tests. 1 with matching title.
assert_equal 1, grouped_conversations.values.flatten.count
end

test "#grouped_by_increasing_time_interval_for_user with a query returning a single conversation message" do
user = users(:keith)
query = "alive"

grouped_conversations = Conversation.grouped_by_increasing_time_interval_for_user(user, query)

# From prior tests. 1 with a matching message.
assert_equal 1, grouped_conversations.values.flatten.count
end

test "#grouped_by_increasing_time_interval_for_user with a query returning matching conversation titles and a message" do
user = users(:keith)
query = "test"

grouped_conversations = Conversation.grouped_by_increasing_time_interval_for_user(user, query)

# Total conversations from all prior tests. 2 with matching titles plus 1 other with a matching message.
assert_equal 3, grouped_conversations.values.flatten.count
end
end

0 comments on commit 7b92818

Please sign in to comment.