Skip to content

Commit a01d56e

Browse files
author
Kshitij Raj
committed
notification count in progress
1 parent c417861 commit a01d56e

File tree

6 files changed

+58
-11
lines changed

6 files changed

+58
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"styled-components": "^5.3.9",
3131
"react-timeago": "^7.2.0",
3232
"socket.io-client": "^4.7.2",
33-
"@kshitijraj09/sharedlib_mf": "1.0.6"
33+
"@kshitijraj09/sharedlib_mf": "1.0.7"
3434
},
3535
"devDependencies": {
3636
"@babel/cli": "^7.21.0",

src/components/AuthRoute/SocketInit.tsx renamed to src/Socket/SocketInit.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { memo, useEffect } from "react";
22
import { Socket, io } from "socket.io-client";
3-
import { getUserInfoFromStorage } from "../../util";
3+
import { getUserInfoFromStorage } from "../util";
4+
import { WindowEvents } from "Sharedlib/eventservice";
45

56
declare global {
67
interface Window {
@@ -12,7 +13,7 @@ const SOCKETIOENDPOINT = process.env.APIBASEURL;
1213
let socket: Socket = io(SOCKETIOENDPOINT, {
1314
autoConnect: false
1415
});
15-
16+
const eventName = 'messageNotification' as typeof WindowEvents.messageNotification;
1617
const SocketInit = () => {
1718
const handleSocketConnection = () => {
1819
console.log('socket init called');
@@ -31,6 +32,15 @@ const SocketInit = () => {
3132

3233
useEffect(() => {
3334
handleSocketConnection();
35+
socket.on('message-notification', (notification) => {
36+
console.log('hello notification', notification)
37+
import("Sharedlib/eventservice").
38+
then((event) => {
39+
setTimeout(() => event.default.fire(eventName, { detail: notification }), 100);
40+
}).catch(error => {
41+
console.error('Error occured in event', error);
42+
})
43+
})
3444
return () => {
3545
socket.disconnect();
3646
}
@@ -39,4 +49,4 @@ const SocketInit = () => {
3949
return (<></>)
4050
}
4151

42-
export const MemosizedSocketInit = memo(SocketInit);
52+
export const MemoizedSocketInit = memo(SocketInit);

src/components/AuthRoute/AuthRoute.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {SideMenuBar} from "../Homepage/SideMenuBar";
33
import {useLocation, useNavigate} from "react-router-dom";
44
import { getAccessToken } from "../../util";
55
import Loader from "../Loaders";
6-
import { MemosizedSocketInit as SocketInit } from "./SocketInit";
6+
import { MemoizedSocketInit as SocketInit } from "../../Socket/SocketInit";
77
import { getUserDetailsApi } from "../../apis/getUserDetails";
88
import { WindowEvents } from "Sharedlib/eventservice";
99

src/components/Homepage/SideMenuBar.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ const LogoutModalLazy = lazy(() => import("../Login/LogoutModal"));
2929
import { styled } from "@mui/material/styles";
3030
import { getUserInfoFromStorage } from "../../util";
3131
import { PageEnum } from "../../typesdeclarations/type";
32+
import useNotificationProvider from "../../customHook/useNotificationProvider";
33+
import { NotificationType, WindowEvents } from "@kshitijraj09/sharedlib_mf";
3234

3335
const drawerWidth = 200;
3436
type SideMenuBarPropsType = {
@@ -82,7 +84,9 @@ export const SideMenuBar = ({
8284
const classes = styles();
8385

8486
const [anchorEl, setAnchorEl] = useState<HTMLElement | null>(null);
85-
87+
const messageNotification = 'messageNotification' as WindowEvents.messageNotification;
88+
const { outputStack: notificationStack } = useNotificationProvider<NotificationType>(messageNotification);
89+
8690
const menuItems = [
8791
{
8892
text: PageEnum.Posts,
@@ -102,7 +106,7 @@ export const SideMenuBar = ({
102106
{
103107
text: PageEnum.Messenger,
104108
icon: (
105-
<Badge color="primary" variant="dot">
109+
<Badge color="primary" badgeContent={notificationStack.length}>
106110
<TextsmsOutlinedIcon />
107111
</Badge>
108112
),
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { WindowEvents } from "@kshitijraj09/sharedlib_mf";
2+
import WindowEventService from "Sharedlib/eventservice";
3+
import { useEffect, useState } from "react";
4+
5+
const useNotificationProvider = <Type,>(eventName: WindowEvents) => {
6+
7+
const [detail, setDetail] = useState<Type>();
8+
const [loading, setLoading] = useState(true);
9+
const [outputStack, setOutputStack] = useState<Type[]>([]);
10+
11+
useEffect(() => {
12+
import("Sharedlib/eventservice").
13+
then((event) => {
14+
event.default.subscribe(eventName, updateHandler)
15+
}).catch(error => {
16+
console.error('Error occured in event', error);
17+
})
18+
return () => {
19+
WindowEventService.unsubscribe(eventName, updateHandler)
20+
}
21+
}, []);
22+
23+
const updateHandler = (event: Event) => {
24+
const { detail } = event as CustomEvent;
25+
setDetail(detail);
26+
setOutputStack((prev) => ([detail, ...prev]));
27+
setLoading(false);
28+
}
29+
30+
return {detail, loading, outputStack}
31+
}
32+
33+
export default useNotificationProvider;

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,10 +1196,10 @@
11961196
"@jridgewell/resolve-uri" "^3.1.0"
11971197
"@jridgewell/sourcemap-codec" "^1.4.14"
11981198

1199-
"@kshitijraj09/sharedlib_mf@1.0.6":
1200-
version "1.0.6"
1201-
resolved "https://npm.pkg.github.com/download/@kshitijraj09/sharedlib_mf/1.0.6/de9ee004fb698c71396f4a5dcbaf932e0f003f06#de9ee004fb698c71396f4a5dcbaf932e0f003f06"
1202-
integrity sha512-7I6o9bBjYcbZ9BuVS3kY0cGo7La/S7Sxe+nbfFqEygvarArH8KQAKrxjktScY9cl2adns+dx5Up861ZhANvZ2Q==
1199+
"@kshitijraj09/sharedlib_mf@1.0.7":
1200+
version "1.0.7"
1201+
resolved "https://npm.pkg.github.com/download/@kshitijraj09/sharedlib_mf/1.0.7/1ee8985920271945693d73b5c44f96d2e389e0b5#1ee8985920271945693d73b5c44f96d2e389e0b5"
1202+
integrity sha512-3K/rsSDEVvjEb50nItE/Azwn2zT9RzqjJaJHCaJaJpsnSJ6NVvra9/pN/f/rYNOGMK7YI7JMMD1SHsbUQrhMXQ==
12031203
dependencies:
12041204
"@emotion/react" "^11.10.6"
12051205
"@emotion/styled" "^11.10.6"

0 commit comments

Comments
 (0)