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

atlp-rwanda / knights-ecomm-fe / 10110043999

26 Jul 2024 10:48AM UTC coverage: 91.005% (+0.03%) from 90.976%
10110043999

push

github

Calebgisa72
feat-apply-coupon

1132 of 1395 branches covered (81.15%)

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.

11656 of 12657 relevant lines covered (92.09%)

12.49 hits per line

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

96.55
/src/components/Notification/Notification.tsx
1
import React, { useEffect, useState } from 'react';
1✔
2
import {
1✔
3
  setOpenNotification,
1✔
4
  setSelectedNotificationsIds,
1✔
5
  setAllNotifications
1✔
6
} from '../../redux/reducers/notification';
1✔
7
import { useDispatch, useSelector } from 'react-redux';
1✔
8
import OneNotification from './OneNotification';
1✔
9
import { RootState } from '../../redux/store';
1✔
10
import axios from 'axios';
1✔
11
import toast from 'react-hot-toast';
1✔
12
import { getConfig } from '../../redux/actions/cartAction';
1✔
13
import { deleteNotifications } from '../../utils/notificationsFunctios/deleteNotifications';
1✔
14

1✔
15
function Notification() {
16✔
16
  const { userToken } = useSelector((state: RootState) => state.auth);
16✔
17
  const dispatch = useDispatch();
16✔
18
  const { allNotifications, selectedNotificationsIds } = useSelector((state: RootState) => state.notification);
16✔
19
  const [notificationDivs, setNotificationDivs] = useState<JSX.Element[]>([]);
16✔
20
  const notificationIds: string[] = [];
16✔
21
  const config = getConfig();
16✔
22

16✔
23
  const handleNotificationPopup = () => {
16✔
24
    dispatch(setOpenNotification(false));
16✔
25
  };
16✔
26

1✔
27
  const handleSelectAll = (event: React.ChangeEvent<HTMLInputElement>) => {
1✔
28
    const checked = event.target.checked;
16✔
29
    if (checked) {
16✔
30
      allNotifications.forEach((notification) => {
2✔
31
        notificationIds.push(notification.id);
2✔
32
      });
2✔
33
      dispatch(setSelectedNotificationsIds(notificationIds));
4✔
34
    } else if (!checked) {
2✔
35
      dispatch(setSelectedNotificationsIds([]));
2✔
36
    }
2!
UNCOV
37
  };
×
UNCOV
38

×
39
  const handleDeletingNotifications = () => {
2✔
40
    if (selectedNotificationsIds.length === 0) {
16✔
41
      toast.error('Please select notifications to delete before deleting');
16✔
42
    } else {
2✔
43
      const updatedNotifications = allNotifications.filter((notification) => {
1✔
44
        return !selectedNotificationsIds.includes(notification.id);
1✔
45
      });
1✔
46
      dispatch(setAllNotifications(updatedNotifications));
2✔
47
      deleteNotifications(userToken, selectedNotificationsIds);
1✔
48
      dispatch(setSelectedNotificationsIds([]));
1✔
49
    }
1✔
50
  };
1✔
51

1✔
52
  const updateSelectedNotifications = async (event: React.MouseEvent<HTMLDivElement>) => {
2✔
53
    event.preventDefault();
16✔
54
    if (selectedNotificationsIds.length > 0) {
16✔
55
      const updatedNotifications = allNotifications.map((notification) => {
3✔
56
        if (selectedNotificationsIds.includes(notification.id)) {
3✔
57
          return { ...notification, isRead: true };
2✔
58
        } else {
3✔
59
          return notification;
1✔
60
        }
3✔
61
      });
2✔
62
      dispatch(setAllNotifications(updatedNotifications));
2✔
63
      try {
2✔
64
        const updateSelectedNotifications = await axios.put(
2✔
65
          `${import.meta.env.VITE_APP_API_URL}/notification`,
2✔
66
          { notificationIds: selectedNotificationsIds },
2✔
67
          config
2✔
68
        );
2✔
69

2✔
70
        if (updateSelectedNotifications.data.status === 'success') {
2✔
71
          toast.success('Selected notifications marked as read');
1✔
72
        } else {
1✔
73
          toast.error('Failed to update selected notifications');
1✔
74
        }
2!
UNCOV
75
      } catch (error) {
×
UNCOV
76
        console.log('Failed to update selected notifications', error);
×
77
        toast.error('Failed to update selected notifications');
2✔
78
      }
1✔
79
      dispatch(setSelectedNotificationsIds([]));
1✔
80
    } else {
1✔
81
      toast.error('No notifications selected');
2✔
82
    }
3✔
83
  };
1✔
84

1✔
85
  useEffect(() => {
3✔
86
    const notificaionJSX: JSX.Element[] = [];
16✔
87
    allNotifications.forEach((notification, index) => {
16✔
88
      notificaionJSX.push(<OneNotification noficationProp={notification} key={index} />);
8✔
89
    });
8✔
90
    setNotificationDivs(notificaionJSX);
13✔
91
  }, [allNotifications]);
8✔
92

8✔
93
  return (
16✔
94
    <div className="fixed animate-slideInFromTop inset-0 z-40 flex pt-[60px] xmd:pt-[30px] px-6 xmd:px-16 md:px-36 lg:px-48 items-center justify-center bg-black bg-opacity-50 transition-opacity duration-300 ease-out">
16✔
95
      <div className="bg-white h-[550px] w-full p-8 pb-2 rounded-lg shadow-lg transform transition-transform duration-500 ease-in-out flex flex-col gap-4">
16✔
96
        <div className="flex justify-between items-center">
16✔
97
          <div className="text-[18px] font-bold">Notification</div>
16✔
98
          <div onClick={handleNotificationPopup} className="text-[25px] font-medium cursor-pointer">
16✔
99
            X
16✔
100
          </div>
16✔
101
        </div>
16✔
102

16✔
103
        {notificationDivs.length > 0 && (
16✔
104
          <div className="flex gap-3 items-center px-4">
16✔
105
            <input
16✔
106
              style={{ height: '1.2rem', width: '1.2rem', cursor: 'pointer' }}
7✔
107
              type="checkbox"
7✔
108
              id="selectAll"
7✔
109
              checked={allNotifications.length === selectedNotificationsIds.length}
7✔
110
              onChange={handleSelectAll}
7✔
111
              name="selectAll"
7✔
112
            />
7✔
113
            <label className="cursor-pointer" htmlFor="selectAll">
7✔
114
              Select All
7✔
115
            </label>
7✔
116
          </div>
7✔
117
        )}
7✔
118
        <div className="w-full h-full overflow-y-scroll overflow-x-auto">
7✔
119
          <div className="w-[600px] xmd:w-full flex flex-col gap-2">
16✔
120
            {notificationDivs.length > 0 && notificationDivs}
16✔
121
            {notificationDivs.length === 0 && <p className="text-center">You have no notifications</p>}
16✔
122
          </div>
16✔
123
        </div>
16✔
124
        {allNotifications.length > 0 && (
9✔
125
          <div className="py-2 text-[15px] flex flex-col gap-y-2 items-center sm:flex-row justify-between">
16✔
126
            <div onClick={updateSelectedNotifications} className="inline-block hover:underline cursor-pointer">
16✔
127
              Mark all selected as read
16✔
128
            </div>
16✔
129
            <div onClick={handleDeletingNotifications} className="inline-block hover:underline cursor-pointer">
14✔
130
              Clear selected
14✔
131
            </div>
14✔
132
          </div>
14✔
133
        )}
14✔
134
      </div>
14✔
135
    </div>
14✔
136
  );
14✔
137
}
14✔
138

14✔
139
export default Notification;
14✔
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