From a25432769794405e1d4acfb154678b191c82f85b Mon Sep 17 00:00:00 2001 From: rushtong Date: Wed, 30 Oct 2024 11:38:29 -0400 Subject: [PATCH 1/3] feat: fix chairperson edit ability --- src/pages/manage_dac/ManageEditDac.jsx | 176 +++++++++++++------------ 1 file changed, 93 insertions(+), 83 deletions(-) diff --git a/src/pages/manage_dac/ManageEditDac.jsx b/src/pages/manage_dac/ManageEditDac.jsx index bcd49fb2e..bc2d2321b 100644 --- a/src/pages/manage_dac/ManageEditDac.jsx +++ b/src/pages/manage_dac/ManageEditDac.jsx @@ -1,22 +1,21 @@ import * as ld from 'lodash'; -import React, { useEffect, useState } from 'react'; +import React, {useEffect, useState} from 'react'; import AsyncSelect from 'react-select/async'; -import { DAC } from '../../libs/ajax/DAC'; -import { Models } from '../../libs/models'; -import { PromiseSerial } from '../../libs/utils'; -import { Alert } from '../../components/Alert'; -import { Link } from 'react-router-dom'; -import { DacUsers } from './DacUsers'; -import { Notifications } from '../../libs/utils'; +import {DAC} from '../../libs/ajax/DAC'; +import {Models} from '../../libs/models'; +import {PromiseSerial} from '../../libs/utils'; +import {Alert} from '../../components/Alert'; +import {Link} from 'react-router-dom'; +import {DacUsers} from './DacUsers'; +import {Notifications} from '../../libs/utils'; import editDACIcon from '../../images/dac_icon.svg'; import backArrowIcon from '../../images/back_arrow.svg'; -import { Spinner } from '../../components/Spinner'; -import { Styles } from '../../libs/theme'; +import {Spinner} from '../../components/Spinner'; +import {Storage} from '../../libs/storage'; +import {Styles} from '../../libs/theme'; export const CHAIR = 'chair'; export const MEMBER = 'member'; -const CHAIRPERSON = 'Chairperson'; -const ADMIN = 'Admin'; // NOTE: This component is to be removed after the promotion of the Dynamic DAA feature // and is to be replaced by the EditDac component. @@ -45,9 +44,8 @@ export default function ManageEditDac(props) { try { const fetchedDac = await DAC.get(dacId); setFetchedDac(fetchedDac); - setState(prev => ({ ...prev, dac: fetchedDac })); - } - catch(e) { + setState(prev => ({...prev, dac: fetchedDac})); + } catch (e) { Notifications.showError({text: 'Error: Unable to retrieve current DAC from server'}); } } @@ -58,34 +56,32 @@ export default function ManageEditDac(props) { const okHandler = async (event) => { event.preventDefault(); - + const user = Storage.getCurrentUser(); let currentDac = state.dac; if (state.dirtyFlag) { - if (props.location.state.userRole === ADMIN) { - if (dacId !== undefined) { - await DAC.update(currentDac.dacId, currentDac.name, currentDac.description, currentDac.email); - } else { + if (dacId !== undefined) { + await DAC.update(currentDac.dacId, currentDac.name, currentDac.description, currentDac.email); + } else { + if (user.isAdmin) { currentDac = await DAC.create(currentDac.name, currentDac.description, currentDac.email); } + } - // Order here is important. Since users cannot have multiple roles in the - // same DAC, we have to make sure we remove users before re-adding any - // back in a different role. - // Chairs are a special case since we cannot remove all chairs from a DAC - // so we handle that case first. - const ops0 = state.chairIdsToAdd.map(id => () => DAC.removeDacMember(currentDac.dacId, id)); - const ops1 = state.memberIdsToRemove.map(id => () => DAC.removeDacMember(currentDac.dacId, id)); - const ops2 = state.chairIdsToAdd.map(id => () => DAC.addDacChair(currentDac.dacId, id)); - const ops3 = state.chairIdsToRemove.map(id => () => DAC.removeDacChair(currentDac.dacId, id)); - const ops4 = state.memberIdsToAdd.map(id => () => DAC.addDacMember(currentDac.dacId, id)); - const allOperations = ops0.concat(ops1, ops2, ops3, ops4); - const responses = await PromiseSerial(allOperations); - const errorCodes = ld.filter(responses, r => JSON.stringify(r) !== '200' && JSON.stringify(r.status) !== '201'); - if (!ld.isEmpty(errorCodes)) { - handleErrors('There was an error saving DAC information. Please verify that the DAC is correct by viewing the current information.'); - } else { - closeHandler(); - } + // Order here is important. Since users cannot have multiple roles in the + // same DAC, we have to make sure we remove users before re-adding any + // back in a different role. + // Chairs are a special case since we cannot remove all chairs from a DAC + // so we handle that case first. + const ops0 = state.chairIdsToAdd.map(id => () => DAC.removeDacMember(currentDac.dacId, id)); + const ops1 = state.memberIdsToRemove.map(id => () => DAC.removeDacMember(currentDac.dacId, id)); + const ops2 = state.chairIdsToAdd.map(id => () => DAC.addDacChair(currentDac.dacId, id)); + const ops3 = state.chairIdsToRemove.map(id => () => DAC.removeDacChair(currentDac.dacId, id)); + const ops4 = state.memberIdsToAdd.map(id => () => DAC.addDacMember(currentDac.dacId, id)); + const allOperations = ops0.concat(ops1, ops2, ops3, ops4); + const responses = await PromiseSerial(allOperations); + const errorCodes = ld.filter(responses, r => JSON.stringify(r) !== '200' && JSON.stringify(r.status) !== '201'); + if (!ld.isEmpty(errorCodes)) { + handleErrors('There was an error saving DAC information. Please verify that the DAC is correct by viewing the current information.'); } else { closeHandler(); } @@ -146,7 +142,9 @@ export default function ManageEditDac(props) { const userSearch = (invalidUserIds, query, callback) => { DAC.autocompleteUsers(query).then( items => { - const filteredUsers = ld.filter(items, item => { return !invalidUserIds.includes(item.userId); }); + const filteredUsers = ld.filter(items, item => { + return !invalidUserIds.includes(item.userId); + }); const options = filteredUsers.map(function (item) { return { key: item.userId, @@ -250,8 +248,8 @@ export default function ManageEditDac(props) { return ( isLoading ? : -
-
+
+
- +
- +
-
{dacText}
-
{dacId === undefined ? 'Create DAC' : fetchedDac?.name}
+
{dacText}
+
{dacId === undefined ? 'Create DAC' : fetchedDac?.name}
-
-
-
- -
+
+ +
+ +
-
- -
+
+ +