ReplicaStore.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 notificationStore from '../stores/NotificationStore'
  17. import ReplicaSource from '../sources/ReplicaSource'
  18. import type { MainItem } from '../types/MainItem'
  19. import type { Execution } from '../types/Execution'
  20. import type { Field } from '../types/Field'
  21. class replicaStoreUtils {
  22. static getNewReplica(replicaDetails: MainItem, execution: Execution): MainItem {
  23. if (replicaDetails.executions) {
  24. return {
  25. ...replicaDetails,
  26. executions: [...replicaDetails.executions.filter(e => e.id !== execution.id), execution],
  27. }
  28. }
  29. return {
  30. ...replicaDetails,
  31. executions: [execution],
  32. }
  33. }
  34. }
  35. class ReplicaStore {
  36. @observable replicas: MainItem[] = []
  37. @observable replicaDetails: ?MainItem = null
  38. @observable loading: boolean = true
  39. @observable backgroundLoading: boolean = false
  40. @observable detailsLoading: boolean = true
  41. @observable executionsLoading: boolean = false
  42. replicasLoaded: boolean = false
  43. @action getReplicas(options?: { showLoading: boolean }): Promise<void> {
  44. this.backgroundLoading = true
  45. if ((options && options.showLoading) || !this.replicasLoaded) {
  46. this.loading = true
  47. }
  48. return ReplicaSource.getReplicas().then(replicas => {
  49. this.replicas = replicas
  50. this.loading = false
  51. this.backgroundLoading = false
  52. this.replicasLoaded = true
  53. }).catch(() => {
  54. this.loading = false
  55. this.backgroundLoading = false
  56. })
  57. }
  58. @action getReplicaExecutions(replicaId: string, showLoading: boolean = false): Promise<void> {
  59. if (showLoading) this.executionsLoading = true
  60. return ReplicaSource.getReplicaExecutions(replicaId).then(executions => {
  61. let replica = this.replicas.find(replica => replica.id === replicaId)
  62. if (replica) {
  63. replica.executions = executions
  64. }
  65. if (this.replicaDetails && this.replicaDetails.id === replicaId) {
  66. this.replicaDetails = {
  67. ...this.replicaDetails,
  68. executions,
  69. }
  70. }
  71. this.executionsLoading = false
  72. }).catch(() => { this.executionsLoading = false })
  73. }
  74. @action getReplica(replicaId: string): Promise<void> {
  75. this.detailsLoading = true
  76. return ReplicaSource.getReplica(replicaId).then(replica => {
  77. this.detailsLoading = false
  78. this.replicaDetails = replica
  79. }).catch(() => {
  80. this.detailsLoading = false
  81. })
  82. }
  83. @action execute(replicaId: string, fields?: Field[]): Promise<void> {
  84. return ReplicaSource.execute(replicaId, fields).then(execution => {
  85. if (this.replicaDetails && this.replicaDetails.id === replicaId) {
  86. this.replicaDetails = replicaStoreUtils.getNewReplica(this.replicaDetails, execution)
  87. }
  88. let replicasItemIndex = this.replicas ? this.replicas.findIndex(r => r.id === replicaId) : -1
  89. if (replicasItemIndex > -1) {
  90. const updatedReplica = replicaStoreUtils.getNewReplica(this.replicas[replicasItemIndex], execution)
  91. this.replicas[replicasItemIndex] = updatedReplica
  92. }
  93. })
  94. }
  95. @action cancelExecution(replicaId: string, executionId: string): Promise<void> {
  96. return ReplicaSource.cancelExecution(replicaId, executionId).then(() => {
  97. notificationStore.alert('Cancelled', 'success')
  98. })
  99. }
  100. @action deleteExecution(replicaId: string, executionId: string): Promise<void> {
  101. return ReplicaSource.deleteExecution(replicaId, executionId).then(() => {
  102. let executions = []
  103. if (this.replicaDetails && this.replicaDetails.id === replicaId) {
  104. if (this.replicaDetails.executions) {
  105. executions = [...this.replicaDetails.executions.filter(e => e.id !== executionId)]
  106. }
  107. this.replicaDetails = {
  108. ...this.replicaDetails,
  109. executions,
  110. }
  111. }
  112. })
  113. }
  114. @action delete(replicaId: string) {
  115. return ReplicaSource.delete(replicaId).then(() => {
  116. this.replicas = this.replicas.filter(r => r.id !== replicaId)
  117. })
  118. }
  119. @action deleteDisks(replicaId: string) {
  120. return ReplicaSource.deleteDisks(replicaId).then(execution => {
  121. if (this.replicaDetails && this.replicaDetails.id === replicaId) {
  122. this.replicaDetails = replicaStoreUtils.getNewReplica(this.replicaDetails, execution)
  123. }
  124. let replicasItemIndex = this.replicas ? this.replicas.findIndex(r => r.id === replicaId) : -1
  125. if (replicasItemIndex > -1) {
  126. const updatedReplica = replicaStoreUtils.getNewReplica(this.replicas[replicasItemIndex], execution)
  127. this.replicas[replicasItemIndex] = updatedReplica
  128. }
  129. })
  130. }
  131. @action clearDetails() {
  132. this.detailsLoading = true
  133. this.replicaDetails = null
  134. }
  135. }
  136. export default new ReplicaStore()