-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementing button for (un)setting the group exam flag.
- Loading branch information
1 parent
966a04c
commit 4f096a9
Showing
12 changed files
with
310 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { FormattedMessage } from 'react-intl'; | ||
import { GroupIcon, LoadingIcon } from '../../icons'; | ||
import Button from '../../widgets/TheButton'; | ||
|
||
const ExamGroupButton = ({ exam, pending, disabled = false, setExamFlag, ...props }) => ( | ||
<Button {...props} variant={disabled ? 'secondary' : 'warning'} onClick={setExamFlag} disabled={pending || disabled}> | ||
{pending ? <LoadingIcon gapRight /> : <GroupIcon exam={!exam} gapRight />} | ||
{exam ? ( | ||
<FormattedMessage id="app.groupTypeButton.regular" defaultMessage="Regular" /> | ||
) : ( | ||
<FormattedMessage id="app.groupTypeButton.exam" defaultMessage="Exam" /> | ||
)} | ||
</Button> | ||
); | ||
|
||
ExamGroupButton.propTypes = { | ||
exam: PropTypes.bool.isRequired, | ||
pending: PropTypes.bool.isRequired, | ||
disabled: PropTypes.bool, | ||
setExamFlag: PropTypes.func.isRequired, | ||
}; | ||
|
||
export default ExamGroupButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import ExamGroupButton from './ExamGroupButton'; | ||
export default ExamGroupButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/containers/ExamGroupButtonContainer/ExamGroupButtonContainer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import ImmutablePropTypes from 'react-immutable-proptypes'; | ||
import { connect } from 'react-redux'; | ||
import { injectIntl } from 'react-intl'; | ||
import { defaultMemoize } from 'reselect'; | ||
|
||
import ExamGroupButton from '../../components/buttons/ExamGroupButton'; | ||
import { setExamFlag } from '../../redux/modules/groups'; | ||
import { groupSelector, groupTypePendingChange } from '../../redux/selectors/groups'; | ||
import ResourceRenderer from '../../components/helpers/ResourceRenderer'; | ||
import { getErrorMessage } from '../../locales/apiErrorMessages'; | ||
import { addNotification } from '../../redux/modules/notifications'; | ||
|
||
const setExamFlagHandlingErrors = defaultMemoize( | ||
(exam, setExamFlag, addNotification, formatMessage) => () => | ||
setExamFlag(!exam).catch(err => { | ||
addNotification(getErrorMessage(formatMessage)(err), false); | ||
}) | ||
); | ||
|
||
const ExamGroupButtonContainer = ({ | ||
group, | ||
pending, | ||
setExamFlag, | ||
addNotification, | ||
intl: { formatMessage }, | ||
...props | ||
}) => ( | ||
<ResourceRenderer resource={group}> | ||
{({ exam, organizational, childGroups, permissionHints }) => ( | ||
<ExamGroupButton | ||
exam={exam} | ||
pending={pending} | ||
setExamFlag={setExamFlagHandlingErrors(exam, setExamFlag, addNotification, formatMessage)} | ||
disabled={!permissionHints.setExamFlag || organizational || childGroups.length > 0} | ||
{...props} | ||
/> | ||
)} | ||
</ResourceRenderer> | ||
); | ||
|
||
ExamGroupButtonContainer.propTypes = { | ||
id: PropTypes.string.isRequired, | ||
group: ImmutablePropTypes.map, | ||
pending: PropTypes.bool.isRequired, | ||
setExamFlag: PropTypes.func.isRequired, | ||
addNotification: PropTypes.func.isRequired, | ||
intl: PropTypes.object, | ||
}; | ||
|
||
const mapStateToProps = (state, { id }) => ({ | ||
group: groupSelector(state, id), | ||
pending: groupTypePendingChange(state, id), | ||
}); | ||
|
||
const mapDispatchToProps = (dispatch, { id }) => ({ | ||
setExamFlag: value => dispatch(setExamFlag(id, value)), | ||
addNotification: (...args) => dispatch(addNotification(...args)), | ||
}); | ||
|
||
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(ExamGroupButtonContainer)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import ExamGroupButtonContainer from './ExamGroupButtonContainer'; | ||
export default ExamGroupButtonContainer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.