Skip to content

Commit

Permalink
feat: separate out ManageUsersDropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
aarohinadkarni committed May 6, 2024
1 parent 633566f commit 1b1c1d7
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 80 deletions.
5 changes: 2 additions & 3 deletions src/libs/ajax/DAA.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as fp from 'lodash/fp';
import fileDownload from 'js-file-download';
import { getApiUrl, fetchOk } from '../ajax';
import { getApiUrl } from '../ajax';
import { Config } from '../config';
import axios from 'axios';

Expand Down Expand Up @@ -52,7 +51,7 @@ export const DAA = {
const url = `${await getApiUrl()}/api/daa/bulk/user/${userId}`;
const res = await axios.delete(url, { ...Config.authOpts(), data: daaList });
return res.data;
},
},

getDaaFileById: async (daaId, daaFileName) => {
const authOpts = Object.assign(Config.authOpts(), { responseType: 'blob' });
Expand Down
81 changes: 4 additions & 77 deletions src/pages/signing_official_console/ManageResearcherDAAsTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import { DAA } from '../../libs/ajax/DAA';
import {User} from '../../libs/ajax/User';
import { USER_ROLES } from '../../libs/utils';
import { Button } from '@mui/material';
import ManageUsersDropdown from './ManageUsersDropdown';

//Styles specific to this table
const styles = {
Expand Down Expand Up @@ -111,68 +111,7 @@ const DAACell = (
};
};

const dropdown = (applyAll, removeAll, handleApplyAllChange, handleRemoveAllChange, handleApplyAll, actionsTitle, option1, option2, download) => {
const name = download ? 'users' : 'daa';
return (
<ul className="dropdown-menu" role="menu" style={{ padding: '20px', textTransform:'none'}}>
<th id="link_signOut" style={{display:'flex', padding: '5px', textAlign: 'left'}}>
<strong>{actionsTitle}</strong>
</th>
<form>
<li style={{paddingTop: '5px', paddingBottom: '5px'}}>
<label style={{fontWeight: 'normal', whiteSpace: 'nowrap'}}>
<input type="radio" name={name} value="apply" checked={applyAll} onChange={handleApplyAllChange}/>
&nbsp;&nbsp;{option1}
</label>
</li>
<li style={{paddingTop: '5px', paddingBottom: '5px'}}>
<label style={{fontWeight: 'normal', whiteSpace: 'nowrap' }}>
<input type="radio" name={name} value="remove" checked={removeAll} onChange={handleRemoveAllChange}/>
&nbsp;&nbsp;{option2}
</label>
</li>
</form>
<li style={{paddingTop: '5px', paddingBottom: '5px'}}>
<Button style={{
fontSize: '15px',
fontWeight: 'normal',
fontFamily: 'Montserrat',
border: '1px solid #0948B7',
borderRadius: '5px',
height: '40px',
marginRight: '1em',
cursor: 'pointer',
color: '#0948B7',
padding: '10px 20px',
textTransform: 'none'
}} onClick={() => handleApplyAll()}>Apply</Button>
</li>
</ul>
);
};

const displayNameCell = (displayName, email, id, daas, handleApplyAllDaaChange, handleRemoveAllDaaChange, applyAllDaa, removeAllDaa, setResearchers) => {
const handleApplyAllDaa = async () => {
const daaList = { 'daaList': daas.map(daa => daa.daaId) };
if (applyAllDaa) {
try {
await DAA.bulkAddDaasToUser(id, daaList);
Notifications.showSuccess({text: `Approved access to request data from all DACs to user: ${displayName}`});
refreshResearchers(setResearchers);
} catch(error) {
Notifications.showError({text: `Error approving access to request data from all DACs to user: ${displayName}`});
}
} else if (removeAllDaa) {
try {
await DAA.bulkRemoveDaasFromUser(id, daaList);
Notifications.showSuccess({text: `Removed approval of access to request data from all DACs from user: ${displayName}`});
refreshResearchers(setResearchers);
} catch(error) {
Notifications.showError({text: `Error removing approval of access to request data from all DACs from user: ${displayName}`});
}
}
};

const displayNameCell = (displayName, email, id, daas, setResearchers) => {
return {
data: (
<>
Expand All @@ -184,7 +123,7 @@ const displayNameCell = (displayName, email, id, daas, handleApplyAllDaaChange,
<small><a href={`mailto:${email}`}>{email || '- -'}</a></small>
</div>
</a>
{dropdown(applyAllDaa, removeAllDaa, handleApplyAllDaaChange, handleRemoveAllDaaChange, handleApplyAllDaa, 'Agreement Actions', 'Apply all agreements to this user', 'Remove all agreements from this user', false, false)}
<ManageUsersDropdown daas={daas} refreshResearchers={refreshResearchers} setResearchers={setResearchers} moreData={{id: id, name: displayName}}/>
</li>
</>
),
Expand All @@ -203,8 +142,6 @@ export default function ManageResearcherDAAsTable(props) {
const [filteredResearchers, setFilteredResearchers] = useState([]);
const [visibleResearchers, setVisibleResearchers] = useState([]);
const searchRef = useRef('');
const [applyAllDaa, setApplyAllDaa] = useState(false);
const [removeAllDaa, setRemoveAllDaa] = useState(false);
const { signingOfficial, isLoading, dacs, daas } = props;

const headers = (dacs) => {
Expand All @@ -230,16 +167,6 @@ export default function ManageResearcherDAAsTable(props) {
);
}, [researchers]);

const handleApplyAllDaaChange = (event) => {
setApplyAllDaa(event.target.checked);
setRemoveAllDaa(!event.target.checked);
};

const handleRemoveAllDaaChange = (event) => {
setRemoveAllDaa(event.target.checked);
setApplyAllDaa(!event.target.checked);
};

//init hook
useEffect(() => {
const init = async() => {
Expand Down Expand Up @@ -299,7 +226,7 @@ export default function ManageResearcherDAAsTable(props) {
const email = researcher.email || libraryCard.userEmail;
const id = researcher.userId || email;
return [
displayNameCell(displayName, email, id, daas, handleApplyAllDaaChange, handleRemoveAllDaaChange, applyAllDaa, removeAllDaa, setResearchers),
displayNameCell(displayName, email, id, daas, setResearchers),
...dacs.map(dac => DAACell(dac, researcher, signingOfficial.institutionId, daas, refreshResearchers, setResearchers))
];
});
Expand Down
78 changes: 78 additions & 0 deletions src/pages/signing_official_console/ManageUsersDropdown.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import React, { useState } from 'react';
import { Button } from '@mui/material';
import { DAA } from '../../libs/ajax/DAA';
import { Notifications } from '../../libs/utils';

export default function ManageUsersDropdown(props) {
const [applyAll, setApplyAll] = useState(false);
const [removeAll, setRemoveAll] = useState(false);
const {daas, refreshResearchers, setResearchers, moreData} = props;

const handleApplyAllChange = (event) => {
setApplyAll(event.target.checked);
setRemoveAll(!event.target.checked);
};

const handleRemoveAllChange = (event) => {
setRemoveAll(event.target.checked);
setApplyAll(!event.target.checked);
};

const handleApplyAll = async () => {
const daaList = { 'daaList': daas.map(daa => daa.daaId) };
if (applyAll) {
try {
await DAA.bulkAddDaasToUser(moreData.id, daaList);
Notifications.showSuccess({text: `Approved access to request data from all DACs to user: ${moreData.name}`});
refreshResearchers(setResearchers);
} catch(error) {
Notifications.showError({text: `Error approving access to request data from all DACs to user: ${moreData.name}`});
}
} else if (removeAll) {
try {
await DAA.bulkRemoveDaasFromUser(moreData.id, daaList);
Notifications.showSuccess({text: `Removed approval of access to request data from all DACs from user: ${moreData.name}`});
refreshResearchers(setResearchers);
} catch(error) {
Notifications.showError({text: `Error removing approval of access to request data from all DACs from user: ${moreData.name}`});
}
}
};

return (
<ul className="dropdown-menu" role="menu" style={{ padding: '20px', textTransform:'none'}}>
<th id="link_signOut" style={{display:'flex', padding: '5px', textAlign: 'left'}}>
<strong>Agreement Actions</strong>
</th>
<form>
<li style={{paddingTop: '5px', paddingBottom: '5px'}}>
<label style={{fontWeight: 'normal', whiteSpace: 'nowrap'}}>
<input type="radio" name="users" value="apply" checked={applyAll} onChange={handleApplyAllChange}/>
&nbsp;&nbsp;Apply all agreements to this user
</label>
</li>
<li style={{paddingTop: '5px', paddingBottom: '5px'}}>
<label style={{fontWeight: 'normal', whiteSpace: 'nowrap' }}>
<input type="radio" name="users" value="remove" checked={removeAll} onChange={handleRemoveAllChange}/>
&nbsp;&nbsp;Remove all agreements from this user
</label>
</li>
</form>
<li style={{paddingTop: '5px', paddingBottom: '5px'}}>
<Button style={{
fontSize: '15px',
fontWeight: 'normal',
fontFamily: 'Montserrat',
border: '1px solid #0948B7',
borderRadius: '5px',
height: '40px',
marginRight: '1em',
cursor: 'pointer',
color: '#0948B7',
padding: '10px 20px',
textTransform: 'none'
}} onClick={() => handleApplyAll()}>Apply</Button>
</li>
</ul>
);
}

0 comments on commit 1b1c1d7

Please sign in to comment.