FEATURE: Added sort by username for directory items (#10482)

FEATURE: Added sort by username for directory items (#10482)

  • done-sorting-usernames-on-users-page

  • sorted-usernames-on-users-page

  • spec written

  • specs added

diff --git a/1.5 b/1.5
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/1.5
diff --git a/app/assets/javascripts/discourse/app/templates/users.hbs b/app/assets/javascripts/discourse/app/templates/users.hbs
index 0766928..b406e63 100644
--- a/app/assets/javascripts/discourse/app/templates/users.hbs
+++ b/app/assets/javascripts/discourse/app/templates/users.hbs
@@ -13,7 +13,14 @@
               </div>
             {{/if}}
           </div>
-          {{text-field value=nameInput placeholderKey="directory.filter_name" class="filter-name no-blur"}}
+          <div>
+            <span class="total-rows">
+              {{#if model.totalRows}}
+                {{i18n "directory.total_rows" count=model.totalRows}}
+              {{/if}}
+            </span>
+            {{text-field value=nameInput placeholderKey="directory.filter_name" class="filter-name no-blur"}}
+          </div>
         </div>
 
         {{#conditional-loading-spinner condition=isLoading}}
@@ -21,7 +28,7 @@
 
             <table>
               <thead>
-                <th>{{i18n "directory.total_rows" count=model.totalRows}}</th>
+                {{table-header-toggle field="username" order=order asc=asc}}
                 {{table-header-toggle field="likes_received" order=order asc=asc icon="heart"}}
                 {{table-header-toggle field="likes_given" order=order asc=asc icon="heart"}}
                 {{table-header-toggle field="topic_count" order=order asc=asc}}
diff --git a/app/assets/stylesheets/common/base/directory.scss b/app/assets/stylesheets/common/base/directory.scss
index c68f47e..bbe8757 100644
--- a/app/assets/stylesheets/common/base/directory.scss
+++ b/app/assets/stylesheets/common/base/directory.scss
@@ -23,6 +23,9 @@
   .directory-controls {
     display: flex;
     justify-content: space-between;
+    .total-rows {
+      color: var(--primary-medium);
+    }
     .filter-name {
       height: 1em;
     }
diff --git a/app/controllers/directory_items_controller.rb b/app/controllers/directory_items_controller.rb
index a0af52a..d174571 100644
--- a/app/controllers/directory_items_controller.rb
+++ b/app/controllers/directory_items_controller.rb
@@ -27,9 +27,11 @@ class DirectoryItemsController < ApplicationController
     end
 
     order = params[:order] || DirectoryItem.headings.first
+    dir = params[:asc] ? 'ASC' : 'DESC'
     if DirectoryItem.headings.include?(order.to_sym)
-      dir = params[:asc] ? 'ASC' : 'DESC'
       result = result.order("directory_items.#{order} #{dir}")
+    elsif params[:order] === 'username'
+      result = result.order("users.#{order} #{dir}")
     end
 
     if period_type == DirectoryItem.period_types[:all]
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index f82f5d8..4e9e6ba 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -578,6 +578,7 @@ en:
       sent_by_you: "Sent by <a href='%{userUrl}'>you</a>"
 
     directory:
+      username: "Username"
       filter_name: "filter by username"
       title: "Users"
       likes_given: "Given"
diff --git a/spec/requests/directory_items_controller_spec.rb b/spec/requests/directory_items_controller_spec.rb
index 80817ef..df7042c 100644
--- a/spec/requests/directory_items_controller_spec.rb
+++ b/spec/requests/directory_items_controller_spec.rb
@@ -60,6 +60,25 @@ describe DirectoryItemsController do
       expect(response).not_to be_successful
     end
 
+    it "sort username with asc as a parameter" do
+      get '/directory_items.json', params: { asc: true, order: 'username', period: 'all' }
+      expect(response.status).to eq(200)
+      json = response.parsed_body
+
+      names = json['directory_items'].map { |item| item['user']['username'] }
+      expect(names).to eq(names.sort)
+    end
+
+    it "sort username without asc as a parameter" do
+      get '/directory_items.json', params: { order: 'username', period: 'all' }
+      expect(response.status).to eq(200)
+      json = response.parsed_body
+
+      names = json['directory_items'].map { |item| item['user']['username'] }
+
+      expect(names).to eq(names.sort.reverse)
+    end
+
     it "finds user by name" do
       get '/directory_items.json', params: { period: 'all', name: 'eviltrout' }
       expect(response.status).to eq(200)

GitHub sha: ae7ff5eb

This commit appears in #10482 which was merged by eviltrout.