forked from mit-teaching-systems-lab/threeflows
/
CohortScenarios.jsx
559 lines (509 loc) · 18.2 KB
/
CohortScenarios.jsx
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
import React from 'react';
import { connect } from 'react-redux';
import { withRouter } from 'react-router-dom';
import PropTypes from 'prop-types';
import {
Button,
Checkbox,
Container,
Icon,
Input,
Menu,
Popup,
Ref,
Table
} from '@components/UI';
import copy from 'copy-text-to-clipboard';
import escapeRegExp from 'lodash.escaperegexp';
import Moment from '@utils/Moment';
import Username from '@components/User/Username';
import ConfirmAuth from '@components/ConfirmAuth';
import EditorMenu from '@components/EditorMenu';
import Loading from '@components/Loading';
import scrollIntoView from '@components/util/scrollIntoView';
import Sortable from '@components/Sortable';
import { getCohort, setCohort } from '@actions/cohort';
import { getScenarios } from '@actions/scenario';
import { getRuns } from '@actions/run';
import { getUsers } from '@actions/users';
import './Cohort.css';
export class CohortScenarios extends React.Component {
constructor(props) {
super(props);
let {
params: { id }
} = this.props.match;
if (!id && this.props.id) {
id = this.props.id;
}
this.state = {
isReady: false,
scenarios: [],
cohort: {
id
}
};
// This is used as a back up copy of
// scenarios when the list is filtered
// by searching.
this.scenarios = [];
this.tableBody = React.createRef();
this.sectionRef = React.createRef();
this.onScenarioOrderChange = this.onScenarioOrderChange.bind(this);
this.onScenarioCheckboxClick = this.onScenarioCheckboxClick.bind(this);
this.onScenarioSearchChange = this.onScenarioSearchChange.bind(this);
this.scrollIntoView = this.scrollIntoView.bind(this);
}
async componentDidMount() {
const {
cohort: { id }
} = this.state;
await this.props.getCohort(Number(id));
await this.props.getScenarios();
await this.props.getRuns();
await this.props.getUsers();
// See note above, re: scenarios list backup
const scenarios = this.props.scenarios.slice();
this.scenarios = scenarios.slice();
this.setState({
isReady: true,
scenarios
});
}
scrollIntoView() {
scrollIntoView(this.tableBody.current.node.firstElementChild);
}
onScenarioCheckboxClick(event, { checked, value }) {
const { cohort } = this.props;
if (checked) {
cohort.scenarios.push(value);
// Move to the top of the list!
this.scrollIntoView();
} else {
cohort.scenarios.splice(cohort.scenarios.indexOf(value), 1);
}
// Force deduping
const scenarios = [...new Set(cohort.scenarios)];
this.props.setCohort({
...cohort,
scenarios
});
}
async moveScenario(fromIndex, toIndex) {
const { cohort } = this.props;
const scenarios = cohort.scenarios.slice();
const moving = scenarios[fromIndex];
scenarios.splice(fromIndex, 1);
scenarios.splice(toIndex, 0, moving);
this.props.setCohort({
...cohort,
scenarios
});
}
onScenarioOrderChange(fromIndex, toIndex) {
this.moveScenario(fromIndex, toIndex);
}
onScenarioSearchChange(event, { value }) {
const { scenarios } = this;
if (value === '') {
this.setState({
scenarios
});
return;
}
if (value.length < 3) {
return;
}
const escapedRegExp = new RegExp(escapeRegExp(value), 'i');
const results = scenarios.filter(scenario => {
if (escapedRegExp.test(scenario.title)) {
return true;
}
if (escapedRegExp.test(scenario.description)) {
return true;
}
return false;
});
this.setState({
scenarios: results
});
}
render() {
const {
onScenarioOrderChange,
onScenarioCheckboxClick,
onScenarioSearchChange
} = this;
const { authority, cohort, onClick, runs, user, usersById } = this.props;
const { isReady, scenarios } = this.state;
const { isFacilitator, isParticipant } = authority;
if (!isReady) {
return <Loading />;
}
// This is the list of scenarios that are IN the
// cohort. The order MUST be preserved.
const cohortScenarios = cohort.scenarios.map(id =>
scenarios.find(scenario => scenario.id === id)
);
// This is the list of scenarios that are available,
// but NOT in the cohort. The order is by id, descending
const reducedScenarios = scenarios.reduce((accum, scenario) => {
if (!cohort.scenarios.includes(scenario.id)) {
accum.push(scenario);
}
return accum;
}, []);
const orderCorrectedScenarios = cohortScenarios;
if (isFacilitator || user.is_super) {
orderCorrectedScenarios.push(...reducedScenarios);
}
const right = [
<Menu.Menu key="menu-menu-search-cohort-scenarios" position="right">
<Menu.Item
key="menu-item-search-cohort-scenarios"
name="Search cohort scenarios"
>
<Input
icon="search"
placeholder="Search..."
onChange={onScenarioSearchChange}
/>
</Menu.Item>
{/*
<Menu.Item
onClick={() => this.sectionRef.scrollIntoView()}
>
<Icon name="angle double up" />
</Menu.Item>
*/}
</Menu.Menu>
];
const editorMenu = (
<Ref innerRef={node => (this.sectionRef = node)}>
<EditorMenu
type="cohort scenarios"
items={{
left: [
<Menu.Item
key="menu-item-cohort-scenarios"
name="Scenarios in this Cohort"
onClick={this.scrollIntoView}
>
<Icon.Group className="em__icon-group-margin">
<Icon name="newspaper outline" />
</Icon.Group>
Scenarios ({cohortScenarios.length})
</Menu.Item>
],
right: user.is_super || isFacilitator ? right : []
}}
/>
</Ref>
);
return (
<Container fluid className="c__table-container">
{editorMenu}
<Table
fixed
striped
selectable
role="grid"
aria-labelledby="header"
className="c__table--constraints"
unstackable
>
<Table.Header className="c__table-thead-tbody-tr">
<Table.Row>
<ConfirmAuth
requiredPermission="edit_scenarios_in_cohort"
isAuthorized={isFacilitator}
>
<Table.HeaderCell className="c__table-cell-first">
<Icon.Group className="em__icon-group-margin">
<Icon name="newspaper outline" />
<Icon corner="top right" name="add" color="green" />
</Icon.Group>
</Table.HeaderCell>
<Table.HeaderCell className="c__table-cell-options c__hidden-on-mobile">
Tools
</Table.HeaderCell>
</ConfirmAuth>
<Table.HeaderCell>Title</Table.HeaderCell>
<Table.HeaderCell className="c__table-cell-content">
{isFacilitator ? 'Author' : 'Started'}
</Table.HeaderCell>
<Table.HeaderCell className="c__table-cell-content">
{isFacilitator ? 'Description' : 'Completed'}
</Table.HeaderCell>
</Table.Row>
</Table.Header>
{scenarios.length ? (
<Sortable
tag="tbody"
className="c__scrolling-tbody"
isAuthorized={isFacilitator}
onChange={onScenarioOrderChange}
tableRef={this.tableBody}
options={{
direction: 'vertical',
swapThreshold: 0.5,
animation: 150
}}
>
{orderCorrectedScenarios.map((scenario, index) => {
if (!scenario) {
return null;
}
const checked = cohort.scenarios.includes(scenario.id);
const disabled = true;
const props = !checked ? { disabled } : {};
// TODO: check localstorage for more appropriate slide number to begin at
const pathname = `/cohort/${cohort.id}/run/${scenario.id}/slide/0`;
const url = `${location.origin}${pathname}`;
const requiredPermission = checked
? 'view_scenarios_in_cohort'
: 'edit_scenarios_in_cohort';
const onClickAddTab = (event, data) => {
onClick(event, {
...data,
type: 'scenario',
source: scenario
});
};
const run =
runs.find(run => run.scenario_id === scenario.id) || {};
const { run_created_at = null, run_ended_at = null } = run;
const createdStatus = run_created_at
? { warning: true }
: { negative: true };
const completeOrIncomplete = isParticipant
? run_ended_at
? { positive: true }
: createdStatus
: {};
const createdAt = run_created_at
? Moment(run_created_at).fromNow()
: '';
const createdAtAlt = run_created_at
? Moment(run_created_at).calendar()
: '';
const endedAt = run_ended_at
? Moment(run_ended_at).fromNow()
: '';
const endedAtAlt = run_ended_at
? Moment(run_ended_at).calendar()
: 'This scenario is not complete';
const startedAtDisplay = run_created_at
? `${createdAt} (${createdAtAlt})`.trim()
: 'This scenario has not been started';
const endedAtCreatedAtAlt = run_created_at ? endedAtAlt : '';
const endedAtDisplay = run_ended_at
? `${endedAt} (${endedAtAlt})`.trim()
: endedAtCreatedAtAlt;
const completionStatus = !isFacilitator
? completeOrIncomplete
: {};
return (
<ConfirmAuth
key={`confirm-${index}`}
requiredPermission={requiredPermission}
>
<Table.Row
key={`row-${index}`}
className="c__table-thead-tbody-tr"
style={{ cursor: 'pointer' }}
{...completionStatus}
>
<ConfirmAuth
isAuthorized={isFacilitator}
requiredPermission="edit_own_cohorts"
>
<Table.Cell
key={`cell-checkbox-${index}`}
className="c__table-cell-first"
>
<Checkbox
key={`checkbox-${index}`}
value={scenario.id}
checked={checked}
onClick={onScenarioCheckboxClick}
/>
</Table.Cell>
</ConfirmAuth>
<ConfirmAuth
isAuthorized={isFacilitator}
requiredPermission="edit_scenarios_in_cohort"
>
<Table.Cell className="c__table-cell-options c__hidden-on-mobile">
{checked ? (
<Button.Group
hidden
basic
size="small"
className="c__button-group--transparent"
>
<Popup
size="tiny"
content="Copy cohort scenario link to clipboard"
trigger={
<Button
icon
content={<Icon name="clipboard outline" />}
onClick={() => copy(url)}
{...props}
/>
}
/>
<Popup
size="tiny"
content="Run this cohort scenario as a participant"
trigger={
<Button
icon
content={<Icon name="play" />}
onClick={() => {
location.href = url;
}}
{...props}
/>
}
/>
<ConfirmAuth
isAuthorized={isFacilitator}
requiredPermission="view_all_data"
>
<Popup
content="View cohort reponses to prompts in this scenario"
trigger={
<Button
icon
content={
<Icon name="file alternate outline" />
}
name={scenario.title}
onClick={onClickAddTab}
{...props}
/>
}
/>
</ConfirmAuth>
<Popup
size="tiny"
content="Move scenario up"
trigger={
<Button
icon="caret up"
aria-label="Move scenario up"
disabled={index === 0}
onClick={() => {
onScenarioOrderChange(index, index - 1);
}}
/>
}
/>
<Popup
size="tiny"
content="Move scenario down"
trigger={
<Button
icon="caret down"
aria-label="Move scenario down"
disabled={
index === cohortScenarios.length - 1
}
onClick={() => {
onScenarioOrderChange(index, index + 1);
}}
/>
}
/>
</Button.Group>
) : null}
</Table.Cell>
</ConfirmAuth>
<Table.Cell.Clickable
href={pathname}
content={scenario.title}
/>
<Table.Cell className="c__table-cell-content">
{isFacilitator ? (
<Username {...usersById[scenario.author.id]} />
) : (
startedAtDisplay
)}
</Table.Cell>
<Table.Cell className="c__table-cell-content">
{isFacilitator ? scenario.description : endedAtDisplay}
</Table.Cell>
</Table.Row>
</ConfirmAuth>
);
})}
</Sortable>
) : (
<Table.Body className="c__scrolling-tbody">
<Table.Row
key={`row-empty-results`}
className="c__table-thead-tbody-tr"
>
<Table.Cell>No scenarios match your search</Table.Cell>
</Table.Row>
</Table.Body>
)}
</Table>
</Container>
);
}
}
CohortScenarios.propTypes = {
authority: PropTypes.object,
cohort: PropTypes.shape({
id: PropTypes.any,
name: PropTypes.string,
roles: PropTypes.array,
runs: PropTypes.array,
scenarios: PropTypes.array,
users: PropTypes.array
}),
getCohort: PropTypes.func,
setCohort: PropTypes.func,
history: PropTypes.shape({
push: PropTypes.func.isRequired
}).isRequired,
id: PropTypes.any,
match: PropTypes.shape({
path: PropTypes.string,
params: PropTypes.shape({
id: PropTypes.node
}).isRequired
}).isRequired,
onClick: PropTypes.func,
getScenarios: PropTypes.func,
scenarios: PropTypes.array,
getRuns: PropTypes.func,
runs: PropTypes.array,
user: PropTypes.object,
getUsers: PropTypes.func,
usersById: PropTypes.object
};
const mapStateToProps = (state, ownProps) => {
const { cohort, cohorts, user, usersById } = state;
const scenarios = state.scenarios.filter(
({ deleted_at, status }) => deleted_at === null && status !== 1
);
const runs = state.runs.filter(run => run.cohort_id === ownProps.id);
return { cohort, cohorts, scenarios, runs, user, usersById };
};
const mapDispatchToProps = dispatch => ({
getCohort: id => dispatch(getCohort(id)),
setCohort: params => dispatch(setCohort(params)),
getScenarios: () => dispatch(getScenarios()),
getRuns: () => dispatch(getRuns()),
getUsers: () => dispatch(getUsers())
});
export default withRouter(
connect(
mapStateToProps,
mapDispatchToProps
)(CohortScenarios)
);