index.jsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. // @flow
  15. import React from 'react'
  16. import { observer } from 'mobx-react'
  17. import styled, { injectGlobal } from 'styled-components'
  18. import NotificationSystem from 'react-notification-system'
  19. import { observe } from 'mobx'
  20. import notificationStore from '../../../stores/NotificationStore'
  21. import type { NotificationItem } from '../../../types/NotificationItem'
  22. import NotificationsStyle from './style.js'
  23. injectGlobal`
  24. ${NotificationsStyle}
  25. `
  26. const Wrapper = styled.div``
  27. @observer
  28. class Notifications extends React.Component<{}> {
  29. notificationsCount: number
  30. notificationSystem: NotificationSystem
  31. constructor() {
  32. super()
  33. this.notificationsCount = 0
  34. }
  35. componentDidMount() {
  36. observe(notificationStore.notifications, change => {
  37. this.handleStoreChange(change.object)
  38. })
  39. }
  40. handleStoreChange(notifications: NotificationItem[]) {
  41. if (!notifications.length || notifications.length <= this.notificationsCount) {
  42. return
  43. }
  44. let lastNotification = notifications[notifications.length - 1]
  45. this.notificationSystem.addNotification({
  46. title: lastNotification.title || lastNotification.message,
  47. message: lastNotification.title ? lastNotification.message : null,
  48. level: lastNotification.level || 'info',
  49. position: 'br',
  50. autoDismiss: 10,
  51. action: lastNotification.options ? lastNotification.options.action : null,
  52. })
  53. this.notificationsCount = notifications.length
  54. }
  55. render() {
  56. return (
  57. <Wrapper>
  58. <NotificationSystem ref={(n) => { this.notificationSystem = n }} />
  59. </Wrapper>
  60. )
  61. }
  62. }
  63. export default Notifications