diff --git a/frontend/app/components/Alerts/Notifications/Notifications.tsx b/frontend/app/components/Alerts/Notifications/Notifications.tsx index 2b9645d77..c4fad52ca 100644 --- a/frontend/app/components/Alerts/Notifications/Notifications.tsx +++ b/frontend/app/components/Alerts/Notifications/Notifications.tsx @@ -1,182 +1,46 @@ import React, { useEffect } from 'react'; import stl from './notifications.module.css'; -import ListItem from './ListItem'; import { connect } from 'react-redux'; -import { Button, SlideModal, Icon, Popup, NoContent } from 'UI'; +import { Icon, Popup } from 'UI'; import { fetchList, setViewed, clearAll } from 'Duck/notifications'; import { setLastRead } from 'Duck/announcements'; -import cn from 'classnames'; -import AnimatedSVG, { ICONS } from 'Shared/AnimatedSVG/AnimatedSVG'; import { useModal } from 'App/components/Modal'; import AlertTriggersModal from 'Shared/AlertTriggersModal'; +import { useStore } from 'App/mstore'; +import { useObserver } from 'mobx-react-lite'; const AUTOREFRESH_INTERVAL = 5 * 60 * 1000; -// @withToggle('visible', 'toggleVisisble') -// @withRouter -// class Notifications extends React.Component { -// state = { alertType: '' }; - -// constructor(props) { -// super(props); -// // setTimeout(() => { -// // props.fetchList(); -// // }, 1000); - -// setInterval(() => { -// props.fetchList(); -// }, AUTOREFRESH_INTERVAL); -// } - -// writeOption = (e, { name, value }) => this.setState({ [ name ]: value }); - -// navigateToUrl = notification => { // TODO should be able to open the alert edit form -// if (notification.options.source === 'ALERT') { -// const { initAlert } = this.props; -// this.props.fetchAlerts().then(function() { -// const { alerts } = this.props; -// const alert = alerts.find(i => i.alertId === notification.options.sourceId) -// initAlert(alert.toJS()); -// }.bind(this)); -// } -// } - -// onClearAll = () => { -// const { notifications } = this.props; -// const firstItem = notifications.first(); -// this.props.clearAll({ endTimestamp: firstItem.createdAt.ts }); -// } - -// onClear = notification => { -// this.props.setViewed(notification.notificationId) -// } - -// toggleModal = () => { -// this.props.toggleVisisble(!this.props.visible); -// } - -// render() { -// const { notifications, visible, loading, clearing, clearingAll } = this.props; -// const { alertType } = this.state; -// const unReadNotificationsCount = notifications.filter(({viewed}) => !viewed).size - -// const filteredList = alertType === '' ? -// notifications : -// notifications.filter(i => i.filterKey === alertType); - -// return ( -//
-// -//
-//
-// { unReadNotificationsCount } -//
-// -//
-//
-// -//
Alerts
-// { unReadNotificationsCount > 0 && ( -//
-// -//
-// )} -//
-// } -// right -// isDisplayed={ visible } -// onClose={ visible && this.toggleModal } -// bgColor="white" -// size="small" -// content={ -//
-// -// -//
-// } -// subtext="There are no alerts to show." -// show={ !loading && notifications.size === 0 } -// size="small" -// > -// { -// filteredList.map(item => ( -//
-// this.onClear(item)} -// loading={clearing} -// /> -//
-// )) -// } -// -// -// } -// /> -// -// ); -// } -// } - -// export default connect(state => ({ -// notifications: state.getIn(['notifications', 'list']), -// loading: state.getIn(['notifications', 'fetchRequest', 'loading']), -// clearing: state.getIn(['notifications', 'setViewed', 'loading']), -// clearingAll: state.getIn(['notifications', 'clearAll', 'loading']), -// alerts: state.getIn(['alerts', 'list']), -// }), { fetchList, setLastRead, setViewed, clearAll, fetchAlerts, initAlert })(Notifications); - interface Props { notifications: any; fetchList: any; } function Notifications(props: Props) { - const { notifications } = props; + // const { notifications } = props; const { showModal } = useModal(); - const unReadNotificationsCount = notifications.filter(({viewed}: any) => !viewed).size + // const unReadNotificationsCount = notifications.filter(({viewed}: any) => !viewed).size + const { notificationStore } = useStore(); + const count = useObserver(() => notificationStore.notificationsCount); useEffect(() => { - if (notifications.size === 0) { - props.fetchList(); - } - + notificationStore.fetchNotificationsCount(); const interval = setInterval(() => { - props.fetchList(); + notificationStore.fetchNotificationsCount() }, AUTOREFRESH_INTERVAL); return () => clearInterval(interval); }, []); - return ( - -
showModal(, { right: true }) }> -
- { unReadNotificationsCount } + return useObserver(() => ( + +
showModal(, { right: true }) }> +
+ { count }
-
- ); + + )); } export default connect((state: any) => ({ diff --git a/frontend/app/components/shared/AlertTriggersModal/AlertTriggersModal.tsx b/frontend/app/components/shared/AlertTriggersModal/AlertTriggersModal.tsx index 5547ae750..e36d03ab1 100644 --- a/frontend/app/components/shared/AlertTriggersModal/AlertTriggersModal.tsx +++ b/frontend/app/components/shared/AlertTriggersModal/AlertTriggersModal.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useEffect } from 'react'; import { Button, NoContent } from 'UI'; import { connect } from 'react-redux'; import { fetchList, setViewed, clearAll } from 'Duck/notifications'; @@ -6,9 +6,10 @@ import { setLastRead } from 'Duck/announcements'; import cn from 'classnames'; import AnimatedSVG, { ICONS } from 'Shared/AnimatedSVG/AnimatedSVG'; import ListItem from './ListItem' +import { useStore } from 'App/mstore'; +import { useObserver } from 'mobx-react-lite'; interface Props { - unReadNotificationsCount: number; setLastRead: Function; clearingAll: boolean; @@ -20,7 +21,10 @@ interface Props { setViewed: Function; } function AlertTriggersModal(props: Props) { - const { list, unReadNotificationsCount, loading, clearingAll, clearing } = props; + const { loading, clearingAll, clearing } = props; + const { notificationStore } = useStore(); + const count = useObserver(() => notificationStore.notificationsCount); + const list = useObserver(() => notificationStore.notifications); const onClearAll = () => { const { list } = props; @@ -29,21 +33,24 @@ function AlertTriggersModal(props: Props) { } const onClear = (notification: any) => { - console.log('onClear', notification); props.setViewed(notification.notificationId) } + useEffect(() => { + notificationStore.fetchNotifications(); + }, []) + return (
Alerts
- { unReadNotificationsCount > 0 && ( + { count > 0 && (