NotificationStore.ts 1.9 KB

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