Skip to content

Commit 2e0ea7f

Browse files
Johan BookJohan Book
Johan Book
authored and
Johan Book
committed
feat(web-ui): add blog post event handler
1 parent a7fe896 commit 2e0ea7f

File tree

6 files changed

+47
-14
lines changed

6 files changed

+47
-14
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export abstract class BaseEvent {}
2+
3+
type Constructor<T> = new (...props: any) => T;
4+
5+
export function dispatchEvent<T extends BaseEvent>(event: T) {
6+
const customEvent = new CustomEvent(event.constructor.name, {
7+
detail: event,
8+
});
9+
document.dispatchEvent(customEvent);
10+
}
11+
12+
export function addEventListener<T extends BaseEvent>(
13+
event: Constructor<T>,
14+
onEvent: (event: T) => void
15+
): void {
16+
document.addEventListener(event.name, (event) => {
17+
const customEvent = event as CustomEvent<T>;
18+
onEvent(customEvent.detail);
19+
});
20+
}

services/web-ui/src/core/notifications/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export { NotificationEventsConstants } from "./constants/notification-events.con
22
export { useHandleNotification } from "./hooks/useHandleNotification";
33
export { useNotifications } from "./hooks/useNotifications";
44
export { NotificationProvider } from "./notification.provider";
5+
export { INotification } from "./types/notification.interface";
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { BaseEvent } from "src/core/events";
2+
3+
import { INotification } from "./types/notification.interface";
4+
5+
export class NotificationEvent extends BaseEvent {
6+
constructor(public readonly notification: INotification) {
7+
super();
8+
}
9+
}

services/web-ui/src/core/notifications/notificationEvent.handler.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { v4 as uuid } from "uuid";
44
import { NotificationMetaDetailsWsEventNamesEnum } from "src/api";
55
import { Logger } from "src/core/logging";
66

7+
import { dispatchEvent } from "../events";
78
import { NotificationEventsConstants } from "./constants/notification-events.constants";
9+
import { NotificationEvent } from "./notification.event";
810
import { INotification } from "./types/notification.interface";
911

1012
const eventName = NotificationMetaDetailsWsEventNamesEnum.Notification;
@@ -79,10 +81,8 @@ export class NotificationEventHandler {
7981

8082
this.logger.trace("Received notification", { notification });
8183

82-
const event = new CustomEvent(eventName, {
83-
detail: notification,
84-
});
85-
document.dispatchEvent(event);
84+
const event = new NotificationEvent(notification);
85+
dispatchEvent(event);
8686

8787
let eventWasHandled = false;
8888

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { addEventListener } from "src/core/events";
2+
import { NotificationEvent } from "src/core/notifications/notification.event";
3+
import { CacheKeysConstants } from "src/core/query";
4+
import { QUERY_CLIENT } from "src/queryQlient";
5+
6+
addEventListener(NotificationEvent, (event) => {
7+
const notification = event.notification;
8+
9+
if ((notification.type as any) === "new_blog_post") {
10+
QUERY_CLIENT.invalidateQueries([CacheKeysConstants.BlogPosts]);
11+
}
12+
});

services/web-ui/src/pages/BlogPostListPage/BlogPostListPage.container.tsx

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ import { blogsApi } from "src/apis";
66
import { ErrorMessage } from "src/components/ui/ErrorMessage";
77
import { useTranslation } from "src/core/i18n";
88
import { InteractionObserver } from "src/core/infinite-scroll";
9-
import {
10-
NotificationEventsConstants,
11-
useHandleNotification,
12-
} from "src/core/notifications";
139
import { CacheKeysConstants, useInfiniteQuery } from "src/core/query";
1410

1511
import { BlogPostPageComponent } from "./BlogPostListPage.component";
@@ -22,7 +18,7 @@ const ITEMS_PER_PAGE = 10;
2218
export function BlogPostListPageContainer(): ReactElement {
2319
const { t } = useTranslation("blog");
2420

25-
const { error, data, isLoading, fetchNextPage, hasNextPage, refetch } =
21+
const { error, data, isLoading, fetchNextPage, hasNextPage } =
2622
useInfiniteQuery(
2723
[CacheKeysConstants.BlogPosts],
2824
({ pageParam = 0 }) =>
@@ -41,11 +37,6 @@ export function BlogPostListPageContainer(): ReactElement {
4137
}
4238
);
4339

44-
useHandleNotification({
45-
onNotification: () => refetch(),
46-
type: NotificationEventsConstants.NEW_BLOG_POST,
47-
});
48-
4940
if (error) {
5041
return (
5142
<>

0 commit comments

Comments
 (0)