Skip to content

Commit da6f9b9

Browse files
committed
Moved permission checking out of the VolunteerTables and into the individual components. The components handle the "onlyMe" setting and permissions too.
1 parent 9210c3b commit da6f9b9

File tree

3 files changed

+44
-32
lines changed

3 files changed

+44
-32
lines changed

web-ui/src/components/volunteer/VolunteerEvents.jsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ import DatePickerField from '../date-picker-field/DatePickerField';
1919
import ConfirmationDialog from '../dialogs/ConfirmationDialog';
2020
import OrganizationDialog from '../dialogs/OrganizationDialog'; // Include OrganizationDialog
2121
import { AppContext } from '../../context/AppContext';
22-
import { selectCsrfToken, selectCurrentUser, selectProfileMap } from '../../context/selectors';
22+
import {
23+
selectCsrfToken,
24+
selectCurrentUser,
25+
selectProfileMap,
26+
selectHasVolunteeringEventsPermission,
27+
} from '../../context/selectors';
2328
import { formatDate } from '../../helpers/datetime';
2429

2530
const eventBaseUrl = '/services/volunteer/event';
@@ -322,6 +327,9 @@ const VolunteerEvents = ({ forceUpdate = () => {}, onlyMe = false }) => {
322327
<td>{event.hours}</td>
323328
<td>{event.notes}</td>
324329
<td>
330+
{(member.id == currentUser.id ||
331+
selectHasVolunteeringEventsPermission(state)) &&
332+
<>
325333
<Tooltip title="Edit">
326334
<IconButton aria-label="Edit" onClick={() => editEvent(event)}>
327335
<Edit />
@@ -332,6 +340,7 @@ const VolunteerEvents = ({ forceUpdate = () => {}, onlyMe = false }) => {
332340
<Delete />
333341
</IconButton>
334342
</Tooltip>
343+
</>}
335344
</td>
336345
</tr>
337346
);
@@ -362,9 +371,10 @@ const VolunteerEvents = ({ forceUpdate = () => {}, onlyMe = false }) => {
362371
</thead>
363372
<tbody>{events.map(eventRow)}</tbody>
364373
</table>
374+
{(onlyMe || selectHasVolunteeringEventsPermission(state)) &&
365375
<IconButton aria-label="Add Volunteer Event" classes={{ root: 'add-button' }} onClick={addEvent}>
366376
<AddCircleOutline />
367-
</IconButton>
377+
</IconButton>}
368378
</div>
369379
);
370380
}, [events, organizationMap, profileMap, relationshipMap, sortAscending, sortColumn]);
@@ -536,4 +546,4 @@ const VolunteerEvents = ({ forceUpdate = () => {}, onlyMe = false }) => {
536546

537547
VolunteerEvents.propTypes = propTypes;
538548

539-
export default VolunteerEvents;
549+
export default VolunteerEvents;

web-ui/src/components/volunteer/VolunteerRelationships.jsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ import DatePickerField from '../date-picker-field/DatePickerField';
1919
import ConfirmationDialog from '../dialogs/ConfirmationDialog';
2020
import OrganizationDialog from '../dialogs/OrganizationDialog';
2121
import { AppContext } from '../../context/AppContext';
22-
import { selectCsrfToken, selectCurrentUser, selectProfileMap } from '../../context/selectors';
22+
import {
23+
selectCsrfToken,
24+
selectCurrentUser,
25+
selectProfileMap,
26+
selectHasVolunteeringRelationshipsPermission,
27+
selectHasVolunteeringOrganizationsPermission,
28+
} from '../../context/selectors';
2329
import { formatDate } from '../../helpers/datetime';
2430
import { showError } from '../../helpers/toast';
2531

@@ -269,6 +275,11 @@ const VolunteerRelationships = ({ forceUpdate = () => {}, onlyMe = false }) => {
269275
[sortAscending, sortColumn]
270276
);
271277

278+
const organizationOptions = Object.keys(organizationMap);
279+
if (selectHasVolunteeringOrganizationsPermission(state)) {
280+
organizationOptions.unshift('new');
281+
}
282+
272283
return (
273284
<div id="volunteer-relationships">
274285
{/* Table for showing relationships */}
@@ -301,6 +312,9 @@ const VolunteerRelationships = ({ forceUpdate = () => {}, onlyMe = false }) => {
301312
<td>{relationship.startDate}</td>
302313
<td>{relationship.endDate}</td>
303314
<td>
315+
{(relationship.memberId == currentUser.id ||
316+
selectHasVolunteeringRelationshipsPermission(state)) &&
317+
<>
304318
<Tooltip title="Edit">
305319
<IconButton
306320
aria-label="Edit"
@@ -320,17 +334,19 @@ const VolunteerRelationships = ({ forceUpdate = () => {}, onlyMe = false }) => {
320334
<Delete />
321335
</IconButton>
322336
</Tooltip>
337+
</>}
323338
</td>
324339
</tr>
325340
))}
326341
</tbody>
327342
</table>
343+
{(onlyMe || selectHasVolunteeringRelationshipsPermission(state)) &&
328344
<IconButton
329345
aria-label="Add Volunteer Relationship"
330346
onClick={addRelationship}
331347
>
332348
<AddCircleOutline />
333-
</IconButton>
349+
</IconButton>}
334350
</div>
335351

336352
{/* Message below the table */}
@@ -347,7 +363,7 @@ const VolunteerRelationships = ({ forceUpdate = () => {}, onlyMe = false }) => {
347363
getOptionLabel={(option) =>
348364
option === 'new' ? 'Create a New Organization' : organizationMap[option]?.name || option
349365
}
350-
options={['new', ...Object.keys(organizationMap)]}
366+
options={organizationOptions}
351367
onChange={(event, value) => {
352368
if (value === 'new') {
353369
setRelationshipDialogOpen(false); // Close the relationship dialog
@@ -402,4 +418,4 @@ const VolunteerRelationships = ({ forceUpdate = () => {}, onlyMe = false }) => {
402418

403419
VolunteerRelationships.propTypes = propTypes;
404420

405-
export default VolunteerRelationships;
421+
export default VolunteerRelationships;

web-ui/src/components/volunteer/VolunteerTables.jsx

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@ import PropTypes from 'prop-types';
22
import React, { useContext, useReducer, useState } from 'react';
33
import { Box, Tab, Tabs, Typography, Card, CardHeader, CardContent, Avatar } from '@mui/material';
44

5-
import {
6-
selectHasVolunteeringEventsPermission,
7-
selectHasVolunteeringRelationshipsPermission,
8-
noPermission,
9-
} from '../../context/selectors';
105
import { AppContext } from '../../context/AppContext';
116

127
import Organizations from './Organizations';
@@ -50,14 +45,7 @@ const VolunteerReportPage = ({ onlyMe = false }) => {
5045
const [n, forceUpdate] = useReducer(n => n + 1, 0);
5146
const [tabIndex, setTabIndex] = useState(0);
5247

53-
// React requires that tabs be sequentially numbered. Use these to ensure
54-
// that each tab coinsides with the correct tab content based on the
55-
// individual permissions.
56-
let tabCount = 0;
57-
let tabContent = 0;
58-
59-
return (selectHasVolunteeringEventsPermission(state) ||
60-
selectHasVolunteeringRelationshipsPermission(state)) ? (
48+
return (
6149
<Card className="volunteer-activities-card">
6250
<CardContent className="volunteer-tables">
6351
<Tabs
@@ -67,7 +55,7 @@ const VolunteerReportPage = ({ onlyMe = false }) => {
6755
value={tabIndex}
6856
variant="fullWidth"
6957
>
70-
{selectHasVolunteeringRelationshipsPermission(state) && <Tab
58+
<Tab
7159
label={
7260
<Box sx={{ display: 'flex', alignItems: 'center' }}>
7361
<Avatar sx={{ mr: 1 }}>
@@ -76,13 +64,13 @@ const VolunteerReportPage = ({ onlyMe = false }) => {
7664
<Typography textTransform="none" variant='h5' component='h2'>Volunteer Orgs</Typography>
7765
</Box>
7866
}
79-
{...a11yProps(tabCount++)}
67+
{...a11yProps(0)}
8068
sx={{
8169
minWidth: '150px',
8270
whiteSpace: 'nowrap'
8371
}}
84-
/>}
85-
{selectHasVolunteeringEventsPermission(state) && <Tab
72+
/>
73+
<Tab
8674
label={
8775
<Box sx={{ display: 'flex', alignItems: 'center' }}>
8876
<Avatar sx={{ mr: 1 }}>
@@ -91,31 +79,29 @@ const VolunteerReportPage = ({ onlyMe = false }) => {
9179
<Typography textTransform="none" variant='h5' component='h2'>Events</Typography>
9280
</Box>
9381
}
94-
{...a11yProps(tabCount++)}
82+
{...a11yProps(1)}
9583
sx={{
9684
minWidth: '150px',
9785
whiteSpace: 'nowrap'
9886
}}
99-
/>}
87+
/>
10088
</Tabs>
101-
{selectHasVolunteeringRelationshipsPermission(state) && <TabPanel index={tabContent++} value={tabIndex}>
89+
<TabPanel index={0} value={tabIndex}>
10290
<VolunteerRelationships
10391
forceUpdate={forceUpdate}
10492
key={'vr' + n}
10593
onlyMe={onlyMe}
10694
/>
107-
</TabPanel>}
108-
{selectHasVolunteeringEventsPermission(state) && <TabPanel index={tabContent++} value={tabIndex}>
95+
</TabPanel>
96+
<TabPanel index={1} value={tabIndex}>
10997
<VolunteerEvents
11098
forceUpdate={forceUpdate}
11199
key={'vh' + n}
112100
onlyMe={onlyMe}
113101
/>
114-
</TabPanel>}
102+
</TabPanel>
115103
</CardContent>
116104
</Card>
117-
) : (
118-
<h3>{noPermission}</h3>
119105
);
120106
};
121107

0 commit comments

Comments
 (0)