-
Notifications
You must be signed in to change notification settings - Fork 7
/
ExamUnlockButtonContainer.js
56 lines (49 loc) · 1.88 KB
/
ExamUnlockButtonContainer.js
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
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { injectIntl } from 'react-intl';
import { defaultMemoize } from 'reselect';
import ExamUnlockButton from '../../components/buttons/ExamUnlockButton';
import { unlockStudentFromExam } from '../../redux/modules/groups';
import { groupPendingUserUnlock } from '../../redux/selectors/groups';
import { loggedInUserSelector } from '../../redux/selectors/users';
import { getErrorMessage } from '../../locales/apiErrorMessages';
import { addNotification } from '../../redux/modules/notifications';
const unlockStudentHandlingErrors = defaultMemoize(
(unlockStudentFromExam, addNotification, formatMessage) => () =>
unlockStudentFromExam().catch(err => {
addNotification(getErrorMessage(formatMessage)(err), false);
})
);
const ExamUnlockButtonContainer = ({
groupId,
userId,
pending = null,
unlockStudentFromExam,
addNotification,
intl: { formatMessage },
...props
}) => (
<ExamUnlockButton
{...props}
pending={pending}
unlockUserForExam={unlockStudentHandlingErrors(unlockStudentFromExam, addNotification, formatMessage)}
/>
);
ExamUnlockButtonContainer.propTypes = {
groupId: PropTypes.string.isRequired,
userId: PropTypes.string.isRequired,
pending: PropTypes.string,
unlockStudentFromExam: PropTypes.func.isRequired,
addNotification: PropTypes.func.isRequired,
intl: PropTypes.object,
};
const mapStateToProps = (state, { groupId }) => ({
pending: groupPendingUserUnlock(state, groupId),
currentUser: loggedInUserSelector(state),
});
const mapDispatchToProps = (dispatch, { groupId, userId }) => ({
unlockStudentFromExam: () => dispatch(unlockStudentFromExam(groupId, userId)),
addNotification: (...args) => dispatch(addNotification(...args)),
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(ExamUnlockButtonContainer));