Explorar o código

New execution is not automatically selected

Fix an issue where executing a replica will not cause the newly created
execution to be automatically selected.

Technical note: store functions which modify the state should create a
new object instead of updating the old one.
Sergiu Miclea %!s(int64=8) %!d(string=hai) anos
pai
achega
84dbff100c
Modificáronse 2 ficheiros con 33 adicións e 38 borrados
  1. 3 6
      src/components/organisms/Executions/index.jsx
  2. 30 32
      src/stores/ReplicaStore.js

+ 3 - 6
src/components/organisms/Executions/index.jsx

@@ -114,13 +114,10 @@ class Executions extends React.Component<Props, State> {
       }
 
       if (this.props.item.executions.length > props.item.executions.length) {
-        // $FlowIssue
-        let isSelectedAvailable = props.item.executions.find(e => e.id === this.state.selectedExecution.id)
+        let isSelectedAvailable = props.item.executions.find(e => this.state.selectedExecution && e.id === this.state.selectedExecution.id)
         if (!isSelectedAvailable) {
-          // $FlowIssue
-          let lastIndex = this.props.item.executions.findIndex(e => e.id === this.state.selectedExecution.id)
-          // $FlowIssue
-          if (props.item.executions.length) {
+          let lastIndex = this.props.item && this.props.item.executions ? this.props.item.executions.findIndex(e => this.state.selectedExecution && e.id === this.state.selectedExecution.id) : -1
+          if (props.item && props.item.executions.length) {
             if (props.item.executions.length - 1 >= lastIndex) {
               selectExecution = props.item.executions[lastIndex]
             } else {

+ 30 - 32
src/stores/ReplicaStore.js

@@ -23,28 +23,18 @@ import type { Execution } from '../types/Execution'
 import type { Field } from '../types/Field'
 
 class ReplicaStoreUtils {
-  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)
-    }
-
-    replicasToUpdate.forEach(r => {
-      if (r.executions.find(e => e.id === opts.execution.id)) {
-        return
+  static getNewReplica(replicaDetails: MainItem, execution: Execution): MainItem {
+    if (replicaDetails.executions) {
+      return {
+        ...replicaDetails,
+        executions: [...replicaDetails.executions, execution],
       }
+    }
 
-      if (r.executions) {
-        r.executions.push(opts.execution)
-      } else {
-        r.executions = [opts.execution]
-      }
-    })
+    return {
+      ...replicaDetails,
+      executions: [execution],
+    }
   }
 }
 
@@ -102,12 +92,16 @@ class ReplicaStore {
 
   @action execute(replicaId: string, fields?: Field[]): Promise<void> {
     return ReplicaSource.execute(replicaId, fields).then(execution => {
-      ReplicaStoreUtils.addExecutionToReplica({
-        replicaId,
-        replicas: this.replicas,
-        replicaDetails: this.replicaDetails,
-        execution,
-      })
+      if (this.replicaDetails && this.replicaDetails.id === replicaId) {
+        this.replicaDetails = ReplicaStoreUtils.getNewReplica(this.replicaDetails, execution)
+      }
+
+      let replicasItemIndex = this.replicas ? this.replicas.findIndex(r => r.id === replicaId) : -1
+
+      if (replicasItemIndex > -1) {
+        const updatedReplica = ReplicaStoreUtils.getNewReplica(this.replicas[replicasItemIndex], execution)
+        this.replicas[replicasItemIndex] = updatedReplica
+      }
     })
   }
 
@@ -142,12 +136,16 @@ class ReplicaStore {
 
   @action deleteDisks(replicaId: string) {
     return ReplicaSource.deleteDisks(replicaId).then(execution => {
-      ReplicaStoreUtils.addExecutionToReplica({
-        replicaId,
-        replicas: this.replicas,
-        replicaDetails: this.replicaDetails,
-        execution,
-      })
+      if (this.replicaDetails && this.replicaDetails.id === replicaId) {
+        this.replicaDetails = ReplicaStoreUtils.getNewReplica(this.replicaDetails, execution)
+      }
+
+      let replicasItemIndex = this.replicas ? this.replicas.findIndex(r => r.id === replicaId) : -1
+
+      if (replicasItemIndex > -1) {
+        const updatedReplica = ReplicaStoreUtils.getNewReplica(this.replicas[replicasItemIndex], execution)
+        this.replicas[replicasItemIndex] = updatedReplica
+      }
     })
   }