forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentView.js
More file actions
231 lines (203 loc) · 8.76 KB
/
Copy pathStudentView.js
File metadata and controls
231 lines (203 loc) · 8.76 KB
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
import React from 'react'
import ReactDOM from 'react-dom'
import $ from 'jquery'
import I18n from 'i18n!student_groups'
import natcompare from 'compiled/util/natcompare'
import Group from 'compiled/models/Group'
import UserCollection from 'compiled/collections/UserCollection'
import ContextGroupCollection from 'compiled/collections/ContextGroupCollection'
import BackboneState from 'jsx/groups/mixins/BackboneState'
import PaginatedGroupList from 'jsx/groups/components/PaginatedGroupList'
import Filter from 'jsx/groups/components/Filter'
import NewGroupDialog from 'jsx/groups/components/NewGroupDialog'
import ManageGroupDialog from 'jsx/groups/components/ManageGroupDialog'
const StudentView = React.createClass({
mixins: [BackboneState],
getInitialState () {
return {
filter: '',
userCollection: new UserCollection(null, {
params: { enrollment_type: 'student' },
comparator: natcompare.byGet('sortable_name'),
}),
groupCollection: new ContextGroupCollection([], { course_id: ENV.course_id })
}
},
openManageGroupDialog (group) {
const $dialog = $('<div>').dialog({
id: 'manage_group_form',
title: 'Manage Student Group',
height: 500,
width: 700,
'fix-dialog-buttons': false,
close: (e) => {
ReactDOM.unmountComponentAtNode($dialog[0])
$(this).remove()
},
})
const closeDialog = (e) => {
e.preventDefault()
$dialog.dialog('close')
}
ReactDOM.render(<ManageGroupDialog userCollection={this.state.userCollection}
checked={group.users.map((u) => u.id)}
groupId={group.id}
name={group.name}
maxMembership={group.max_membership}
updateGroup={this.updateGroup}
closeDialog={closeDialog}
loadMore={() => this._loadMore(this.state.userCollection)} />, $dialog[0])
},
openNewGroupDialog () {
const $dialog = $('<div>').dialog({
id: 'add_group_form',
title: 'New Student Group',
height: 500,
width: 700,
'fix-dialog-buttons': false,
close: (e) => {
ReactDOM.unmountComponentAtNode($dialog[0])
$(this).remove()
},
})
const closeDialog = (e) => {
e.preventDefault()
$dialog.dialog('close')
}
ReactDOM.render(<NewGroupDialog userCollection={this.state.userCollection}
createGroup={this.createGroup}
closeDialog={closeDialog}
loadMore={() => this._loadMore(this.state.userCollection)} />, $dialog[0])
},
_categoryGroups (group) {
return this.state.groupCollection.filter((g) => g.get('group_category_id') === group.get('group_category_id'))
},
_onCreateGroup (group) {
this.state.groupCollection.add(group)
$.flashMessage(I18n.t('Created Group %{group_name}', {group_name: group.name}))
},
createGroup (name, joinLevel, invitees) {
$.ajaxJSON(`/courses/${ENV.course_id}/groups`,
'POST',
{group: {name, join_level: joinLevel}, invitees},
(group) => this._onCreateGroup(group))
},
_onUpdateGroup (group) {
this.state.groupCollection.add(group, {merge: true})
$.flashMessage(I18n.t('Updated Group %{group_name}', {group_name: group.name}))
},
updateGroup (groupId, name, members) {
$.ajaxJSON(`/api/v1/groups/${groupId}`,
'PUT',
{name: name, members: members},
(group) => this._onUpdateGroup(group))
},
_loadMore (collection) {
if (!collection.loadedAll && !collection.fetchingNextPage) {
// if we specify a page before we actually need it, we lose
// the params being passed to the api
const options = collection.length === 0 ? {} : {page: 'next'}
collection.fetch(options)
}
},
_extendAttribute (model, attribute, hash) {
const copy = Object.assign({}, model.get(attribute))
model.set(attribute, Object.assign(copy, hash))
},
_addUser (groupModel, user) {
groupModel.set('users', groupModel.get('users').concat(user))
},
_removeUser (groupModel, userId) {
groupModel.set('users', groupModel.get('users').filter((u) => u.id !== userId))
// If user was a leader, unset the leader attribute.
const leader = groupModel.get('leader')
if (leader && leader.id === userId) {
groupModel.set('leader', null)
}
},
_onLeave (group) {
const groupModel = this.state.groupCollection.get(group.id)
this._removeUser(groupModel, ENV.current_user_id)
if (!groupModel.get('group_category').allows_multiple_memberships) {
this._categoryGroups(groupModel).forEach((g) => {
this._extendAttribute(g, 'group_category', {is_member: false})
})
}
$.flashMessage(I18n.t('Left Group %{group_name}', {group_name: group.name}))
},
leave (group) {
const dfd = $.ajaxJSON(`/api/v1/groups/${group.id}/memberships/self`,
'DELETE',
{},
() => this._onLeave(group))
$(ReactDOM.findDOMNode(this.refs.panel)).disableWhileLoading(dfd)
},
_onJoin (group) {
const groupModel = this.state.groupCollection.get(group.id)
this._categoryGroups(groupModel).forEach((g) => {
this._extendAttribute(g, 'group_category', {is_member: true})
if (!groupModel.get('group_category').allows_multiple_memberships) {
this._removeUser(g, ENV.current_user_id)
}
})
this._addUser(groupModel, ENV.current_user)
$.flashMessage(I18n.t('Joined Group %{group_name}', {group_name: group.name}))
},
join (group) {
const dfd = $.ajaxJSON(`/api/v1/groups/${group.id}/memberships`,
'POST',
{user_id: 'self'},
() => this._onJoin(group),
// This is making an assumption that when the current user can't join a group it is likely beacuse a student
// from another section joined that group after the page loaded for the current user
() => this._extendAttribute(this.state.groupCollection.get(group.id), 'permissions', {join: false}))
$(ReactDOM.findDOMNode(this.refs.panel)).disableWhileLoading(dfd)
},
_filter (group) {
const filter = this.state.filter.toLowerCase()
return (!filter ||
group.name.toLowerCase().indexOf(filter) > -1 ||
group.users.some(u => u.name.toLowerCase().indexOf(filter) > -1))
},
manage (group) {
this.openManageGroupDialog(group)
},
render () {
const filteredGroups = this.state.groupCollection.toJSON().filter(this._filter)
let newGroupButton = null
if (ENV.STUDENT_CAN_ORGANIZE_GROUPS_FOR_COURSE) {
newGroupButton = (
<button aria-label={I18n.t('Add new group')} className="btn btn-primary add_group_link" onClick={this.openNewGroupDialog}>
<i className="icon-plus" />
{I18n.t('Group')}
</button>)
}
return (
<div>
<div id="group_categories_tabs" className="ui-tabs-minimal ui-tabs ui-widget ui-widget-content ui-corner-all">
<ul className="collectionViewItems ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all">
<li className="ui-state-default ui-corner-top">
<a href={`/courses/${ENV.course_id}/users`}>{I18n.t('Everyone')}</a>
</li>
<li className="ui-state-default ui-corner-top ui-tabs-active ui-state-active">
<a href="#" tabIndex="-1">{I18n.t('Groups')}</a>
</li>
</ul>
<div className="pull-right group-categories-actions">
{newGroupButton}
</div>
<div className="roster-tab tab-panel" ref="panel">
<Filter onChange={(e) => this.setState({filter: e.target.value})} />
<PaginatedGroupList loading={this.state.groupCollection.fetchingNextPage}
groups={filteredGroups}
filter={this.state.filter}
loadMore={() => this._loadMore(this.state.groupCollection)}
onLeave={this.leave}
onJoin={this.join}
onManage={this.manage}/>
</div>
</div>
</div>)
},
})
export default <StudentView />