MigrationStore.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 { MainItem } from '../types/MainItem'
  17. import type { Field } from '../types/Field'
  18. import notificationStore from '../stores/NotificationStore'
  19. import MigrationSource from '../sources/MigrationSource'
  20. class MigrationStore {
  21. @observable migrations: MainItem[] = []
  22. @observable migrationDetails: ?MainItem = null
  23. @observable loading: boolean = true
  24. @observable canceling: boolean | { failed: boolean } = true
  25. @observable detailsLoading: boolean = true
  26. migrationsLoaded: boolean = false
  27. @action getMigrations(options?: { showLoading: boolean }) {
  28. if ((options && options.showLoading) || !this.migrationsLoaded) {
  29. this.loading = true
  30. }
  31. return MigrationSource.getMigrations().then(migrations => {
  32. this.migrations = migrations.map(migration => {
  33. let oldMigration = this.migrations.find(r => r.id === migration.id)
  34. if (oldMigration) {
  35. migration.executions = oldMigration.executions
  36. }
  37. return migration
  38. })
  39. this.loading = false
  40. this.migrationsLoaded = true
  41. }).catch(() => {
  42. this.loading = false
  43. })
  44. }
  45. @action getMigration(migrationId: string, showLoading: boolean) {
  46. this.detailsLoading = showLoading
  47. return MigrationSource.getMigration(migrationId).then(migration => {
  48. this.detailsLoading = false
  49. this.migrationDetails = migration
  50. }).catch(() => {
  51. this.detailsLoading = false
  52. })
  53. }
  54. @action cancel(migrationId: string) {
  55. this.canceling = true
  56. return MigrationSource.cancel(migrationId).then(() => {
  57. this.canceling = false
  58. }).catch(() => {
  59. this.canceling = { failed: true }
  60. })
  61. }
  62. @action delete(migrationId: string) {
  63. return MigrationSource.delete(migrationId).then(() => {
  64. this.migrations = this.migrations.filter(r => r.id !== migrationId)
  65. })
  66. }
  67. @action migrateReplica(replicaId: string, options: Field[]) {
  68. return MigrationSource.migrateReplica(replicaId, options).then(migration => {
  69. this.migrations = [
  70. migration,
  71. ...this.migrations,
  72. ]
  73. notificationStore.alert('Migration successfully created from replica.', 'success', {
  74. action: {
  75. label: 'View Migration Status',
  76. callback: () => {
  77. window.location.href = `/#/migration/tasks/${migration.id}`
  78. },
  79. },
  80. })
  81. })
  82. }
  83. @action clearDetails() {
  84. this.detailsLoading = true
  85. }
  86. }
  87. export default new MigrationStore()