1
- import { Access } from '@redhat-cloud-services/rbac-client' ;
1
+ import { Access , AccessApi } from '@redhat-cloud-services/rbac-client' ;
2
+ import axios from 'axios' ;
2
3
3
4
import { getDateDaysAgo } from '../UtcDate' ;
4
5
@@ -12,6 +13,8 @@ import {
12
13
NotificationDrawerState ,
13
14
} from '../../types/Drawer' ;
14
15
16
+ const rbacApi = new AccessApi ( undefined , '/api/rbac/v1' , axios . create ( ) ) ;
17
+
15
18
interface Bundle {
16
19
id : string ;
17
20
name : string ;
@@ -26,14 +29,29 @@ const initialState: NotificationDrawerState = {
26
29
filterConfig : [ ] ,
27
30
hasNotificationsPermissions : false ,
28
31
hasUnread : false ,
32
+ ready : false ,
33
+ initializing : false ,
29
34
} ;
30
35
31
36
export class DrawerSingleton {
32
37
private static _instance : DrawerSingleton ;
33
38
private static _subs : { id : string ; rerenderer : ( ) => void } [ ] ;
34
39
private static _state : NotificationDrawerState = initialState ;
40
+
35
41
static subscribe ( rerenderer : ( ) => void ) {
36
42
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
+ }
37
55
DrawerSingleton . _subs . push ( { id, rerenderer } ) ;
38
56
return id ;
39
57
}
0 commit comments