LicenceStore.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. Copyright (C) 2019 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, runInAction } from 'mobx'
  16. import apiCaller from '../utils/ApiCaller'
  17. import licenceSource from '../sources/LincenceSource'
  18. import type { Licence } from '../types/Licence'
  19. class LicenceStore {
  20. @observable loadingLicenceInfo: boolean = false
  21. @observable licenceInfo: ?Licence = null
  22. @observable addingLicence: boolean = false
  23. @observable version: ?string = null
  24. async loadVersion(): Promise<string> {
  25. if (this.version) {
  26. return this.version
  27. }
  28. let response = await apiCaller.get('/version')
  29. runInAction(() => {
  30. this.version = response.data.version
  31. })
  32. return this.version || ''
  33. }
  34. @action async loadLicenceInfo(opts?: { skipLog?: boolean, showLoading?: boolean }) {
  35. if (opts && opts.showLoading) this.loadingLicenceInfo = true
  36. try {
  37. let licence = await licenceSource.loadLicenceInfo(opts && opts.skipLog)
  38. runInAction(() => {
  39. this.licenceInfo = licence
  40. this.loadingLicenceInfo = false
  41. })
  42. } catch (ex) {
  43. runInAction(() => {
  44. this.loadingLicenceInfo = false
  45. })
  46. throw ex
  47. }
  48. }
  49. @action async addLicence(licence: string) {
  50. this.addingLicence = true
  51. try {
  52. await licenceSource.addLicence(licence)
  53. runInAction(() => {
  54. this.addingLicence = false
  55. })
  56. } catch (ex) {
  57. runInAction(() => {
  58. this.addingLicence = false
  59. })
  60. throw ex
  61. }
  62. }
  63. }
  64. export default new LicenceStore()