Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STCOR-905: based on 'users-keycloak' interface use bl-users or users-keycloak for _self endpoint in useUserTenantPermissions #1556

Merged
merged 12 commits into from
Nov 15, 2024
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* Wait longer before declaring a rotation request to be stale. Refs STCOR-895.
* Send the stored central tenant name in the header on logout. Refs STCOR-900.
* Provide `<IfAnyPermission>` and `stripes.hasAnyPermission()`. Refs STCOR-910.
* Use the `users-keycloak/_self` endpoint conditionally when the `users-keycloak` interface is present; otherwise, use `bl-users/_self` within `useUserTenantPermissions`. Refs STCOR-905.


## [10.2.0](https://github.com/folio-org/stripes-core/tree/v10.2.0) (2024-10-11)
[Full Changelog](https://github.com/folio-org/stripes-core/compare/v10.1.1...v10.2.0)
Expand Down
53 changes: 0 additions & 53 deletions src/hooks/useUserSelfTenantPermissions.js

This file was deleted.

71 changes: 0 additions & 71 deletions src/hooks/useUserSelfTenantPermissions.test.js

This file was deleted.

61 changes: 0 additions & 61 deletions src/hooks/useUserTenantPermissionNames.js

This file was deleted.

72 changes: 0 additions & 72 deletions src/hooks/useUserTenantPermissionNames.test.js

This file was deleted.

56 changes: 33 additions & 23 deletions src/hooks/useUserTenantPermissions.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,51 @@
import { useQuery } from 'react-query';

import { useStripes } from '../StripesContext';
import useUserSelfTenantPermissions from './useUserSelfTenantPermissions';
import useUserTenantPermissionNames from './useUserTenantPermissionNames';
import { useNamespace } from '../components';
import useOkapiKy from '../useOkapiKy';

const INITIAL_DATA = [];

const useUserTenantPermissions = (
{ tenantId },
options = {},
) => {
const stripes = useStripes();
const ky = useOkapiKy();
const api = ky.extend({
hooks: {
beforeRequest: [(req) => req.headers.set('X-Okapi-Tenant', tenantId)]
}
});
const [namespace] = useNamespace({ key: 'user-self-permissions' });

const {
isFetching: isPermissionsFetching,
isFetched: isPermissionsFetched,
isLoading: isPermissionsLoading,
userPermissions: permissionsData = {},
totalRecords: permissionsTotalRecords
} = useUserTenantPermissionNames({ tenantId }, options);
const permPath = stripes.hasInterface('users-keycloak') ? 'users-keycloak' : 'bl-users';

const {
isFetching: isSelfPermissionsFetching,
isFetched: isSelfPermissionsFetched,
isLoading: isSelfPermissionsLoading,
userPermissions:selfPermissionsData = {},
totalRecords: selfPermissionsTotalRecords
} = useUserSelfTenantPermissions({ tenantId }, options);

const isFetching = stripes.hasInterface('roles') ? isSelfPermissionsFetching : isPermissionsFetching;
const isFetched = stripes.hasInterface('roles') ? isSelfPermissionsFetched : isPermissionsFetched;
const isLoading = stripes.hasInterface('roles') ? isSelfPermissionsLoading : isPermissionsLoading;
const userPermissions = stripes.hasInterface('roles') ? selfPermissionsData : permissionsData;
const totalRecords = stripes.hasInterface('roles') ? selfPermissionsTotalRecords : permissionsTotalRecords;
isFetching,
isFetched,
isLoading,
data,
} = useQuery(
[namespace, tenantId],
({ signal }) => {
return api.get(
`${permPath}/_self?expandPermissions=true`,
{ signal },
).json();
},
{
keepPreviousData: true,
...options,
},
);

return ({
isFetching,
isFetched,
isLoading,
userPermissions,
totalRecords
userPermissions: data?.permissions.permissions || INITIAL_DATA,
totalRecords: data?.permissions.permissions.length || 0,
});
};

Expand Down
Loading
Loading