bito-code-review[bot] commented on code in PR #37107:
URL: https://github.com/apache/superset/pull/37107#discussion_r2711586515


##########
superset-frontend/src/features/reports/ReportModal/reducer.ts:
##########
@@ -0,0 +1,150 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/* eslint-disable camelcase */
+import { omit } from 'lodash';
+import {
+  SET_REPORT,
+  ADD_REPORT,
+  EDIT_REPORT,
+  DELETE_REPORT,
+  ReportAction,
+  SetReportAction,
+  AddReportAction,
+  EditReportAction,
+  DeleteReportAction,
+} from './actions';
+import { ReportObject, ReportCreationMethod } from 
'src/features/reports/types';
+
+// State structure: { dashboards: { [id]: ReportObject }, charts: { [id]: 
ReportObject } }
+export interface ReportsState {
+  dashboards?: Record<number, ReportObject>;
+  charts?: Record<number, ReportObject>;
+  alerts_reports?: Record<number, ReportObject>;
+}
+
+type ActionHandlers = {
+  [key: string]: () => ReportsState;
+};
+
+export default function reportsReducer(
+  state: ReportsState = {},
+  action: ReportAction,
+): ReportsState {
+  const actionHandlers: ActionHandlers = {
+    [SET_REPORT]() {
+      const { report, resourceId, creationMethod, filterField } =
+        action as SetReportAction;
+      // Map filterField ('dashboard_id' or 'chart_id') to the corresponding
+      // ReportObject property ('dashboard' or 'chart')
+      const propertyName =
+        filterField === 'dashboard_id' ? 'dashboard' : 'chart';
+      // For now report count should only be one, but we are checking in case
+      // functionality changes.
+      const reportObject = report.result?.find(
+        (r: ReportObject) => r[propertyName] === resourceId,
+      );
+
+      if (reportObject) {
+        return {
+          ...state,
+          [creationMethod]: {
+            ...state[creationMethod],
+            [resourceId]: reportObject,
+          },
+        };
+      }
+      if (state?.[creationMethod]?.[resourceId]) {
+        // remove the empty report from state
+        const methodState = state[creationMethod];
+        if (methodState) {
+          return {
+            ...state,
+            [creationMethod]: omit(methodState, resourceId),
+          };
+        }
+      }
+      return { ...state };
+    },
+
+    [ADD_REPORT]() {
+      const { result, id } = (action as AddReportAction).json;
+      const report: ReportObject = { ...result, id } as ReportObject;
+      const reportTypeId = report.dashboard ?? report.chart;
+      const creationMethod = report.creation_method as ReportCreationMethod;
+      // this is the id of either the chart or the dashboard associated with 
the report.
+
+      if (reportTypeId === undefined) {
+        return state;
+      }
+
+      return {
+        ...state,
+        [creationMethod]: {
+          ...state[creationMethod],
+          [reportTypeId]: report,
+        },
+      };
+    },
+
+    [EDIT_REPORT]() {
+      const actionTyped = action as EditReportAction;
+      const report: ReportObject = {
+        ...actionTyped.json.result,
+        id: actionTyped.json.id,
+      } as ReportObject;
+      const reportTypeId = report.dashboard ?? report.chart;
+      const creationMethod = report.creation_method as ReportCreationMethod;
+
+      if (reportTypeId === undefined) {
+        return state;
+      }
+
+      return {
+        ...state,
+        [creationMethod]: {
+          ...state[creationMethod],
+          [reportTypeId]: report,
+        },
+      };
+    },
+
+    [DELETE_REPORT]() {
+      const { report } = action as DeleteReportAction;
+      const reportTypeId = report.dashboard ?? report.chart;
+      const creationMethod = report.creation_method as ReportCreationMethod;
+
+      if (reportTypeId === undefined) {
+        return state;
+      }
+
+      const methodState = state[creationMethod];
+      return {
+        ...state,
+        [creationMethod]: methodState
+          ? omit(methodState, reportTypeId)
+          : undefined,
+      };
+    },

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Reducer logic bug for alerts</b></div>
   <div id="fix">
   
   The reducer's logic for determining the state key assumes reportTypeId (from 
dashboard/chart) is always defined, but for alerts_reports, it's undefined, 
preventing state updates. This breaks add, edit, and delete for alerts.
   </div>
   
   
   <details>
   <summary>
   <b>Code suggestion</b>
   </summary>
   <blockquote>Check the AI-generated fix before applying</blockquote>
   <div id="code">
   
   
   ```
    -      const report: ReportObject = { ...result, id } as ReportObject;
    -      const reportTypeId = report.dashboard ?? report.chart;
    -      const creationMethod = report.creation_method as 
ReportCreationMethod;
    -      // this is the id of either the chart or the dashboard associated 
with the report.
    +      const report: ReportObject = { ...result, id } as ReportObject;
    +      const creationMethod = report.creation_method as 
ReportCreationMethod;
    +      const reportTypeId = creationMethod === 'alerts_reports' ? report.id 
: (report.dashboard ?? report.chart);
    +      // this is the id of either the chart or the dashboard associated 
with the report.
    @@ -111,12 +111,13 @@
    -    const reportTypeId = report.dashboard ?? report.chart;
    -    const creationMethod = report.creation_method as ReportCreationMethod;
    -    
    -    if (reportTypeId === undefined) {
    -      return state;
    -    }
    -    
    -    return {
    -      ...state,
    -      [creationMethod]: {
    -        ...state[creationMethod],
    -        [reportTypeId]: report,
    -      },
    -    };
    +    const reportTypeId = report.dashboard ?? report.chart;
    +    const creationMethod = report.creation_method as ReportCreationMethod;
    +    const key = creationMethod === 'alerts_reports' ? report.id : 
reportTypeId;
    +    
    +    if (reportTypeId === undefined && creationMethod !== 'alerts_reports') 
{
    +      return state;
    -    }
    -    
    -    return {
    -      ...state,
    -      [creationMethod]: {
    -        ...state[creationMethod],
    -        [reportTypeId]: report,
    -      },
    -    };
    +    return {
    +      ...state,
    +      [creationMethod]: {
    -        ...state[creationMethod],
    -        [reportTypeId]: report,
    -      },
    -    };
    +        ...state[creationMethod],
    +        [key]: report,
    +      },
    +    };
    @@ -127,17 +127,17 @@
    -    [DELETE_REPORT]() {
    -      const { report } = action as DeleteReportAction;
    -      const reportTypeId = report.dashboard ?? report.chart;
    -      const creationMethod = report.creation_method as 
ReportCreationMethod;
    -
    -      if (reportTypeId === undefined) {
    -        return state;
    -      }
    -
    -      const methodState = state[creationMethod];
    -      return {
    -        ...state,
    -        [creationMethod]: methodState
    -          ? omit(methodState, reportTypeId)
    -          : undefined,
    -      };
    -    },
    +    [DELETE_REPORT]() {
    +      const { report } = action as DeleteReportAction;
    +      const creationMethod = report.creation_method as 
ReportCreationMethod;
    +      const key = creationMethod === 'alerts_reports' ? report.id : 
(report.dashboard ?? report.chart);
    +
    +      if (key === undefined) {
    -        return state;
    -      }
    +
    +      const methodState = state[creationMethod];
    +      return {
    -        ...state,
    -        [creationMethod]: methodState
    -          ? omit(methodState, reportTypeId)
    -          : undefined,
    -      };
    -    },
    +        ...state,
    +        [creationMethod]: methodState
    +          ? omit(methodState, key)
    +          : undefined,
    +      };
    +    },
   ```
   
   </div>
   </details>
   
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #d24bd9</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



-- 
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