Skip to content

Commit

Permalink
refetch role list on navigating back after role creation
Browse files Browse the repository at this point in the history
  • Loading branch information
eokoneyo committed Nov 14, 2024
1 parent 289ba33 commit 36b982b
Showing 1 changed file with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
EuiSpacer,
EuiText,
EuiTitle,
useGeneratedHtmlId,
} from '@elastic/eui';
import type { EuiComboBoxOptionOption } from '@elastic/eui';
import type { FC } from 'react';
Expand Down Expand Up @@ -81,10 +82,13 @@ export const PrivilegesRolesForm: FC<PrivilegesRolesFormProps> = (props) => {
const [selectedRoles, setSelectedRoles] = useState<ReturnType<typeof createRolesComboBoxOptions>>(
createRolesComboBoxOptions(defaultSelected)
);
const manageRoleLinkId = useGeneratedHtmlId();
const isEditOperation = useRef(Boolean(defaultSelected.length));

useEffect(() => {
async function fetchRequiredData(spaceId: string) {
const userInvokedPageVisibilityChange = useRef<boolean | null>(null);

const fetchRequiredData = useCallback(
async (spaceId: string) => {
setFetchingDataDeps(true);

const [systemRoles, _kibanaPrivileges] = await invokeClient((clients) =>
Expand All @@ -109,10 +113,29 @@ export const PrivilegesRolesForm: FC<PrivilegesRolesFormProps> = (props) => {
);

setKibanaPrivileges(_kibanaPrivileges);
}
},
[invokeClient]
);

useEffect(() => {
fetchRequiredData(space.id!).finally(() => setFetchingDataDeps(false));
}, [invokeClient, space.id]);
}, [fetchRequiredData, invokeClient, space.id]);

useEffect(() => {
async function visibilityChangeHandler() {
// page just transitioned back to visible state from hidden state caused by user interaction
if (userInvokedPageVisibilityChange.current && !document.hidden) {
await fetchRequiredData(space.id!).finally(() => setFetchingDataDeps(false));
userInvokedPageVisibilityChange.current = null;
}
}

document.addEventListener('visibilitychange', visibilityChangeHandler);

return () => {
document.removeEventListener('visibilitychange', visibilityChangeHandler);
};
}, [fetchRequiredData, invokeClient, space.id]);

const selectedRolesCombinedPrivileges = useMemo(() => {
const combinedPrivilege = new Set(
Expand Down Expand Up @@ -345,12 +368,19 @@ export const PrivilegesRolesForm: FC<PrivilegesRolesFormProps> = (props) => {
<React.Fragment>
{!isEditOperation.current && (
<EuiFormRow
onClick={(event: React.MouseEvent<HTMLFieldSetElement>) => {
// leverage event propagation, check if manage role link element was clicked
if ((event.target as HTMLFieldSetElement).id === manageRoleLinkId) {
userInvokedPageVisibilityChange.current = true;
}
}}
label={i18n.translate(
'xpack.spaces.management.spaceDetails.roles.selectRolesFormRowLabel',
{ defaultMessage: 'Select roles' }
)}
labelAppend={
<EuiLink
id={manageRoleLinkId}
href={getUrlForApp('management', { deepLinkId: 'roles' })}
external={false}
target="_blank"
Expand Down

0 comments on commit 36b982b

Please sign in to comment.