Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ryandberger committed Jan 31, 2025
1 parent eaa87eb commit cb6dd05
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 21 deletions.
15 changes: 9 additions & 6 deletions src/components/EditSections/EditUserRoles/EditUserRoles.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function EditUserRoles({ accordionId, form:{ change }, user, setAssignedRoleIds,
} else {
refetch();
}
}, [affiliations, stripes.okapi.tenant, tenantId]);
}, [affiliations, stripes.okapi.tenant, setTenantId, tenantId, refetch]);

const changeUserRoles = (roleIds) => {
change(`assignedRoleIds[${tenantId}]`, roleIds);
Expand All @@ -50,20 +50,20 @@ function EditUserRoles({ accordionId, form:{ change }, user, setAssignedRoleIds,
const listItemsData = useMemo(() => {
if (isEmpty(assignedRoleIds[tenantId]) || isAllRolesDataLoading) return [];

return assignedRoleIds[tenantId].map(i => {
const foundUserRole = allRolesMapStructure.get(i.roleId);
return assignedRoleIds[tenantId].map(roleId => {
const foundUserRole = allRolesMapStructure.get(roleId);

return { name: foundUserRole?.name, id: foundUserRole?.id };
});
}, [assignedRoleIds, isAllRolesDataLoading, allRolesMapStructure]);
}, [assignedRoleIds, isAllRolesDataLoading, allRolesMapStructure, tenantId]);

const unassignAllMessage = <FormattedMessage
id="ui-users.roles.modal.unassignAll.label"
values={{ roles: listItemsData.map(d => d.name).join(', ') }}
/>;

const renderRoleComponent = (fields) => (_, index) => {
const tenantValue = fields.value[tenantId];
const tenantValue = fields.value;
if (isEmpty(tenantValue)) return null;

const roleId = tenantValue[index];
Expand Down Expand Up @@ -106,7 +106,7 @@ function EditUserRoles({ accordionId, form:{ change }, user, setAssignedRoleIds,
return (
<Col xs={12}>
<FieldArray
name="assignedRoleIds"
name={`assignedRoleIds.${tenantId}`}
component={renderUserRolesComponent}
/>
</Col>
Expand Down Expand Up @@ -173,8 +173,11 @@ EditUserRoles.propTypes = {
match: PropTypes.shape({ params: { id: PropTypes.string } }),
accordionId: PropTypes.string,
form: PropTypes.object.isRequired,
user: PropTypes.object.isRequired,
assignedRoleIds: PropTypes.object.isRequired,
setAssignedRoleIds: PropTypes.func.isRequired,
tenantId: PropTypes.string.isRequired,
setTenantId: PropTypes.func.isRequired
};

export default withRouter(EditUserRoles);
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ UserRolesList.propTypes = {
).isRequired,
toggleRole: PropTypes.func.isRequired,
toggleAllRoles: PropTypes.func.isRequired,
tenantId: PropTypes.string.isRequired
};

export default UserRolesList;
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,17 @@ export default function UserRolesModal({ isOpen,

const toggleRole = (id) => {
if (assignedRoleIds[tenantId]?.includes(id)) {
setAssignedRoleIds({...assignedRoleIds, [tenantId]: assignedRoleIds[tenantId].filter(role => role !== id)});
setAssignedRoleIds({ ...assignedRoleIds, [tenantId]: assignedRoleIds[tenantId].filter(role => role !== id) });
} else {
setAssignedRoleIds({...assignedRoleIds, [tenantId]: assignedRoleIds[tenantId].concat(id)});
setAssignedRoleIds({ ...assignedRoleIds, [tenantId]: assignedRoleIds[tenantId].concat(id) });
}
};

const toggleAllRoles = (checked) => {
if (checked) {
setAssignedRoleIds({...assignedRoleIds, [tenantId]: allRolesData?.roles.map(role => role.id)});
}
else {
setAssignedRoleIds({...assignedRoleIds, [tenantId]: []});
setAssignedRoleIds({ ...assignedRoleIds, [tenantId]: allRolesData?.roles.map(role => role.id) });
} else {
setAssignedRoleIds({ ...assignedRoleIds, [tenantId]: [] });
}
};

Expand Down Expand Up @@ -188,4 +187,5 @@ UserRolesModal.propTypes = {
onClose: PropTypes.func.isRequired,
initialRoleIds: PropTypes.object,
changeUserRoles: PropTypes.func.isRequired,
tenantId: PropTypes.string.isRequired,
};
16 changes: 9 additions & 7 deletions src/components/Wrappers/withUserRoles.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useCallback, useEffect, useState } from 'react';
//import { groupBy, keyBy, keys, merge, union, values } from 'lodash';
// import { groupBy, keyBy, keys, merge, union, values } from 'lodash';

import { useStripes, useOkapiKy, useCallout } from '@folio/stripes/core';
import isEqual from 'lodash/isEqual';
Expand Down Expand Up @@ -43,8 +43,8 @@ const withUserRoles = (WrappedComponent) => (props) => {
}).sort((a, b) => a.name.localeCompare(b.name)).map(r => r.id);

setTenantsLoaded(tenantsLoaded.concat(tenantId));
setAssignedRoleIds({...assignedRoleIds, [tenantId]: assignedRoles});
setInitialAssignedRoleIds({...assignedRoleIds, [tenantId]: assignedRoles});
setAssignedRoleIds({ ...assignedRoleIds, [tenantId]: assignedRoles });
setInitialAssignedRoleIds({ ...assignedRoleIds, [tenantId]: assignedRoles });
}, [allRolesMapStructure]);

Check warning on line 48 in src/components/Wrappers/withUserRoles.js

View workflow job for this annotation

GitHub Actions / build-npm

React Hook useCallback has missing dependencies: 'assignedRoleIds', 'tenantId', and 'tenantsLoaded'. Either include them or remove the dependency array. You can also do a functional update 'setTenantsLoaded(t => ...)' if you only need 'tenantsLoaded' in the 'setTenantsLoaded' call

useEffect(() => {
Expand All @@ -64,15 +64,17 @@ const withUserRoles = (WrappedComponent) => (props) => {
[userId, isAllRolesDataLoading, setAssignedRoleIdsOnLoad, tenantId]);

const updateUserRoles = (roleIds) => {
Object.keys(roleIds).forEach(async (tenantIdKey) => {
await api.put(
Object.keys(roleIds).forEach((tenantIdKey) => {
// Using ky so we can override tenant header
ky.put(
`roles/users/${userId}`, { json: {
userId,
roleIds: roleIds[tenantIdKey],
}, headers: { 'X-Okapi-Tenant': tenantIdKey } },
},
headers: { 'X-Okapi-Tenant': tenantIdKey } },
).json()
// eslint-disable-next-line no-console
.catch(sendErrorCallout);
.catch(sendErrorCallout);
});
};

Expand Down
4 changes: 3 additions & 1 deletion src/views/UserEdit/UserEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
find,
omit,
get,
compact,
compact
} from 'lodash';

import { LoadingView } from '@folio/stripes/components';
Expand Down Expand Up @@ -54,6 +54,8 @@ class UserEdit extends React.Component {
*/
assignedRoleIds: PropTypes.object,
setAssignedRoleIds: PropTypes.func,
tenantId: PropTypes.string,
setTenantId: PropTypes.func,
isCreateKeycloakUserConfirmationOpen: PropTypes.bool,
initialAssignedRoleIds: PropTypes.arrayOf(PropTypes.string),
checkAndHandleKeycloakAuthUser: PropTypes.func,
Expand Down
5 changes: 4 additions & 1 deletion src/views/UserEdit/UserForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import { EditCustomFieldsRecord } from '@folio/stripes/smart-components';
import stripesFinalForm from '@folio/stripes/final-form';

import { set } from 'lodash';

Check warning on line 29 in src/views/UserEdit/UserForm.js

View workflow job for this annotation

GitHub Actions / build-npm

'set' is defined but never used. Allowed unused vars must match /React/u
import { USER_TYPES } from '../../constants';
import {
EditUserInfo,
Expand Down Expand Up @@ -110,7 +111,9 @@ class UserForm extends React.Component {
onCancelKeycloakConfirmation: PropTypes.func,
confirmCreateKeycloakUser: PropTypes.func,
setAssignedRoleIds: PropTypes.func,
assignedRoleIds: PropTypes.object
assignedRoleIds: PropTypes.object,
setTenantId: PropTypes.func,
tenantId: PropTypes.string
};

static defaultProps = {
Expand Down

0 comments on commit cb6dd05

Please sign in to comment.