forked from opendatahub-io/odh-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from jeff-phillips-18/notification-drawer
Add notifications drawer
- Loading branch information
Showing
6 changed files
with
188 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import React from 'react'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import { | ||
Button, | ||
ButtonVariant, | ||
NotificationDrawer, | ||
NotificationDrawerHeader, | ||
NotificationDrawerBody, | ||
NotificationDrawerList, | ||
NotificationDrawerListItem, | ||
NotificationDrawerListItemHeader, | ||
NotificationDrawerListItemBody, | ||
EmptyStateVariant, | ||
EmptyState, | ||
EmptyStateBody, | ||
} from '@patternfly/react-core'; | ||
import { TimesIcon } from '@patternfly/react-icons'; | ||
import { AppNotification, State } from '../redux/types'; | ||
import { ackNotification, removeNotification } from '../redux/actions/actions'; | ||
import { calculateRelativeTime } from '../utilities/utils'; | ||
|
||
interface AppNotificationDrawerProps { | ||
onClose: () => void; | ||
} | ||
|
||
const AppNotificationDrawer: React.FC<AppNotificationDrawerProps> = ({ onClose }) => { | ||
const notifications: AppNotification[] = useSelector<State, AppNotification[]>( | ||
(state) => state.appState.notifications, | ||
); | ||
const dispatch = useDispatch(); | ||
const newNotifications = React.useMemo(() => { | ||
return notifications.filter((notification) => !notification.read).length; | ||
}, [notifications]); | ||
const [currentTime, setCurrentTime] = React.useState<Date>(new Date()); | ||
|
||
React.useEffect(() => { | ||
const timeHandle = setInterval(() => setCurrentTime(new Date()), 20 * 1000); | ||
return () => { | ||
clearInterval(timeHandle); | ||
}; | ||
}, []); | ||
|
||
const markNotificationRead = (notification: AppNotification): void => { | ||
dispatch(ackNotification(notification)); | ||
}; | ||
|
||
const onRemoveNotification = (notification: AppNotification): void => { | ||
dispatch(removeNotification(notification)); | ||
}; | ||
|
||
return ( | ||
<NotificationDrawer className="odh-dashboard__notification-drawer"> | ||
<NotificationDrawerHeader count={newNotifications} onClose={onClose} /> | ||
<NotificationDrawerBody> | ||
{notifications.length ? ( | ||
<NotificationDrawerList> | ||
{notifications.map((notification) => ( | ||
<NotificationDrawerListItem | ||
key={notification.id} | ||
variant={notification.status} | ||
onClick={() => markNotificationRead(notification)} | ||
isRead={notification.read} | ||
> | ||
<NotificationDrawerListItemHeader | ||
variant={notification.status} | ||
title={notification.title} | ||
srTitle={notification.title} | ||
> | ||
<div> | ||
<Button | ||
className="odh-dashboard__notification-drawer__item-remove" | ||
variant={ButtonVariant.plain} | ||
aria-label="remove notification" | ||
onClick={() => onRemoveNotification(notification)} | ||
> | ||
<TimesIcon aria-hidden="true" /> | ||
</Button> | ||
</div> | ||
</NotificationDrawerListItemHeader> | ||
<NotificationDrawerListItemBody | ||
timestamp={calculateRelativeTime(notification.timestamp, currentTime)} | ||
className={notification.message ? '' : 'm-is-hidden'} | ||
> | ||
{notification.message} | ||
</NotificationDrawerListItemBody> | ||
</NotificationDrawerListItem> | ||
))} | ||
</NotificationDrawerList> | ||
) : ( | ||
<EmptyState variant={EmptyStateVariant.small}> | ||
<EmptyStateBody>There are no notifications at this time.</EmptyStateBody> | ||
</EmptyState> | ||
)} | ||
</NotificationDrawerBody> | ||
</NotificationDrawer> | ||
); | ||
}; | ||
|
||
export default AppNotificationDrawer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters