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

atlp-rwanda / knights-ecomm-fe / 10065696284

23 Jul 2024 08:10PM UTC coverage: 90.801% (+0.02%) from 90.782%
10065696284

push

github

web-flow
Merge pull request #77 from atlp-rwanda/fix-category-issues

Fix-category-banner-issues

1089 of 1370 branches covered (79.49%)

Branch coverage included in aggregate %.

84 of 97 new or added lines in 8 files covered. (86.6%)

42 existing lines in 6 files now uncovered.

11437 of 12425 relevant lines covered (92.05%)

12.36 hits per line

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

84.0
/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 '../../../public/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 } = useSelector((state: RootState) => state.notification);
51✔
30
  const { decodedToken } = useJwt<DecodedToken>(userToken);
51✔
31
  const socketUrl = import.meta.env.VITE_APP_API_URL;
51✔
32
  const [loading, setLoading] = useState(true);
51✔
33

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

2✔
40
  const email = decodedToken?.email;
2✔
41
  const dispatch = useDispatch<AppDispatch>();
51✔
42

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

×
60
          const sorted = sortedNotifications(notifications.notificationDetails.notifications);
2✔
61

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

18✔
73
  useEffect(() => {
51✔
74
    const socket = io(socketUrl);
51✔
75

51✔
76
    socket.on('connect', () => {
19✔
77
      console.log('Socket.IO Connection established');
19✔
78
    });
19✔
79

×
80
    socket.on('notification', (message: notificationMessage) => {
19✔
81
      if (message.action === `${email} notification`) {
19✔
82
        dispatch(setAllNotifications(sortedNotifications(message.notifications.allNotifications)));
19✔
83
        dispatch(setUnreadNotification(message.notifications.unRead));
×
NEW
84
        if (message.sound === true) {
×
NEW
85
          playNotificationSound();
×
NEW
86
        }
×
87
      }
×
88
    });
×
89

×
90
    socket.on('disconnect', () => {
19✔
91
      console.log('Socket.IO connection closed');
19✔
92
    });
19✔
93

×
94
    return () => {
19✔
95
      socket.disconnect();
19✔
96
    };
19✔
97
  }, [socketUrl, email, dispatch, unreadNotifications]);
19✔
98

19✔
99
  return <div>{!loading && openNotification && <Notification />}</div>;
51✔
100
}
51✔
101

51✔
102
const playNotificationSound = () => {
51✔
103
  const audio = new Audio(notificaticationBellSound);
1✔
104
  audio.volume = 0.2;
1✔
NEW
105
  audio.play().catch((error) => console.error('Error playing sound:', error));
×
NEW
106
};
×
NEW
107

×
UNCOV
108
export default NotificationLayout;
×
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

© 2026 Coveralls, Inc