forked from WikiEducationFoundation/WikiEduDashboard
/
activity_table.jsx
146 lines (129 loc) · 4.16 KB
/
activity_table.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
import React from 'react';
import createReactClass from 'create-react-class';
import PropTypes from 'prop-types';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { flatten, zip } from 'lodash-es';
import { formatDateWithTime } from '../../utils/date_utils';
import * as UIActions from '../../actions';
import ActivityTableRow from './activity_table_row.jsx';
import Loading from '../common/loading.jsx';
const ActivityTable = createReactClass({
displayName: 'ActivityTable',
propTypes: {
loading: PropTypes.bool,
activity: PropTypes.array,
headers: PropTypes.array,
noActivityMessage: PropTypes.string,
openKey: PropTypes.string,
toggleDrawer: PropTypes.func
},
clearAllSortableClassNames() {
Array.prototype.forEach.call(document.getElementsByClassName('sortable'), (el) => {
el.classList.remove('asc');
el.classList.remove('desc');
});
},
sortItems(e) {
this.props.onSort(e.currentTarget.getAttribute('data-sort-key'));
},
_renderActivites() {
return this.props.activity.map((revision) => {
const roundedRevisionScore = Math.round(revision.revision_score) || 'unknown';
const revisionDateTime = formatDateWithTime(revision.datetime);
const talkPageLink = `${revision.base_url}/wiki/User_talk:${revision.username}`;
const isOpen = this.props.openKey === `drawer_${revision.key}`;
return (
<ActivityTableRow
revision={revision}
key={revision.key}
rowId={revision.key}
articleUrl={revision.article_url}
diffUrl={revision.diff_url}
talkPageLink={talkPageLink}
reportUrl={revision.report_url}
title={revision.title}
revisionScore={roundedRevisionScore}
author={revision.username}
revisionDateTime={revisionDateTime}
isOpen={isOpen}
toggleDrawer={this.props.toggleDrawer}
/>
);
});
},
_renderDrawers() {
return this.props.activity.map((revision) => {
const courses = revision.courses.map((course) => {
return (
<li key={`${revision.key}-${course.slug}`}>
<a href={`/courses/${course.slug}`}>{course.title}</a>
</li>
);
});
return (
<tr key={`${revision.key}-${revision.username}`} className="activity-table-drawer drawer">
<td colSpan="5">
<span />
<table className="table">
<tbody>
<tr>
<td>
<span>
<h5>{I18n.t('recent_activity.active_courses')}</h5>
<ul className="activity-table__course-list">
{courses}
</ul>
</span>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
);
});
},
_renderHeaders() {
return this.props.headers.map((header) => {
return (
<th style={header.style || {}} key={header.key} onClick={this.sortItems} className="sortable asc" data-sort-key={header.key}>
{header.title}
<span className="sortable-indicator" />
</th>
);
});
},
render() {
if (this.props.loading) {
return <Loading />;
}
const activity = this._renderActivites();
const drawers = this._renderDrawers();
const ths = this._renderHeaders();
let elements = flatten(zip(activity, drawers));
if (!elements.length) {
elements = <tr><td colSpan={this.props.headers.length + 1}>{this.props.noActivityMessage}</td></tr>;
}
return (
<table className="table table--expandable table--hoverable table--clickable table--sortable activity-table">
<thead>
<tr>
{ths}
<th />
</tr>
</thead>
<tbody>
{elements}
</tbody>
</table>
);
}
});
const mapStateToProps = state => ({
openKey: state.ui.openKey
});
const mapDispatchToProps = dispatch => ({
toggleDrawer: bindActionCreators(UIActions, dispatch).toggleUI
});
export default connect(mapStateToProps, mapDispatchToProps)(ActivityTable);