import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import classNames from 'classnames'; import React, { useEffect, useState } from 'react'; import { Link } from 'react-router-dom'; import { Card, Dropdown, DropdownMenu, DropdownToggle } from 'reactstrap'; import ListGroup from 'reactstrap/es/ListGroup'; import ListGroupItem from 'reactstrap/es/ListGroupItem'; import { rawEarlierNotifications, rawNewNotifications } from '../../data/notification/notification'; import { isIterableArray } from '../../helpers/utils'; import useFakeFetch from '../../hooks/useFakeFetch'; import FalconCardHeader from '../common/FalconCardHeader'; import Notification from '../notification/Notification'; import { withTranslation } from 'react-i18next'; import { APIBaseURL } from "../../config"; import moment from 'moment'; import { toast } from "react-toastify"; import { getCookieValue } from '../../helpers/utils'; const NotificationDropdown = ({ t }) => { // State const [rawNewNotificationschild, setRawNewNotificationschild] = useState([]); const [isOpen, setIsOpen] = useState(false); const [isAllRead, setIsAllRead] = useState(false); useEffect(() => { let isResponseOK = false; fetch(APIBaseURL + '/webmessagesnew', { method: 'GET', headers: { "Content-type": "application/json", "User-UUID": getCookieValue('user_uuid'), "Token": getCookieValue('token') }, body: null, }).then(response => { console.log(response); if (response.ok) { isResponseOK = true; } return response.json(); }).then(json => { console.log(json) if (isResponseOK) { let NewnotificationList = [] if (json.length > 0) { json.forEach((currentValue, index) => { let notification = {} notification['id'] = json[index]['id']; notification['status'] = json[index]['status']; notification['subject'] = json[index]['subject'] notification['message'] = json[index]['message']; notification['created_datetime'] = moment(parseInt(json[index]['created_datetime'])) .format("YYYY-MM-DD HH:mm:ss"); if (NewnotificationList.length > 3 ){ return true } if (notification['message'].length > 40){ notification['message'] = notification['message'].substring(0,30) + "..."; } if (notification["status"] === "new"){ NewnotificationList.push(notification); } }); } setRawNewNotificationschild(NewnotificationList); } }).catch(err => { console.log(err); }); }, [t,]); console.log("test"); console.log(rawNewNotificationschild); // Handler const handleToggle = e => { e.preventDefault(); setIsOpen(!isOpen); }; const markAsRead = e => { e.preventDefault(); rawNewNotificationschild.map((notification,index) => { console.log('Mark As Read: ', notification["id"]) let isResponseOK = false; fetch(APIBaseURL + '/webmessages/' + notification["id"], { method: 'PUT', headers: { "Content-type": "application/json", "User-UUID": getCookieValue('user_uuid'), "Token": getCookieValue('token') }, body: JSON.stringify({ "data": { "status": 'read' } }), }).then(response => { if (response.ok) { isResponseOK = true; return null; } else { return response.json(); } }).then(json => { console.log(isResponseOK); if (isResponseOK) { } else { toast.error(json.description) } }).catch(err => { console.log(err); }); }); }; return ( { let windowWidth = window.innerWidth; windowWidth > 992 && setIsOpen(true); }} onMouseLeave={() => { let windowWidth = window.innerWidth; windowWidth > 992 && setIsOpen(false); }} > {t('Mark all as read')}
{t('notification_NEW')}
{isIterableArray(rawNewNotificationschild) && rawNewNotificationschild.map((notification, index) => ( ))}
{t('View all')}
); }; export default withTranslation()(NotificationDropdown);