• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

atlp-rwanda / knights-ecomm-fe / 10110276239

26 Jul 2024 11:08AM UTC coverage: 91.005% (+0.03%) from 90.977%
10110276239

push

github

web-flow
Merge pull request #85 from atlp-rwanda/feat-apply-coupon

feat-apply-coupon

1132 of 1396 branches covered (81.09%)

Branch coverage included in aggregate %.

94 of 97 new or added lines in 6 files covered. (96.91%)

15 existing lines in 3 files now uncovered.

11667 of 12668 relevant lines covered (92.1%)

12.46 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.51
/src/components/Notification/NotificationLayout.tsx
1
import React, { useEffect, useState } from 'react';
1✔
2
import { useJwt } from 'react-jwt';
1✔
3
import { useDispatch, useSelector } from 'react-redux';
1✔
4
import { RootState, AppDispatch } from '../../redux/store';
1✔
5
import { setAllNotifications, setUnreadNotification } from '../../redux/reducers/notification';
1✔
6
import Notification from './Notification';
1✔
7
import { io } from 'socket.io-client';
1✔
8
import { DecodedToken } from '../../pages/Authentication/Login';
1✔
9
import { NotificationProps } from './OneNotification';
1✔
10
import axios from 'axios';
1✔
11
import notificaticationBellSound from './assets/audios/mixkit-achievement-bell-600.wav';
1✔
12

1✔
13
interface notifications {
1✔
14
  allNotifications: NotificationProps[];
1✔
15
  createdAt?: Date;
1✔
16
  id: string;
1✔
17
  unRead: string | number;
1✔
18
  updatedAt?: Date;
1✔
19
}
1✔
20

1✔
21
interface notificationMessage {
1✔
22
  action: string;
1✔
23
  sound?: boolean;
1✔
24
  notifications: notifications;
1✔
25
}
1✔
26

1✔
27
function NotificationLayout() {
51✔
28
  const { userToken } = useSelector((state: RootState) => state.auth);
51✔
29
  const { openNotification, unreadNotifications, allNotifications } = useSelector(
51✔
30
    (state: RootState) => state.notification
51✔
31
  );
51✔
32
  const { decodedToken } = useJwt<DecodedToken>(userToken);
51✔
33
  const socketUrl = import.meta.env.VITE_APP_API_URL;
51✔
34
  const [loading, setLoading] = useState(true);
51✔
35

51✔
36
  const sortedNotifications = (notifications: NotificationProps[]) => {
51✔
37
    return notifications.sort((a, b) => {
2✔
38
      return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime();
4✔
39
    });
2✔
40
  };
2✔
41

51✔
42
  const email = decodedToken?.email;
51!
43
  const dispatch = useDispatch<AppDispatch>();
51✔
44

51✔
45
  useEffect(() => {
51✔
46
    const fetchData = async () => {
18✔
47
      try {
3✔
48
        const response = await axios.get(`${import.meta.env.VITE_APP_API_URL}/notification/`, {
3✔
49
          headers: {
3✔
50
            Authorization: `Bearer ${userToken}`
3✔
51
          }
3✔
52
        });
3✔
53
        if (response.status === 200) {
2✔
54
          if (response.data.data.message === "User doesn't have any notifications.") {
2!
UNCOV
55
            setLoading(false);
×
UNCOV
56
            dispatch(setAllNotifications([]));
×
57
            dispatch(setUnreadNotification(0));
×
58
            return;
×
59
          }
×
60
          const notifications = response.data.data;
2✔
61

2✔
62
          const sorted = sortedNotifications(notifications.notificationDetails.notifications);
2✔
63

2✔
64
          dispatch(setAllNotifications(sorted));
2✔
65
          dispatch(setUnreadNotification(notifications.notificationDetails.unRead));
2✔
66
        }
2✔
67
        setLoading(false);
1✔
68
      } catch (error) {
1✔
69
        console.log('Failed to fetch notifications');
1✔
70
      }
1✔
71
    };
3✔
72
    userToken && fetchData();
18✔
73
  }, [userToken, dispatch]);
51✔
74

51✔
75
  useEffect(() => {
51✔
76
    let unread: number = 0;
19✔
77
    allNotifications.forEach((notification) => {
19✔
78
      if (notification.isRead === false) {
3✔
79
        unread += 1;
2✔
80
      }
2✔
81
    });
19✔
82
    dispatch(setUnreadNotification(unread));
19✔
83
  }, [allNotifications, dispatch]);
51✔
84

51✔
85
  useEffect(() => {
51✔
86
    const socket = io(socketUrl);
19✔
87

19✔
88
    socket.on('connect', () => {
19✔
UNCOV
89
      console.log('Socket.IO Connection established');
×
90
    });
19✔
91

19✔
92
    socket.on('notification', (message: notificationMessage) => {
19✔
UNCOV
93
      if (message.action === `${email} notification`) {
×
UNCOV
94
        dispatch(setAllNotifications(sortedNotifications(message.notifications.allNotifications)));
×
95
        dispatch(setUnreadNotification(message.notifications.unRead));
×
96
        if (message.sound === true) {
×
97
          playNotificationSound();
×
98
        }
×
99
      }
×
100
    });
19✔
101

19✔
102
    socket.on('disconnect', () => {
19✔
UNCOV
103
      console.log('Socket.IO connection closed');
×
104
    });
19✔
105

19✔
106
    return () => {
19✔
107
      socket.disconnect();
19✔
108
    };
19✔
109
  }, [socketUrl, email, dispatch, unreadNotifications]);
51✔
110

51✔
111
  return <div>{!loading && openNotification && <Notification />}</div>;
51✔
112
}
51✔
113

1✔
114
const playNotificationSound = () => {
1✔
UNCOV
115
  const audio = new Audio(notificaticationBellSound);
×
UNCOV
116
  audio.volume = 0.2;
×
117
  audio.play().catch((error) => console.error('Error playing sound:', error));
×
118
};
×
119

1✔
120
export default NotificationLayout;
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc