Skip to content

Commit 5c3de38

Browse files
refactor: use useGlobalAnnouncement
1 parent 45c4150 commit 5c3de38

File tree

4 files changed

+10
-41
lines changed

4 files changed

+10
-41
lines changed

src/components/App/app-top-bar.tsx

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import {
1515
useState,
1616
} from 'react';
1717
import { capitalize, Tab, Tabs, useTheme } from '@mui/material';
18-
import { Groups, ManageAccounts, PeopleAlt, NotificationImportant } from '@mui/icons-material';
19-
import { fetchAppsMetadata, logout, Metadata, TopBar, useNotificationsListener } from '@gridsuite/commons-ui';
18+
import { Groups, ManageAccounts, NotificationImportant, PeopleAlt } from '@mui/icons-material';
19+
import { fetchAppsMetadata, logout, Metadata, TopBar, useGlobalAnnouncement } from '@gridsuite/commons-ui';
2020
import { useParameterState } from '../parameters';
2121
import { APP_NAME, PARAM_LANGUAGE, PARAM_THEME } from '../../utils/config-params';
2222
import { NavLink, type To, useMatches, useNavigate } from 'react-router';
@@ -29,7 +29,6 @@ import AppPackage from '../../../package.json';
2929
import { AppState } from '../../redux/reducer';
3030
import { AppDispatch } from '../../redux/store';
3131
import { MainPaths } from '../../routes/utils';
32-
import { NOTIFICATIONS_URL_KEYS } from '../../utils/notifications-provider';
3332

3433
const tabs = new Map<MainPaths, ReactElement>([
3534
[
@@ -112,31 +111,7 @@ const AppTopBar: FunctionComponent = () => {
112111

113112
const [appsAndUrls, setAppsAndUrls] = useState<Metadata[]>([]);
114113

115-
const [announcementInfos, setAnnouncementInfos] = useState<AnnouncementProps | null>(null);
116-
117-
useNotificationsListener(NOTIFICATIONS_URL_KEYS.GLOBAL_CONFIG, {
118-
listenerCallbackMessage: (event) => {
119-
const eventData = JSON.parse(event.data);
120-
if (eventData.headers.messageType === 'announcement') {
121-
if (
122-
announcementInfos != null &&
123-
announcementInfos.announcementId === eventData.headers.announcementId
124-
) {
125-
// If we receive a notification for an announcement that we already received we ignore it
126-
return;
127-
}
128-
const announcement = {
129-
announcementId: eventData.headers.announcementId,
130-
message: eventData.payload,
131-
severity: eventData.headers.severity,
132-
duration: eventData.headers.duration,
133-
} as AnnouncementProps;
134-
setAnnouncementInfos(announcement);
135-
} else if (eventData.headers.messageType === 'cancelAnnouncement') {
136-
setAnnouncementInfos(null);
137-
}
138-
},
139-
});
114+
const announcementInfos = useGlobalAnnouncement(user);
140115

141116
useEffect(() => {
142117
if (user !== null) {

src/pages/banners/add-announcement-form.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ const AddAnnouncementForm: FunctionComponent<AddAnnouncementProps> = ({ onAnnoun
9898
{...register('startDate')}
9999
name={START_DATE}
100100
label={intl.formatMessage({ id: 'banners.table.startDate' })}
101-
onChange={(newValue) => setValue('startDate', newValue)}
101+
onChange={(newValue) => setValue('startDate', newValue?.toISOString() ?? '')}
102102
/>
103103
</LocalizationProvider>
104104
</Grid>
@@ -108,7 +108,7 @@ const AddAnnouncementForm: FunctionComponent<AddAnnouncementProps> = ({ onAnnoun
108108
{...register('endDate')}
109109
name={END_DATE}
110110
label={intl.formatMessage({ id: 'banners.table.endDate' })}
111-
onChange={(newValue) => setValue('endDate', newValue)}
111+
onChange={(newValue) => setValue('endDate', newValue?.toISOString() ?? '')}
112112
/>
113113
</LocalizationProvider>
114114
</Grid>

src/services/user-admin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ export type Announcement = {
306306
export function addAnnouncement(announcement: Announcement): Promise<void> {
307307
console.debug(`Creating announcement ...`);
308308
return backendFetch(
309-
`${USER_ADMIN_URL}/announcements/${announcement.id}?startDate=${announcement.startDate}&endDate=${announcement.endDate}&severity=${announcement.severity}`,
309+
`${USER_ADMIN_URL}/announcements?startDate=${announcement.startDate}&endDate=${announcement.endDate}&severity=${announcement.severity}`,
310310
{
311311
method: 'post',
312312
headers: {

src/utils/notifications-provider.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,7 @@ import { useSelector } from 'react-redux';
1010
import type { AppState } from '../redux/reducer';
1111
import { getUrlWithToken, getWsBase } from './api-ws';
1212
import { APP_NAME } from './config-params';
13-
14-
export enum NOTIFICATIONS_URL_KEYS {
15-
CONFIG = 'CONFIG',
16-
GLOBAL_CONFIG = 'GLOBAL_CONFIG',
17-
}
18-
19-
export const PREFIX_CONFIG_NOTIFICATION_WS = '/config-notification';
13+
import { NotificationsUrlKeys, PREFIX_CONFIG_NOTIFICATION_WS } from '@gridsuite/commons-ui';
2014

2115
export function useNotificationsUrlGenerator() {
2216
// The websocket API doesn't allow relative urls
@@ -28,17 +22,17 @@ export function useNotificationsUrlGenerator() {
2822
return useMemo(
2923
() =>
3024
({
31-
[NOTIFICATIONS_URL_KEYS.CONFIG]: tokenId
25+
[NotificationsUrlKeys.CONFIG]: tokenId
3226
? getUrlWithToken(
3327
`${wsBase}${PREFIX_CONFIG_NOTIFICATION_WS}/notify?${new URLSearchParams({
3428
appName: APP_NAME,
3529
})}`
3630
)
3731
: undefined,
38-
[NOTIFICATIONS_URL_KEYS.GLOBAL_CONFIG]: tokenId
32+
[NotificationsUrlKeys.GLOBAL_CONFIG]: tokenId
3933
? getUrlWithToken(`${wsBase}${PREFIX_CONFIG_NOTIFICATION_WS}/global`)
4034
: undefined,
41-
}) satisfies Record<NOTIFICATIONS_URL_KEYS, string | undefined>,
35+
}) satisfies { [Key in NotificationsUrlKeys]?: string | undefined },
4236
[wsBase, tokenId]
4337
);
4438
}

0 commit comments

Comments
 (0)