Skip to content

Commit 2a4f3c4

Browse files
author
Kshitij Raj
committed
updated notification configuration and zustand implemented
1 parent a01d56e commit 2a4f3c4

File tree

16 files changed

+229
-98
lines changed

16 files changed

+229
-98
lines changed

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,29 @@
1818
"@emotion/react": "^11.10.6",
1919
"@emotion/styled": "^11.10.6",
2020
"@fontsource/roboto": "^4.5.8",
21+
"@kshitijraj09/sharedlib_mf": "1.0.9",
2122
"@mui/icons-material": "^5.11.16",
2223
"@mui/lab": "^5.0.0-alpha.127",
2324
"@mui/material": "^5.12.0",
2425
"@mui/styled-engine-sc": "^5.12.0",
2526
"axios": "^1.3.5",
2627
"react": "^18.2.0",
2728
"react-dom": "^18.2.0",
29+
"react-error-boundary": "^4.0.11",
2830
"react-hook-form": "^7.43.9",
2931
"react-router-dom": "^6.10.0",
30-
"styled-components": "^5.3.9",
3132
"react-timeago": "^7.2.0",
3233
"socket.io-client": "^4.7.2",
33-
"@kshitijraj09/sharedlib_mf": "1.0.7"
34+
"styled-components": "^5.3.9",
35+
"zustand": "^4.4.7"
3436
},
3537
"devDependencies": {
3638
"@babel/cli": "^7.21.0",
3739
"@babel/core": "^7.21.4",
3840
"@babel/preset-env": "^7.21.4",
3941
"@babel/preset-react": "^7.18.6",
4042
"@types/react": "^18.2.0",
43+
"@types/react-timeago": "^4.1.5",
4144
"babel-loader": "^9.1.2",
4245
"css-loader": "^6.7.3",
4346
"dotenv-webpack": "^8.0.1",
@@ -47,7 +50,6 @@
4750
"typescript": "^5.0.4",
4851
"webpack": "^5.78.0",
4952
"webpack-cli": "^5.0.1",
50-
"webpack-dev-server": "^4.13.2",
51-
"@types/react-timeago": "^4.1.5"
53+
"webpack-dev-server": "^4.13.2"
5254
}
5355
}

src/Socket/SocketInit.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import React, { memo, useEffect } from "react";
22
import { Socket, io } from "socket.io-client";
33
import { getUserInfoFromStorage } from "../util";
4-
import { WindowEvents } from "Sharedlib/eventservice";
4+
import { WindowEvents } from "@kshitijraj09/sharedlib_mf";
5+
import useNotificationStore from "../zustand-config/notificationStore";
56

