Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/EditSections/EditUserRoles/EditUserRoles.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ function EditUserRoles({ accordionId, form:{ change }, user, setAssignedRoleIds,
<Accordion
label={<Headline size="large" tag="h3"><FormattedMessage id="ui-users.roles.userRoles" /></Headline>}
id={accordionId}
displayWhenClosed={<Badge>{assignedRoleIds[tenantId]?.length}</Badge>}
displayWhenClosed={<Badge>{assignedRoleIds[tenantId]?.length || 0}</Badge>}
>
<Row>
<IfConsortium>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default function UserRolesModal({ isOpen,
if (assignedRoleIds[tenantId]?.includes(id)) {
setAssignedRoleIds({ ...assignedRoleIds, [tenantId]: assignedRoleIds[tenantId].filter(role => role !== id) });
} else {
setAssignedRoleIds({ ...assignedRoleIds, [tenantId]: assignedRoleIds[tenantId] ? assignedRoleIds[tenantId].concat(id) : [id] });
setAssignedRoleIds({ ...assignedRoleIds, [tenantId]: (assignedRoleIds[tenantId] || []).concat(id) });
}
};

Expand All @@ -72,7 +72,7 @@ export default function UserRolesModal({ isOpen,
};

const handleSaveClick = () => {
const sortedAlphabetically = assignedRoleIds[tenantId]
const sortedAlphabetically = (assignedRoleIds[tenantId] || [])
.map(id => {
const foundRole = allRolesMapStructure.get(id);
return { name: foundRole?.name, id: foundRole?.id };
Expand Down Expand Up @@ -106,7 +106,7 @@ export default function UserRolesModal({ isOpen,
<div>
<FormattedMessage
id="ui-users.permissions.modal.total"
values={{ count: assignedRoleIds[tenantId]?.length }}
values={{ count: assignedRoleIds[tenantId]?.length || 0 }}
/>
</div>
<Button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,23 @@ describe('UserRoleModal', () => {
await userEvent.click(submitButton);
expect(mockChangeUserRoles).toHaveBeenCalledWith(['4', '1']);
});

it('should handle toggling a role when assignedRoleIds[tenantId] is undefined', async () => {
const mockChangeUserRoles = jest.fn();

renderComponent({
isOpen: true,
onClose: mockOnClose,
initialRoleIds: {},
changeUserRoles: mockChangeUserRoles,
tenantId,
});

const roleCheckbox = document.querySelector('[name="selected-3"]');
expect(roleCheckbox.checked).toBe(false);

await userEvent.click(roleCheckbox);

expect(roleCheckbox.checked).toBe(true);
});
});
23 changes: 19 additions & 4 deletions src/components/Wrappers/withUserRoles.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import React, { useEffect, useState } from 'react';

import { useStripes, useOkapiKy, useCallout } from '@folio/stripes/core';
import { useQueryClient } from 'react-query';

import {
useStripes,
useOkapiKy,
useCallout,
useNamespace,
} from '@folio/stripes/core';
import isEqual from 'lodash/isEqual';
import isEmpty from 'lodash/isEmpty';
import { useCreateAuthUserKeycloak, useUserAffiliationRoles } from '../../hooks';
import { KEYCLOAK_USER_EXISTANCE } from '../../constants';
import {
KEYCLOAK_USER_EXISTANCE,
USER_AFFILIATION_ROLES_CACHE_KEY,
} from '../../constants';
import { showErrorCallout } from '../../views/UserEdit/UserEditHelpers';

const withUserRoles = (WrappedComponent) => (props) => {
const { okapi } = useStripes();
const [namespace] = useNamespace({ key: USER_AFFILIATION_ROLES_CACHE_KEY });
const queryClient = useQueryClient();
// eslint-disable-next-line react/prop-types
const userId = props.match.params.id;
const [initialAssignedRoleIds, setInitialAssignedRoleIds] = useState({});
Expand Down Expand Up @@ -69,7 +80,11 @@ const withUserRoles = (WrappedComponent) => (props) => {
userId,
roleIds: roleIds[tenantIdKey],
}
}).catch(sendErrorCallout);
})
.then(async () => {
await queryClient.invalidateQueries({ queryKey: [namespace, userId, tenantIdKey] });
})
.catch(sendErrorCallout);
Comment on lines -72 to +87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sonar is grumpy about how deeply nested this is. Let's convert it all to async/await instead of mixing that with promise chaining. I think that'll satisfy Sonar, and it'll be more consistent.

});

await Promise.allSettled(requests);
Expand Down
3 changes: 1 addition & 2 deletions src/components/Wrappers/withUserRoles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ const mockData = {
username: 'testUser',
};

const mockApiPut = jest.fn(() => ({
const mockApiPut = jest.fn(() => Promise.resolve({
json: () => Promise.resolve(),
catch: jest.fn(() => Promise.resolve({ status: 400 })),
}));

const mockKyPut = jest.fn();
Expand Down
2 changes: 2 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,5 @@ export const MODULE_NAME = 'users';
export const CUSTOM_FIELDS_ENTITY_TYPE = 'user';

export const TAGS_SCOPE = 'ui-tags.tags.manage';

export const USER_AFFILIATION_ROLES_CACHE_KEY = 'user-affiliation-roles';
3 changes: 2 additions & 1 deletion src/hooks/useUserAffiliationRoles/useUserAffiliationRoles.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import { useQuery } from 'react-query';
import { useStripes, useOkapiKy, useNamespace } from '@folio/stripes/core';

import useAllRolesData from '../useAllRolesData/useAllRolesData';
import { USER_AFFILIATION_ROLES_CACHE_KEY } from '../../constants';

const DEFAULT = [];

function useUserAffiliationRoles(userId, tenantId) {
const stripes = useStripes();
const [namespace] = useNamespace({ key: 'user-affiliation-roles' });
const [namespace] = useNamespace({ key: USER_AFFILIATION_ROLES_CACHE_KEY });
const ky = useOkapiKy({ tenant: tenantId });

const hasViewRolesPermission = stripes.hasPerm('ui-users.roles.view');
Expand Down
Loading