Skip to content

Commit 8d81829

Browse files
committed
feat: add user filter in dashboard
closes #284
1 parent 7cb48aa commit 8d81829

File tree

3 files changed

+71
-14
lines changed

3 files changed

+71
-14
lines changed

src/components/dashboard/ActionEditor.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import { withStyles } from '@material-ui/core';
99
import { getDatabase } from '../../actions';
1010
import Loader from '../common/Loader';
1111
import Styles from '../../Styles';
12-
import { FILTER_ALL_SPACE_ID } from '../../config/constants';
12+
import {
13+
FILTER_ALL_SPACES_ID,
14+
FILTER_ALL_USERS_ID,
15+
} from '../../config/constants';
1316

1417
export class ActionEditor extends Component {
1518
static propTypes = {
@@ -24,11 +27,13 @@ export class ActionEditor extends Component {
2427
actions: PropTypes.arrayOf(PropTypes.object),
2528
}),
2629
spaceId: PropTypes.string,
30+
userId: PropTypes.string,
2731
};
2832

2933
static defaultProps = {
3034
database: {},
31-
spaceId: FILTER_ALL_SPACE_ID,
35+
spaceId: FILTER_ALL_SPACES_ID,
36+
userId: FILTER_ALL_USERS_ID,
3237
};
3338

