diff --git a/new-lamassu-admin/src/components/NotificationCenter/NotificationCenter.js b/new-lamassu-admin/src/components/NotificationCenter/NotificationCenter.js index d9ac99ed..bf7788a0 100644 --- a/new-lamassu-admin/src/components/NotificationCenter/NotificationCenter.js +++ b/new-lamassu-admin/src/components/NotificationCenter/NotificationCenter.js @@ -54,15 +54,20 @@ const CLEAR_ALL_NOTIFICATIONS = gql` const NotificationCenter = ({ close, - notifyUnread, hasUnreadProp, - notifButtonCoords + notifButtonCoords, + popperRef, + refetchHasUnreadHeader, + setHeaderHasUnread }) => { const { data, loading } = useQuery(GET_NOTIFICATIONS, { pollInterval: 60000 }) + + const [xOffset, setXoffset] = useState(300) + const [showingUnread, setShowingUnread] = useState(false) - const [xOffset, setXOffset] = useState(0) + const classes = useStyles({ notifButtonCoords, xOffset }) const machines = R.compose( R.map(R.prop('name')), @@ -70,12 +75,9 @@ const NotificationCenter = ({ )(data?.machines ?? []) const notifications = R.path(['notifications'])(data) ?? [] - const hasUnread = hasUnreadProp || (data?.hasUnreadNotifications ?? false) - const popperDOM = document.querySelector('#notifications-popper') - - if (!hasUnread) { - notifyUnread && notifyUnread() - } + const [hasUnread, setHasUnread] = useState( + hasUnreadProp || (data?.hasUnreadNotifications ?? false) + ) const [toggleClearNotification] = useMutation(TOGGLE_CLEAR_NOTIFICATION, { onError: () => console.error('Error while clearing notification'), refetchQueries: () => ['getNotifications'] @@ -86,8 +88,13 @@ const NotificationCenter = ({ }) useEffect(() => { - if (popperDOM) setXOffset(popperDOM.getBoundingClientRect().x) - }, [popperDOM]) + setXoffset(popperRef.current.getBoundingClientRect().x) + if (data && data.hasUnreadNotifications !== hasUnread) { + refetchHasUnreadHeader() + setHasUnread(!hasUnread) + } + }, [popperRef, data, hasUnread, refetchHasUnreadHeader]) + const buildNotifications = () => { const notificationsToShow = !showingUnread || !hasUnread @@ -117,13 +124,13 @@ const NotificationCenter = ({ return ( <> -
Notifications
+
{hasUnread && ( diff --git a/new-lamassu-admin/src/components/NotificationCenter/NotificationCenter.styles.js b/new-lamassu-admin/src/components/NotificationCenter/NotificationCenter.styles.js index d2142fb8..7263ba52 100644 --- a/new-lamassu-admin/src/components/NotificationCenter/NotificationCenter.styles.js +++ b/new-lamassu-admin/src/components/NotificationCenter/NotificationCenter.styles.js @@ -39,7 +39,7 @@ const styles = { }, notificationIcon: ({ notifButtonCoords, xOffset }) => ({ position: 'absolute', - top: notifButtonCoords ? notifButtonCoords.y : 0, + top: notifButtonCoords ? notifButtonCoords.y - 1 : 0, left: notifButtonCoords ? notifButtonCoords.x - xOffset : 0, cursor: 'pointer', background: 'transparent', diff --git a/new-lamassu-admin/src/components/layout/Header.js b/new-lamassu-admin/src/components/layout/Header.js index 8254c2d6..5d7733e0 100644 --- a/new-lamassu-admin/src/components/layout/Header.js +++ b/new-lamassu-admin/src/components/layout/Header.js @@ -68,6 +68,7 @@ const Header = memo(({ tree }) => { const { data, refetch } = useQuery(HAS_UNREAD, { pollInterval: 60000 }) const notifCenterButtonRef = useRef() + const popperRef = useRef() const history = useHistory() const classes = useStyles() @@ -155,6 +156,7 @@ const Header = memo(({ tree }) => { {hasUnread &&
} { } }}> { + setHasUnread(unread) + }} />