NetworkStore.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 { Network } from '../types/Network'
  17. import NetworkSource from '../sources/NetworkSource'
  18. class NetworkLocalStorage {
  19. static loadNetworksFromStorage(id: string): ?Network[] {
  20. let networkStorage: { id: string, networks: Network[] }[] = JSON.parse(localStorage.getItem('networks') || '[]')
  21. let endpointNetworks = networkStorage.find(n => n.id === id)
  22. if (!endpointNetworks) {
  23. return null
  24. }
  25. return endpointNetworks.networks
  26. }
  27. static saveNetworksToLocalStorage(id: string, networks: Network[]) {
  28. let networkStorage: { id: string, networks: Network[] }[] = JSON.parse(localStorage.getItem('networks') || '[]')
  29. let endpointNetworksIndex = networkStorage.findIndex(n => n.id === id)
  30. if (endpointNetworksIndex > -1) {
  31. networkStorage.splice(endpointNetworksIndex, 1)
  32. }
  33. networkStorage.push({ id, networks })
  34. localStorage.setItem('networks', JSON.stringify(networkStorage))
  35. }
  36. }
  37. class NetworkStore {
  38. @observable networks: Network[] = []
  39. @observable loading: boolean = false
  40. cachedId: string = ''
  41. @action loadNetworks(endpointId: string, environment: any, options?: {
  42. useLocalStorage?: boolean,
  43. quietError?: boolean,
  44. }): Promise<void> {
  45. let id = `${endpointId}-${btoa(JSON.stringify(environment))}`
  46. if (this.cachedId === id && options && options.useLocalStorage) {
  47. return Promise.resolve()
  48. }
  49. this.loading = true
  50. if (options && options.useLocalStorage) {
  51. let networkStorage = NetworkLocalStorage.loadNetworksFromStorage(id)
  52. if (networkStorage) {
  53. this.loading = false
  54. this.networks = networkStorage
  55. this.cachedId = id
  56. return Promise.resolve()
  57. }
  58. }
  59. this.networks = []
  60. return NetworkSource.loadNetworks(endpointId, environment, options).then((networks: Network[]) => {
  61. this.loading = false
  62. this.networks = networks
  63. this.cachedId = id
  64. NetworkLocalStorage.saveNetworksToLocalStorage(id, networks)
  65. }).catch(() => {
  66. this.loading = false
  67. })
  68. }
  69. }
  70. export default new NetworkStore()