Просмотр исходного кода

Merge pull request #160 from smiclea/fix-execution-add

Fix execution being added twice
Dorin Paslaru 8 лет назад
Родитель
Сommit
2e16971807
1 измененных файлов с 30 добавлено и 11 удалено
  1. 30 11
      src/stores/ReplicaStore.js

+ 30 - 11
src/stores/ReplicaStore.js

@@ -23,19 +23,28 @@ import type { Execution } from '../types/Execution'
 import type { Field } from '../types/Field'
 
 class ReplicaStoreUtils {
-  static addExecutionToReplica(opts: { replicaStore: any, replicaId: string, execution: Execution }) {
-    let executions = [opts.execution]
+  static addExecutionToReplica(opts: {
+    replicas: MainItem[],
+    replicaDetails: ?MainItem,
+    execution: Execution,
+    replicaId: string,
+  }) {
+    let replicasToUpdate = opts.replicas.filter(r => r.id === opts.replicaId)
+    if (opts.replicaDetails && opts.replicaDetails.id === opts.replicaId) {
+      replicasToUpdate.push(opts.replicaDetails)
+    }
 
-    if (opts.replicaStore.replicaDetails.id === opts.replicaId) {
-      if (opts.replicaStore.replicaDetails.executions) {
-        executions = [...opts.replicaStore.replicaDetails.executions, opts.execution]
+    replicasToUpdate.forEach(r => {
+      if (r.executions.find(e => e.id === opts.execution.id)) {
+        return
       }
 
-      opts.replicaStore.replicaDetails = {
-        ...opts.replicaStore.replicaDetails,
-        executions,
+      if (r.executions) {
+        r.executions.push(opts.execution)
+      } else {
+        r.executions = [opts.execution]
       }
-    }
+    })
   }
 }
 
@@ -93,7 +102,12 @@ class ReplicaStore {
 
   @action execute(replicaId: string, fields?: Field[]): Promise<void> {
     return ReplicaSource.execute(replicaId, fields).then(execution => {
-      ReplicaStoreUtils.addExecutionToReplica({ replicaStore: this, replicaId, execution })
+      ReplicaStoreUtils.addExecutionToReplica({
+        replicaId,
+        replicas: this.replicas,
+        replicaDetails: this.replicaDetails,
+        execution,
+      })
     })
   }
 
@@ -128,7 +142,12 @@ class ReplicaStore {
 
   @action deleteDisks(replicaId: string) {
     return ReplicaSource.deleteDisks(replicaId).then(execution => {
-      ReplicaStoreUtils.addExecutionToReplica({ replicaStore: this, replicaId, execution })
+      ReplicaStoreUtils.addExecutionToReplica({
+        replicaId,
+        replicas: this.replicas,
+        replicaDetails: this.replicaDetails,
+        execution,
+      })
     })
   }