3439
componentDidMount() {
@@ -37,7 +42,7 @@ export class ActionEditor extends Component {
3742
}
3843

3944
render() {
40-
const { database, t, spaceId } = this.props;
45+
const { database, t, spaceId, userId } = this.props;
4146

4247
if (!database) {
4348
return <Loader />;
@@ -48,7 +53,10 @@ export class ActionEditor extends Component {
4853
}
4954

5055
let { actions } = database;
51-
if (spaceId !== FILTER_ALL_SPACE_ID) {
56+
if (userId !== FILTER_ALL_USERS_ID) {
57+
actions = actions.filter(({ user }) => user === userId);
58+
}
59+
if (spaceId !== FILTER_ALL_SPACES_ID) {
5260
actions = actions.filter(({ spaceId: id }) => id === spaceId);
5361
}
5462

src/components/dashboard/Dashboard.js

+57-9
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ import ActionEditor from './ActionEditor';
1919
import Loader from '../common/Loader';
2020
import Main from '../common/Main';
2121
import { getDatabase } from '../../actions';
22-
import { FILTER_ALL_SPACE_ID, USER_MODES } from '../../config/constants';
22+
import {
23+
FILTER_ALL_SPACES_ID,
24+
FILTER_ALL_USERS_ID,
25+
USER_MODES,
26+
} from '../../config/constants';
2327
import { DASHBOARD_MAIN_ID } from '../../config/selectors';
2428

2529
const styles = theme => ({
@@ -36,7 +40,8 @@ const styles = theme => ({
3640

3741
export class Dashboard extends Component {
3842
state = {
39-
spaceId: FILTER_ALL_SPACE_ID,
43+
spaceId: FILTER_ALL_SPACES_ID,
44+
filteredUserId: FILTER_ALL_USERS_ID,
4045
};
4146

4247
static propTypes = {
@@ -69,6 +74,7 @@ export class Dashboard extends Component {
6974
database: PropTypes.shape({
7075
user: PropTypes.object,
7176
spaces: PropTypes.arrayOf(PropTypes.object),
77+
users: PropTypes.arrayOf(PropTypes.object),
7278
actions: PropTypes.arrayOf(PropTypes.object),
7379
}),
7480
dispatchGetDatabase: PropTypes.func.isRequired,
@@ -89,6 +95,10 @@ export class Dashboard extends Component {
8995
this.setState({ spaceId: event.target.value });
9096
};
9197

98+
handleUserChange = event => {
99+
this.setState({ filteredUserId: event.target.value });
100+
};
101+
92102
renderSpaceFilter = () => {
93103
const { database, t } = this.props;
94104
const { spaceId } = this.state;
@@ -105,7 +115,7 @@ export class Dashboard extends Component {
105115
value={spaceId}
106116
onChange={this.handleSpaceChange}
107117
>
108-
<MenuItem value={FILTER_ALL_SPACE_ID}>
118+
<MenuItem value={FILTER_ALL_SPACES_ID}>
109119
<em>{t('All Spaces')}</em>
110120
</MenuItem>
111121
{database.spaces.map(space => (
@@ -116,19 +126,54 @@ export class Dashboard extends Component {
116126
);
117127
};
118128

129+
renderUserFilter = () => {
130+
const { database, t, userMode } = this.props;
131+
const { filteredUserId } = this.state;
132+
133+
if (!database || _.isEmpty(database)) {
134+
return <Loader />;
135+
}
136+
137+
if (userMode !== USER_MODES.TEACHER) {
138+
return null;
139+
}
140+
141+
return (
142+
<FormControl variant="outlined" fullWidth>
143+
<InputLabel>{t('Filter by User')}</InputLabel>
144+
<Select
145+
label="Filter by User"
146+
value={filteredUserId}
147+
onChange={this.handleUserChange}
148+
>
149+
<MenuItem value={FILTER_ALL_USERS_ID}>
150+
<em>{t('All Users')}</em>
151+
</MenuItem>
152+
{database.users.map(user => (
153+
<MenuItem value={user.id}>{user.username}</MenuItem>
154+
))}
155+
</Select>
156+
</FormControl>
157+
);
158+
};
159+
119160
renderActionWidgets = () => {
120161
const { database, t, classes, userMode, userId } = this.props;
121-
const { spaceId } = this.state;
162+
const { spaceId, filteredUserId } = this.state;
122163

123164
let filteredActions = database.actions;
124165

125-
// filter action per user if userMode is student
166+
// filter action per user if userMode is student or with filter
126167
if (userMode === USER_MODES.STUDENT) {
127168
filteredActions = filteredActions.filter(({ user }) => user === userId);
169+
} else if (filteredUserId !== FILTER_ALL_USERS_ID) {
170+
filteredActions = filteredActions.filter(
171+
({ user }) => user === filteredUserId
172+
);
128173
}
129174

130175
// filter action per space
131-
if (spaceId !== FILTER_ALL_SPACE_ID) {
176+
if (spaceId !== FILTER_ALL_SPACES_ID) {
132177
filteredActions = filteredActions.filter(
133178
({ spaceId: id }) => id === spaceId
134179
);
@@ -164,7 +209,7 @@ export class Dashboard extends Component {
164209

165210
render() {
166211
const { classes, t, database } = this.props;
167-
const { spaceId } = this.state;
212+
const { spaceId, filteredUserId } = this.state;
168213

169214
if (!database) {
170215
return <Loader />;
@@ -184,19 +229,22 @@ export class Dashboard extends Component {
184229
alignItems="center"
185230
spacing={3}
186231
>
187-
<Grid item xs={12} sm={9}>
232+
<Grid item xs={12} sm={6}>
188233
<Typography variant="h4" className={classes.screenTitle}>
189234
{t('Dashboard')}
190235
</Typography>
191236
</Grid>
237+
<Grid item xs={12} sm={3}>
238+
{this.renderUserFilter()}
239+
</Grid>
192240
<Grid item xs={12} sm={3}>
193241
{this.renderSpaceFilter()}
194242
</Grid>
195243
</Grid>
196244

197245
{this.renderActionWidgets()}
198246

199-
<ActionEditor spaceId={spaceId} />
247+
<ActionEditor spaceId={spaceId} userId={filteredUserId} />
200248
</div>
201249
</Main>
202250
);

src/config/constants.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ export const BLOCK_MATH_REGEX = /(\\\[(.*?)\\])/g;
3939
export const INLINE_MATH_REGEX = /(\\\((.*?)\\\))/g;
4040
export const RADIX = 10;
4141

42-
export const FILTER_ALL_SPACE_ID = 'defaultid';
42+
export const FILTER_ALL_SPACES_ID = 'allSpacesSelectId';
43+
export const FILTER_ALL_USERS_ID = 'allUsersSelectId';
4344
export const DEFAULT_AUTHENTICATION = false;
4445
export const AUTHENTICATED = 'authenticated';
4546

0 commit comments

Comments
 (0)