Skip to content

Commit bc668dc

Browse files
committed
Self initialize drawer global shared state.
1 parent 5c73ced commit bc668dc

File tree

5 files changed

+36
-3
lines changed

5 files changed

+36
-3
lines changed

src/components/NotificationsDrawer/DrawerBell.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const DrawerBell: React.ComponentType<DrawerBellProps> = ({
1818
drawerActions: { toggleDrawerContent },
1919
} = useChrome();
2020
const {
21-
state: { hasUnread },
21+
state: { hasUnread, ready },
2222
} = useNotificationDrawer();
2323
return (
2424
<ToolbarItem className="pf-v6-u-mx-0">
@@ -38,6 +38,7 @@ const DrawerBell: React.ComponentType<DrawerBellProps> = ({
3838
module: './DrawerPanel',
3939
});
4040
}}
41+
isDisabled={!ready}
4142
aria-label="Notifications"
4243
isExpanded={isNotificationDrawerExpanded}
4344
>

src/components/NotificationsDrawer/DrawerPanel.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
NotificationDrawerList,
1313
} from '@patternfly/react-core/dist/dynamic/components/NotificationDrawer';
1414
import { Badge } from '@patternfly/react-core/dist/dynamic/components/Badge';
15+
import Spinner from '@redhat-cloud-services/frontend-components/Spinner';
1516

1617
import orderBy from 'lodash/orderBy';
1718
import { useNavigate } from 'react-router-dom';
@@ -32,7 +33,7 @@ const DrawerPanelBase = ({ toggleDrawer }: DrawerPanelProps) => {
3233
const [isOrgAdmin, setIsOrgAdmin] = useState(false);
3334
const [isFilterDropdownOpen, setIsFilterDropdownOpen] = useState(false);
3435
const {
35-
state,
36+
state: { ready, ...state },
3637
addNotification,
3738
updateNotificationRead,
3839
updateSelectedStatus,
@@ -151,6 +152,10 @@ const DrawerPanelBase = ({ toggleDrawer }: DrawerPanelProps) => {
151152
));
152153
};
153154

155+
if (!ready) {
156+
return <Spinner centered />;
157+
}
158+
154159
return (
155160
<>
156161
<NotificationDrawerHeader
@@ -172,6 +177,7 @@ const DrawerPanelBase = ({ toggleDrawer }: DrawerPanelProps) => {
172177
/>
173178
<BulkSelect
174179
id="notifications-bulk-select"
180+
onSelect={(checked) => selectAllNotifications(checked)}
175181
items={[
176182
{
177183
title: 'Select none (0)',

src/components/NotificationsDrawer/DrawerSingleton.tsx

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Access } from '@redhat-cloud-services/rbac-client';
1+
import { Access, AccessApi } from '@redhat-cloud-services/rbac-client';
2+
import axios from 'axios';
23

34
import { getDateDaysAgo } from '../UtcDate';
45

@@ -12,6 +13,8 @@ import {
1213
NotificationDrawerState,
1314
} from '../../types/Drawer';
1415

16+
const rbacApi = new AccessApi(undefined, '/api/rbac/v1', axios.create());
17+
1518
interface Bundle {
1619
id: string;
1720
name: string;
@@ -26,14 +29,29 @@ const initialState: NotificationDrawerState = {
2629
filterConfig: [],
2730
hasNotificationsPermissions: false,
2831
hasUnread: false,
32+
ready: false,
33+
initializing: false,
2934
};
3035

3136
export class DrawerSingleton {
3237
private static _instance: DrawerSingleton;
3338
private static _subs: { id: string; rerenderer: () => void }[];
3439
private static _state: NotificationDrawerState = initialState;
40+
3541
static subscribe(rerenderer: () => void) {
3642
const id = crypto.randomUUID();
43+
// Run the init procedure if the state is not ready for subscriber
44+
if (!DrawerSingleton._state.initializing && !DrawerSingleton._state.ready) {
45+
DrawerSingleton._state.initializing = true;
46+
rbacApi
47+
.getPrincipalAccess('notifications', undefined, undefined, 1000)
48+
.then(({ data: { data } }) => {
49+
DrawerSingleton.Instance.initialize(true, data);
50+
DrawerSingleton._state.initializing = false;
51+
DrawerSingleton._state.ready = true;
52+
DrawerSingleton._subs.push({ id, rerenderer });
53+
});
54+
}
3755
DrawerSingleton._subs.push({ id, rerenderer });
3856
return id;
3957
}

src/components/NotificationsDrawer/initNotificationScope.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@ import DrawerPanel from './DrawerPanel';
33
import useNotificationDrawer from '../../hooks/useNotificationDrawer';
44
import { DrawerSingleton } from './DrawerSingleton';
55

6+
/**
7+
* @deprecated The subscribe method checks if the state was initialized and if not, it will run the init method.
8+
*/
69
function initNotificationScope() {
710
const scope = getSharedScope();
11+
console.error(
12+
'This module is deprecated and will be removed in the future! Notification state is initialized if a subscription request is detected.'
13+
);
814
scope['@notif-module/drawer'] = {
915
'1.0.0': {
1016
loaded: 1,

src/types/Drawer.ts

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export type NotificationDrawerState = {
2727
filterConfig: FilterConfigItem[];
2828
hasNotificationsPermissions: boolean;
2929
hasUnread: boolean;
30+
ready: boolean;
31+
initializing: boolean;
3032
};
3133

3234
export interface FilterConfigItem {

0 commit comments

Comments
 (0)