RegionStore.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. Copyright (C) 2020 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, runInAction } from "mobx";
  15. import { Endpoint } from "@src/@types/Endpoint";
  16. import { Region } from "@src/@types/Region";
  17. import regionSource from "@src/sources/RegionSource";
  18. class RegionStore {
  19. @observable regions: Region[] = [];
  20. @observable loading = false;
  21. @action async getRegions() {
  22. this.loading = true;
  23. try {
  24. const regions = await regionSource.getRegions();
  25. runInAction(() => {
  26. this.regions = regions;
  27. });
  28. return regions;
  29. } finally {
  30. runInAction(() => {
  31. this.loading = false;
  32. });
  33. }
  34. }
  35. async setEndpointRegionExport(endpoint: Endpoint) {
  36. if (!endpoint.mapped_regions?.length) {
  37. return;
  38. }
  39. const regions = this.regions.length
  40. ? this.regions
  41. : await this.getRegions();
  42. endpoint.mapped_regions = endpoint.mapped_regions.map(
  43. id => regions.find(r => r.id === id)?.name || id,
  44. );
  45. }
  46. }
  47. export default new RegionStore();