NotificationStore.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 { observable, action } from 'mobx'
  16. import type { AlertInfo, NotificationItemData } from '../types/NotificationItem'
  17. import NotificationSource from '../sources/NotificationSource'
  18. class NotificationStore {
  19. @observable alerts: AlertInfo[] = []
  20. @observable notificationItems: NotificationItemData[] = []
  21. @observable loading: boolean = false
  22. visibleErrors: string[] = []
  23. @action alert(message: string, level?: $PropertyType<AlertInfo, 'level'>, options?: $PropertyType<AlertInfo, 'options'>) {
  24. if (this.visibleErrors.find(e => e === message)) {
  25. return
  26. }
  27. this.alerts.push({ message, level, options })
  28. if (level === 'error') {
  29. this.visibleErrors.push(message)
  30. setTimeout(() => { this.visibleErrors = this.visibleErrors.filter(e => e !== message) }, 10000)
  31. }
  32. }
  33. @action async loadData(showLoading?: boolean) {
  34. this.loading = Boolean(showLoading)
  35. let data = await NotificationSource.loadData()
  36. this.loading = false
  37. this.notificationItems = data
  38. }
  39. @action saveSeen() {
  40. this.notificationItems = this.notificationItems.map(item => { return { ...item, unseen: false } })
  41. NotificationSource.saveSeen(this.notificationItems)
  42. }
  43. }
  44. export default new NotificationStore()