AAfghahi commented on code in PR #19130:
URL: https://github.com/apache/superset/pull/19130#discussion_r866132571


##########
superset-frontend/src/reports/reducers/reports.js:
##########
@@ -17,32 +17,64 @@
  * under the License.
  */
 /* eslint-disable camelcase */
+// eslint-disable-next-line import/no-extraneous-dependencies
 import { SET_REPORT, ADD_REPORT, EDIT_REPORT } from '../actions/reports';
 
 export default function reportsReducer(state = {}, action) {
   const actionHandlers = {
     [SET_REPORT]() {
-      return {
-        ...action.report.result.reduce(
-          (obj, report) => ({ ...obj, [report.id]: report }),
-          {},
-        ),
-      };
+      const { report, resourceId, creationMethod, filterField } = action;
+      // For now report count should only be one, but we are checking in case
+      // functionality changes.
+      const reportObject = report.result?.find(
+        report => report[filterField] === resourceId,
+      );
+
+      if (reportObject) {
+        return {
+          ...state,
+          [creationMethod]: {
+            ...state[creationMethod],
+            [resourceId]: reportObject,
+          },
+        };
+      }
+      if (state?.[creationMethod]?.[resourceId]) {
+        // remove the empty report from state
+        const newState = { ...state };
+        delete newState[creationMethod][resourceId];
+        return newState;
+      }
+      return { ...state };
     },
+
     [ADD_REPORT]() {
-      const report = action.json.result;
-      report.id = action.json.id;
+      const { result, id } = action.json;
+      const report = { ...result, id };
+      const reportId = report.dashboard || report.chart;

Review Comment:
   Yes, that is a great question, and I do remember the basic gist for our 
reasoning for it because it was an annoying bug during GA. 
   
   Iirc, when we are adding a report, what we are sending to this action, is 
the report object that we have created, the api sends us back only the created 
id as confirmation that it was created, not the entire object. So, in order for 
the report to have an id (which I think further code uses to validate), we 
attach the created id to the created report in state. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to