-
Notifications
You must be signed in to change notification settings - Fork 634
/
index.vue
256 lines (236 loc) · 7.2 KB
/
index.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<template>
<CoreBase
:immersivePage="true"
immersivePageIcon="arrow_back"
:immersivePagePrimary="false"
:primary="true"
:toolbarTitle="groupsPageStrings.$tr('classGroups')"
:appBarTitle="groupsPageStrings.$tr('classGroups')"
:immersivePageRoute="$router.getRoute('GroupMembersPage')"
:pageTitle="pageTitle"
>
<KPageContainer>
<h1>
{{ $tr('pageHeader', { className: currentGroup.name }) }}
</h1>
<form @submit.prevent="addSelectedUsersToGroup">
<div class="actions-header">
<KFilterTextbox
v-model.trim="filterInput"
:placeholder="$tr('searchForUser')"
@input="pageNum = 1"
/>
</div>
<h2>{{ $tr('userTableLabel') }}</h2>
<UserTable
v-model="selectedUsers"
:users="visibleFilteredUsers"
:selectable="true"
:selectAllLabel="$tr('selectAllOnPage')"
:userCheckboxLabel="$tr('selectUser')"
:emptyMessage="emptyMessage"
/>
<nav>
<span>
{{ $tr('pagination', {
visibleStartRange,
visibleEndRange,
numFilteredUsers
}) }}
</span>
<UiIconButton
type="primary"
:ariaLabel="$tr('previousResults')"
:disabled="pageNum === 1"
size="small"
@click="goToPage(pageNum - 1)"
>
<mat-svg
v-if="isRtl"
name="chevron_right"
category="navigation"
/>
<mat-svg
v-else
name="chevron_left"
category="navigation"
/>
</UiIconButton>
<UiIconButton
type="primary"
:ariaLabel="$tr('nextResults')"
:disabled="pageNum === numPages"
size="small"
@click="goToPage(pageNum + 1)"
>
<mat-svg
v-if="isRtl"
name="chevron_left"
category="navigation"
/>
<mat-svg
v-else
name="chevron_right"
category="navigation"
/>
</UiIconButton>
</nav>
<div class="footer">
<KButton
:text="$tr('confirmSelectionButtonLabel')"
:primary="true"
type="submit"
:disabled="selectedUsers.length === 0"
/>
</div>
</form>
</KPageContainer>
</CoreBase>
</template>
<script>
import { mapActions, mapState } from 'vuex';
import differenceWith from 'lodash/differenceWith';
import responsiveWindow from 'kolibri.coreVue.mixins.responsiveWindow';
import KButton from 'kolibri.coreVue.components.KButton';
import UiIconButton from 'kolibri.coreVue.components.UiIconButton';
import KFilterTextbox from 'kolibri.coreVue.components.KFilterTextbox';
import { crossComponentTranslator } from 'kolibri.utils.i18n';
import commonCoach from '../../common';
import {
userMatchesFilter,
filterAndSortUsers,
} from '../../../../../../facility_management/assets/src/userSearchUtils';
import UserTable from '../../../../../../facility_management/assets/src/views/UserTable';
import GroupsPage from '../GroupsPage';
const groupsPageStrings = crossComponentTranslator(GroupsPage);
export default {
name: 'GroupEnrollPage',
components: {
KButton,
UiIconButton,
KFilterTextbox,
UserTable,
},
mixins: [responsiveWindow, commonCoach],
data() {
return {
filterInput: '',
perPage: 10,
pageNum: 1,
selectedUsers: [],
groupsPageStrings,
};
},
$trs: {
pageHeader: "Enroll learners into '{className}'",
confirmSelectionButtonLabel: 'Confirm',
searchForUser: 'Search for a user',
userTableLabel: 'User List',
noUsersExist: 'No users exist',
noUsersMatch: 'No users match',
previousResults: 'Previous results',
nextResults: 'Next results',
selectAllOnPage: 'Select all on page',
allUsersAlready: 'All users are already enrolled in this class',
selectUser: 'Select user',
pagination:
'{ visibleStartRange, number } - { visibleEndRange, number } of { numFilteredUsers, number }',
},
computed: {
...mapState('groups', ['groups', 'classUsers']),
pageTitle() {
return this.$tr('pageHeader', { className: this.currentGroup.name });
},
currentGroupUsers() {
if (this.currentGroup) {
return this.currentGroup.users;
}
return [];
},
currentGroup() {
return this.groups.find(g => g.id === this.$route.params.groupId) || {};
},
usersNotInClass() {
return differenceWith(this.classUsers, this.currentGroupUsers, (a, b) => a.id === b.id);
},
sortedFilteredUsers() {
return filterAndSortUsers(this.usersNotInClass, user =>
userMatchesFilter(user, this.filterInput)
);
},
numFilteredUsers() {
return this.sortedFilteredUsers.length;
},
numPages() {
return Math.ceil(this.numFilteredUsers / this.perPage);
},
startRange() {
return (this.pageNum - 1) * this.perPage;
},
visibleStartRange() {
return Math.min(this.startRange + 1, this.numFilteredUsers);
},
endRange() {
return this.pageNum * this.perPage;
},
visibleEndRange() {
return Math.min(this.endRange, this.numFilteredUsers);
},
visibleFilteredUsers() {
return this.sortedFilteredUsers.slice(this.startRange, this.endRange);
},
emptyMessage() {
if (this.classUsers.length === 0) {
return this.$tr('noUsersExist');
}
if (this.usersNotInClass.length === 0) {
return this.$tr('allUsersAlready');
}
if (this.sortedFilteredUsers.length === 0 && this.filterInput !== '') {
// TODO internationalize this
return `${this.$tr('noUsersMatch')}: '${this.filterInput}'`;
}
return '';
},
},
methods: {
...mapActions('groups', ['addUsersToGroup']),
...mapActions(['createSnackbar']),
addSelectedUsersToGroup() {
this.addUsersToGroup({
groupId: this.currentGroup.id,
userIds: this.selectedUsers,
}).then(() => {
this.$router.push(this.$router.getRoute('GroupMembersPage'), () => {
this.createSnackbar(this.coachStrings.$tr('updatedNotification'));
});
});
},
reducePageNum() {
while (this.visibleFilteredUsers.length === 0 && this.pageNum > 1) {
this.pageNum = this.pageNum - 1;
}
},
goToPage(page) {
this.pageNum = page;
},
pageWithinRange(page) {
const maxOnEachSide = 1;
if (this.pageNum === 1 || this.pageNum === this.numPages) {
return Math.abs(this.pageNum - page) <= maxOnEachSide + 1;
}
return Math.abs(this.pageNum - page) <= maxOnEachSide;
},
},
};
</script>
<style lang="scss" scoped>
.actions-header {
margin-bottom: 8px;
}
.actions-header,
.footer,
nav {
text-align: right;
}
</style>