Skip to content

Commit

Permalink
Adjusting visibility of group attach/detach buttons on the edit exerc…
Browse files Browse the repository at this point in the history
…ise page based on ACLs.
  • Loading branch information
krulis-martin committed Nov 3, 2023
1 parent 7ca2981 commit 25a1437
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
22 changes: 16 additions & 6 deletions src/components/Exercises/ExerciseGroups/ExerciseGroups.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Icon, { GroupIcon, LoadingIcon } from '../../icons';
import GroupsNameContainer from '../../../containers/GroupsNameContainer';
import Button from '../../widgets/TheButton';
import GroupsTreeContainer from '../../../containers/GroupsTreeContainer';
import { arrayToObject, identity } from '../../../helpers/common';
import { arrayToObject, identity, EMPTY_MAP } from '../../../helpers/common';

class ExerciseGroups extends Component {
state = { dialogOpen: false };
Expand Down Expand Up @@ -51,12 +51,14 @@ class ExerciseGroups extends Component {
);
};

buttonsCreator = attachedGroupsIds => group => {
return <span>{attachedGroupsIds[group.id] ? this.detachButton(group.id) : this.attachButton(group.id)}</span>;
buttonsCreator = (attachedGroupsIds, groupDataAccessor) => group => {
return (groupDataAccessor(group.id) || EMPTY_MAP).getIn(['permissionHints', 'createExercise'], false) ? (
<span>{attachedGroupsIds[group.id] ? this.detachButton(group.id) : this.attachButton(group.id)}</span>
) : null;
};

render() {
const { groupsIds = [], showButtons = false } = this.props;
const { groupsIds = [], showButtons = false, groupDataAccessor } = this.props;
return (
<Box
title={<FormattedMessage id="app.exercise.groups" defaultMessage="Groups of Residence" />}
Expand All @@ -82,7 +84,11 @@ class ExerciseGroups extends Component {
<td>
<GroupsNameContainer groupId={groupId} fullName translations links admins />
</td>
<td className="text-right">{showButtons && this.detachButton(groupId)}</td>
<td className="text-right">
{showButtons &&
(groupDataAccessor(groupId) || EMPTY_MAP).getIn(['permissionHints', 'createExercise'], false) &&
this.detachButton(groupId)}
</td>
</tr>
))}
</tbody>
Expand All @@ -101,7 +107,10 @@ class ExerciseGroups extends Component {
<Modal.Body>
<GroupsTreeContainer
onlyEditable
buttonsCreator={this.buttonsCreator(arrayToObject(groupsIds, identity, () => true))}
buttonsCreator={this.buttonsCreator(
arrayToObject(groupsIds, identity, () => true),
groupDataAccessor
)}
/>
</Modal.Body>
</Modal>
Expand All @@ -119,6 +128,7 @@ ExerciseGroups.propTypes = {
detachingGroupId: PropTypes.string,
attachExerciseToGroup: PropTypes.func.isRequired,
detachExerciseFromGroup: PropTypes.func.isRequired,
groupDataAccessor: PropTypes.func.isRequired,
};

export default ExerciseGroups;
6 changes: 5 additions & 1 deletion src/pages/EditExercise/EditExercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { fetchByIds } from '../../redux/modules/users';
import { getExercise, getExerciseAttachingGroupId, getExerciseDetachingGroupId } from '../../redux/selectors/exercises';
import { isSubmitting } from '../../redux/selectors/submission';
import { loggedInUserSelector } from '../../redux/selectors/users';
import { getGroupsAdmins } from '../../redux/selectors/groups';
import { getGroupsAdmins, groupDataAccessorSelector } from '../../redux/selectors/groups';

import { getLocalizedTextsInitialValues, transformLocalizedTextsFormData } from '../../helpers/localizedData';
import { safeGet } from '../../helpers/common';
Expand Down Expand Up @@ -117,6 +117,7 @@ class EditExercise extends Component {
attachExerciseToGroup,
detachExerciseFromGroup,
sendNotification,
groupDataAccessor,
} = this.props;

return (
Expand Down Expand Up @@ -158,6 +159,7 @@ class EditExercise extends Component {
detachingGroupId={detachingGroupId}
attachExerciseToGroup={attachExerciseToGroup}
detachExerciseFromGroup={detachExerciseFromGroup}
groupDataAccessor={groupDataAccessor}
/>

<Box title={<FormattedMessage id="app.editExercise.editTags" defaultMessage="Edit Tags" />}>
Expand Down Expand Up @@ -240,6 +242,7 @@ EditExercise.propTypes = {
loggedUser: ImmutablePropTypes.map,
attachingGroupId: PropTypes.string,
detachingGroupId: PropTypes.string,
groupDataAccessor: PropTypes.func.isRequired,
loadAsync: PropTypes.func.isRequired,
reset: PropTypes.func.isRequired,
editExercise: PropTypes.func.isRequired,
Expand All @@ -261,6 +264,7 @@ export default withLinks(
loggedUser: loggedInUserSelector(state),
attachingGroupId: getExerciseAttachingGroupId(exerciseId)(state),
detachingGroupId: getExerciseDetachingGroupId(exerciseId)(state),
groupDataAccessor: groupDataAccessorSelector(state),
}),
(dispatch, { params: { exerciseId } }) => ({
reset: () => dispatch(reset('editExercise')),
Expand Down

0 comments on commit 25a1437

Please sign in to comment.