67
declare global {
78
interface Window {
@@ -13,10 +14,10 @@ const SOCKETIOENDPOINT = process.env.APIBASEURL;
1314
let socket: Socket = io(SOCKETIOENDPOINT, {
1415
autoConnect: false
1516
});
16-
const eventName = 'messageNotification' as typeof WindowEvents.messageNotification;
17-
const SocketInit = () => {
17+
const eventName: WindowEvents = 'messageNotification';
18+
const useSocketInit = () => {
19+
const { increaseNotifications , fetchNotifications } = useNotificationStore();
1820
const handleSocketConnection = () => {
19-
console.log('socket init called');
2021
if (!socket.connected) {
2122
const userInfo = JSON.parse(getUserInfoFromStorage());
2223
socket.connect();
@@ -32,8 +33,9 @@ const SocketInit = () => {
3233

3334
useEffect(() => {
3435
handleSocketConnection();
36+
fetchNotifications();
3537
socket.on('message-notification', (notification) => {
36-
console.log('hello notification', notification)
38+
increaseNotifications(notification);
3739
import("Sharedlib/eventservice").
3840
then((event) => {
3941
setTimeout(() => event.default.fire(eventName, { detail: notification }), 100);
@@ -46,7 +48,7 @@ const SocketInit = () => {
4648
}
4749
}, [])
4850

49-
return (<></>)
51+
return {}
5052
}
5153

52-
export const MemoizedSocketInit = memo(SocketInit);
54+
export default useSocketInit;

src/apis/apiconstants.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ export const unfollowUserAPIUrl = `${followApiRoute}/unfollowuser`;
1616

1717
//People API constants
1818
export const peopleApiRoute = '/user';
19-
export const getUserDetailsAPIUrl = `${peopleApiRoute}/getUserDetails`;
19+
export const getUserDetailsAPIUrl = `${peopleApiRoute}/getUserDetails`;
20+
21+
//Notification API constants
22+
export const notificationApiRoute = '/notification';

src/apis/getNotifications.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { notificationApiRoute } from "./apiconstants";
2+
import { axiosInstance } from "./axiosInstance";
3+
4+
export const getNotificationsAPI = async () => {
5+
try {
6+
const { data } = await axiosInstance.get(notificationApiRoute);
7+
return data;
8+
} catch (response ) {
9+
console.log(response);
10+
}
11+
}

src/apis/getUserDetails.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { axiosInstance } from "./axiosInstance";
33

44
export const getUserDetailsApi = async () => {
55
try {
6-
const { data } = await axiosInstance.get(getUserDetailsAPIUrl);
7-
return data;
8-
} catch (error) {
9-
console.log(error);
6+
const { data } = await axiosInstance.get(getUserDetailsAPIUrl);
7+
return data;
8+
} catch (response ) {
9+
console.log(response);
1010
}
1111
}

src/components/AuthRoute/AuthRoute.tsx

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,34 @@
11
import React, {useEffect, useState, Suspense} from "react";
22
import {SideMenuBar} from "../Homepage/SideMenuBar";
33
import {useLocation, useNavigate} from "react-router-dom";
4-
import { getAccessToken } from "../../util";
4+
import { getAccessToken, getCurrentPage, getUserDetailsHelper } from "../../util";
55
import Loader from "../Loaders";
6-
import { MemoizedSocketInit as SocketInit } from "../../Socket/SocketInit";
7-
import { getUserDetailsApi } from "../../apis/getUserDetails";
8-
import { WindowEvents } from "Sharedlib/eventservice";
9-
106
type AuthRouteProps = {
117
Component: React.ComponentType;
128
};
139

1410
export const AuthRoute = ({Component}: AuthRouteProps) => {
1511
const navigate = useNavigate();
1612
const [isAuthenticated, setIsAuthticated] = useState(false);
17-
const [selectedPage, setSelectedPage] = useState<string>('')
18-
const eventName = 'currentUser' as typeof WindowEvents.currentUser;
19-
const {pathname} = useLocation();
20-
console.log('process.env.APIBASEURL', process.env.APIBASEURL, pathname);
13+
const [selectedPage, setSelectedPage] = useState<string>(() => getCurrentPage());
14+
const { pathname } = useLocation();
15+
console.log('process.env.APIBASEURL', process.env.APIBASEURL);
2116

2217
useEffect(() => {
2318
const userToken = getAccessToken();
24-
if (!userToken) {
19+
if (!userToken) {
2520
navigate("/");
2621
}
27-
if (pathname.includes('homepage')) {
22+
setIsAuthticated(true);
23+
if (isAuthenticated && selectedPage.includes('homepage')) {
2824
getUserDetailsHelper();
2925
}
30-
setIsAuthticated(true);
3126
}, [isAuthenticated, selectedPage]);
3227

3328
useEffect(() => {
34-
const pathsplit = pathname.split('/')
35-
const currentPath = pathsplit[pathsplit.length - 1];
36-
setSelectedPage(currentPath);
37-
}, [pathname])
29+
setSelectedPage(getCurrentPage());
30+
}, [pathname]);
3831

39-
const getUserDetailsHelper = async() => {
40-
let userInfo = await getUserDetailsApi();
41-
import("Sharedlib/eventservice").
42-
then((event) => {
43-
setTimeout(() => event.default.fire(eventName, { detail: userInfo }), 100);
44-
}).catch(error => {
45-
console.error('Error occured in event', error);
46-
})
47-
}
4832
return (
4933
<div>
5034
{isAuthenticated && (
@@ -54,7 +38,6 @@ export const AuthRoute = ({Component}: AuthRouteProps) => {
5438
<Component />
5539
</Suspense>
5640
</SideMenuBar>
57-
<SocketInit />
5841
</>
5942
)}
6043
</div>

src/components/Homepage/SideMenuBar.tsx

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ import { getUserInfoFromStorage } from "../../util";
3131
import { PageEnum } from "../../typesdeclarations/type";
3232
import useNotificationProvider from "../../customHook/useNotificationProvider";
3333
import { NotificationType, WindowEvents } from "@kshitijraj09/sharedlib_mf";
34+
import useNotificationStore from "../../zustand-config/notificationStore";
35+
import useSocketInit from "../../Socket/SocketInit";
36+
3437

3538
const drawerWidth = 200;
3639
type SideMenuBarPropsType = {
@@ -63,7 +66,14 @@ const styles = () => ({
6366
active: {
6467
background: "#f4f4f4",
6568
},
66-
69+
linkTab: {
70+
"& span": {
71+
whiteSpace: "nowrap",
72+
overflow: "hidden",
73+
textOverflow: "ellipsis",
74+
width: "100px"
75+
}
76+
}
6777
});
6878

6979
const DrawerHeader = styled("div")(({ theme }) => ({
@@ -74,19 +84,22 @@ const DrawerHeader = styled("div")(({ theme }) => ({
7484
justifyContent: "flex-start",
7585
}));
7686

87+
const updateNotification: WindowEvents = 'updateNotification';
88+
7789
export const SideMenuBar = ({
7890
children,
7991
setIsAuthenticated,
8092
}: SideMenuBarPropsType) => {
93+
useSocketInit();
8194
const [isDrawerOpen, setIsDrawerOpen] = useState(false);
8295
const location = useLocation();
8396
const { name: currentUserName, avatar: currentuserAvatar } = JSON.parse(getUserInfoFromStorage());
8497
const classes = styles();
85-
8698
const [anchorEl, setAnchorEl] = useState<HTMLElement | null>(null);
87-
const messageNotification = 'messageNotification' as WindowEvents.messageNotification;
88-
const { outputStack: notificationStack } = useNotificationProvider<NotificationType>(messageNotification);
89-
99+
100+
useNotificationProvider<NotificationType>(updateNotification); //custom hook for notifications
101+
const { notifications } = useNotificationStore(); //Zustand store
102+
90103
const menuItems = [
91104
{
92105
text: PageEnum.Posts,
@@ -99,14 +112,14 @@ export const SideMenuBar = ({
99112
path: "/people",
100113
},
101114
{
102-
text: PageEnum.Account,
115+
text: `Hi, ${currentUserName}`,
103116
icon: <AccountCircleIcon />,
104117
path: "/account",
105118
},
106119
{
107120
text: PageEnum.Messenger,
108121
icon: (
109-
<Badge color="primary" badgeContent={notificationStack.length}>
122+
<Badge color="primary" badgeContent={notifications.length}>
110123
<TextsmsOutlinedIcon />
111124
</Badge>
112125
),
@@ -189,8 +202,8 @@ export const SideMenuBar = ({
189202
>
190203
<ListItemIcon>{item.icon}</ListItemIcon>
191204
<ListItemText
192-
primary={item.text}
193-
sx={{ fontSize: "20px" }}
205+
primary={item.text}
206+
sx={classes.linkTab}
194207
/>
195208
</ListItemButton>
196209
)}

src/components/Login/LoginForm.tsx

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
import React, {useState, useEffect} from "react";
2-
import Avatar from "@mui/material/Avatar";
3-
import Button from "@mui/material/Button";
4-
import CssBaseline from "@mui/material/CssBaseline";
5-
import TextField from "@mui/material/TextField";
6-
import FormControlLabel from "@mui/material/FormControlLabel";
7-
import Checkbox from "@mui/material/Checkbox";
8-
import MuiLink from "@mui/material/Link";
9-
import Grid from "@mui/material/Grid";
10-
import Box from "@mui/material/Box";
2+
import {
3+
Avatar, CssBaseline,
4+
TextField, FormControlLabel,
5+
Checkbox, Link as MuiLink,
6+
Grid, Box, Typography,
7+
Container
8+
} from "@mui/material";
119
import LockOutlinedIcon from "@mui/icons-material/LockOutlined";
12-
import Typography from "@mui/material/Typography";
13-
import Container from "@mui/material/Container";
1410
import {isUserIdExistPropType} from "../../typesdeclarations/type";
1511
import {loginApi} from "./apis/loginApi";
1612
import {useNavigate} from "react-router-dom";
1713
import { EventEmitter, setAccessToken, setUserInfoInStorage } from "../../util";
14+
import { LoadingButton } from "@mui/lab";
1815

1916
const styles = () => ({
2017
GridItem: {
@@ -38,10 +35,11 @@ export const LoginForm = ({
3835
}: isUserIdExistPropType) => {
3936

4037
const [loginError,setLoginError] = useState<loginErrorInterface>({email:"",password:""});
41-
38+
const [isLoading, setIsLoading] = useState(false);
4239
const navigate = useNavigate();
4340
const classes = styles();
4441
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
42+
setIsLoading(true);
4543
event.preventDefault();
4644
const formData = new FormData(event.currentTarget);
4745
const formdata = {
@@ -58,7 +56,9 @@ export const LoginForm = ({
5856
avatar: data.avatar
5957
});
6058
navigate("/homepage");
59+
return;
6160
}
61+
setIsLoading(false);
6262
};
6363

6464
useEffect(() => {
@@ -121,13 +121,16 @@ export const LoginForm = ({
121121
control={<Checkbox value='remember' color='primary' />}
122122
label='Remember me'
123123
/>
124-
<Button
124+
<LoadingButton
125125
type='submit'
126126
fullWidth
127127
variant='contained'
128-
sx={{mt: 3, mb: 2}}>
128+
sx={{ mt: 3, mb: 2 }}
129+
loading={isLoading}
130+
loadingPosition="center"
131+
>
129132
Sign In
130-
</Button>
133+
</LoadingButton>
131134
<Grid container>
132135
{
133136
// To be implemented in future

0 commit comments

Comments
 (0)