|
| 1 | +import Component from '@glimmer/component'; |
| 2 | +import { cached } from '@glimmer/tracking'; |
| 3 | +import { action } from '@ember/object'; |
| 4 | +import { fn } from '@ember/helper'; |
| 5 | +import or from 'ember-truth-helpers/helpers/or'; |
| 6 | +import eq from 'ember-truth-helpers/helpers/eq'; |
1 | 7 | import t from 'ember-intl/helpers/t';
|
2 | 8 | import FaIcon from 'ilios-common/components/fa-icon';
|
3 | 9 | import { LinkTo } from '@ember/routing';
|
| 10 | +import { TrackedAsyncData } from 'ember-async-data'; |
4 | 11 | import UserNameInfo from 'ilios-common/components/user-name-info';
|
5 |
| -<template> |
6 |
| - <div class="user-list" data-test-user-list ...attributes> |
7 |
| - <table> |
8 |
| - <thead> |
| 12 | +import SortableTh from 'ilios-common/components/sortable-th'; |
| 13 | +import LoadingSpinner from 'ilios-common/components/loading-spinner'; |
| 14 | + |
| 15 | +export default class UserList extends Component { |
| 16 | + get sortedAscending() { |
| 17 | + return this.args.sortBy.search(/desc/) === -1; |
| 18 | + } |
| 19 | + |
| 20 | + @cached |
| 21 | + get sortedUsersData() { |
| 22 | + return new TrackedAsyncData( |
| 23 | + this.args.searchForUsers.perform( |
| 24 | + this.sortInfo.column, |
| 25 | + this.sortInfo.descending ? 'DESC' : 'ASC', |
| 26 | + ), |
| 27 | + ); |
| 28 | + } |
| 29 | + |
| 30 | + get sortedUsers() { |
| 31 | + if (this.args.users) { |
| 32 | + return this.args.users; |
| 33 | + } |
| 34 | + return this.sortedUsersData.isResolved ? this.sortedUsersData.value : []; |
| 35 | + } |
| 36 | + |
| 37 | + get sortInfo() { |
| 38 | + const parts = this.args.sortBy.split(':'); |
| 39 | + const column = parts[0]; |
| 40 | + const descending = parts.length > 1 && parts[1] === 'desc'; |
| 41 | + |
| 42 | + return { column, descending, sortBy: this.args.sortBy }; |
| 43 | + } |
| 44 | + |
| 45 | + @action |
| 46 | + setSortBy(what) { |
| 47 | + if (this.args.sortBy === what) { |
| 48 | + what += ':desc'; |
| 49 | + } |
| 50 | + this.args.setSortBy(what); |
| 51 | + } |
| 52 | + <template> |
| 53 | + <table class="user-list" data-test-user-list ...attributes> |
| 54 | + <thead class={{if @headerIsLocked "locked"}}> |
9 | 55 | <tr>
|
10 |
| - <th class="text-left" colspan="1"></th> |
11 |
| - <th class="text-left" colspan="3"> |
| 56 | + <th colspan="1" class="user-list-disabled" data-test-user-list-disabled></th> |
| 57 | + <SortableTh |
| 58 | + @align="left" |
| 59 | + @colspan={{3}} |
| 60 | + @sortedAscending={{this.sortedAscending}} |
| 61 | + @onClick={{fn this.setSortBy "fullName"}} |
| 62 | + @sortedBy={{or (eq @sortBy "fullName") (eq @sortBy "fullName:desc")}} |
| 63 | + > |
12 | 64 | {{t "general.fullName"}}
|
13 |
| - </th> |
14 |
| - <th class="text-left hide-from-small-screen" colspan="2"> |
| 65 | + </SortableTh> |
| 66 | + <SortableTh |
| 67 | + class="hide-from-small-screen" |
| 68 | + @align="left" |
| 69 | + @colspan={{2}} |
| 70 | + @sortedAscending={{this.sortedAscending}} |
| 71 | + @onClick={{fn this.setSortBy "campusId"}} |
| 72 | + @sortedBy={{or (eq @sortBy "campusId") (eq @sortBy "campusId:desc")}} |
| 73 | + > |
15 | 74 | {{t "general.campusId"}}
|
16 |
| - </th> |
17 |
| - <th class="text-left hide-from-small-screen" colspan="5"> |
| 75 | + </SortableTh> |
| 76 | + <SortableTh |
| 77 | + class="hide-from-small-screen" |
| 78 | + @align="left" |
| 79 | + @colspan={{5}} |
| 80 | + @sortedAscending={{this.sortedAscending}} |
| 81 | + @onClick={{fn this.setSortBy "email"}} |
| 82 | + @sortedBy={{or (eq @sortBy "email") (eq @sortBy "email:desc")}} |
| 83 | + > |
18 | 84 | {{t "general.email"}}
|
19 |
| - </th> |
20 |
| - <th class="text-left hide-from-small-screen" colspan="2"> |
21 |
| - {{t "general.primarySchool"}} |
22 |
| - </th> |
| 85 | + </SortableTh> |
| 86 | + <th colspan="2" class="hide-from-small-screen" data-test-user-list-school>{{t |
| 87 | + "general.primarySchool" |
| 88 | + }}</th> |
23 | 89 | </tr>
|
24 | 90 | </thead>
|
25 | 91 | <tbody>
|
26 |
| - {{#each @users as |user|}} |
27 |
| - <tr class={{unless user.enabled "disabled-user-account" ""}} data-test-user> |
28 |
| - <td class="text-left" colspan="1"> |
29 |
| - {{#unless user.enabled}} |
30 |
| - <FaIcon |
31 |
| - @icon="user-xmark" |
32 |
| - @title={{t "general.disabled"}} |
33 |
| - class="error" |
34 |
| - data-test-disabled-user-icon |
35 |
| - /> |
36 |
| - {{/unless}} |
37 |
| - </td> |
38 |
| - <td class="text-left" colspan="3"> |
39 |
| - <LinkTo @route="user" @model={{user}} data-test-user-link> |
40 |
| - <UserNameInfo @user={{user}} /> |
41 |
| - </LinkTo> |
42 |
| - </td> |
43 |
| - <td class="text-left hide-from-small-screen" colspan="2" data-test-campus-id> |
44 |
| - {{user.campusId}} |
45 |
| - </td> |
46 |
| - <td class="text-left hide-from-small-screen" colspan="5" data-test-email> |
47 |
| - {{user.email}} |
48 |
| - </td> |
49 |
| - <td class="text-left hide-from-small-screen" colspan="2" data-test-school> |
50 |
| - {{user.school.title}} |
51 |
| - </td> |
52 |
| - </tr> |
53 |
| - {{/each}} |
| 92 | + {{#if (or @users this.sortedUsersData.isResolved)}} |
| 93 | + {{#if this.sortedUsers.length}} |
| 94 | + {{#each this.sortedUsers as |user|}} |
| 95 | + <tr |
| 96 | + class="user-list-row{{unless user.enabled ' disabled-user-account'}}" |
| 97 | + data-test-user |
| 98 | + > |
| 99 | + <td colspan="1" class="text-left" data-test-user-disabled> |
| 100 | + {{#unless user.enabled}} |
| 101 | + <FaIcon |
| 102 | + @icon="user-xmark" |
| 103 | + @title={{t "general.disabled"}} |
| 104 | + class="error" |
| 105 | + data-test-disabled-user-icon |
| 106 | + /> |
| 107 | + {{/unless}} |
| 108 | + </td> |
| 109 | + <td colspan="3" class="text-left" data-test-full-name> |
| 110 | + <LinkTo @route="user" @model={{user}} data-test-user-link> |
| 111 | + <UserNameInfo @user={{user}} /> |
| 112 | + </LinkTo> |
| 113 | + </td> |
| 114 | + <td colspan="2" class="text-left hide-from-small-screen" data-test-campus-id> |
| 115 | + {{user.campusId}} |
| 116 | + </td> |
| 117 | + <td colspan="5" class="text-left hide-from-small-screen" data-test-email> |
| 118 | + {{user.email}} |
| 119 | + </td> |
| 120 | + <td colspan="2" class="text-left hide-from-small-screen" data-test-school> |
| 121 | + {{user.school.title}} |
| 122 | + </td> |
| 123 | + </tr> |
| 124 | + {{/each}} |
| 125 | + {{else}} |
| 126 | + <tr> |
| 127 | + <td colspan="13" class="no-results"> |
| 128 | + {{t "general.noResultsFound"}} |
| 129 | + </td> |
| 130 | + </tr> |
| 131 | + {{/if}} |
| 132 | + {{else}} |
| 133 | + <LoadingSpinner /> |
| 134 | + {{/if}} |
54 | 135 | </tbody>
|
55 | 136 | </table>
|
56 |
| - </div> |
57 |
| -</template> |
| 137 | + </template> |
| 138 | +} |
0 commit comments