nytai commented on a change in pull request #12189:
URL: 
https://github.com/apache/incubator-superset/pull/12189#discussion_r547619006



##########
File path: superset-frontend/src/views/CRUD/hooks.ts
##########
@@ -206,119 +206,128 @@ export function useSingleViewResource<D extends object 
= any>(
     setState(currentState => ({ ...currentState, ...update }));
   }
 
-  const fetchResource = useCallback((resourceID: number) => {
-    // Set loading state
-    updateState({
-      loading: true,
-    });
+  const fetchResource = useCallback(
+    (resourceID: number) => {
+      // Set loading state
+      updateState({
+        loading: true,
+      });
 
-    return SupersetClient.get({
-      endpoint: `/api/v1/${resourceName}/${resourceID}`,
-    })
-      .then(
-        ({ json = {} }) => {
-          updateState({
-            resource: json.result,
-            error: null,
-          });
-          return json.result;
-        },
-        createErrorHandler(errMsg => {
-          handleErrorMsg(
-            t(
-              'An error occurred while fetching %ss: %s',
-              resourceLabel,
-              JSON.stringify(errMsg),
-            ),
-          );
+      return SupersetClient.get({
+        endpoint: `/api/v1/${resourceName}/${resourceID}`,
+      })
+        .then(
+          ({ json = {} }) => {
+            updateState({
+              resource: json.result,
+              error: null,
+            });
+            return json.result;
+          },
+          createErrorHandler(errMsg => {
+            handleErrorMsg(
+              t(
+                'An error occurred while fetching %ss: %s',
+                resourceLabel,
+                JSON.stringify(errMsg),
+              ),
+            );
 
-          updateState({
-            error: errMsg,
-          });
-        }),
-      )
-      .finally(() => {
-        updateState({ loading: false });
+            updateState({
+              error: errMsg,
+            });
+          }),
+        )
+        .finally(() => {
+          updateState({ loading: false });
+        });
+    },
+    [handleErrorMsg, resourceName, resourceLabel],
+  );
+
+  const createResource = useCallback(
+    (resource: D) => {
+      // Set loading state
+      updateState({
+        loading: true,
       });
-  }, []);
 
-  const createResource = useCallback((resource: D) => {
-    // Set loading state
-    updateState({
-      loading: true,
-    });
+      return SupersetClient.post({
+        endpoint: `/api/v1/${resourceName}/`,
+        body: JSON.stringify(resource),
+        headers: { 'Content-Type': 'application/json' },
+      })
+        .then(
+          ({ json = {} }) => {
+            updateState({
+              resource: json.result,
+              error: null,
+            });
+            return json.id;
+          },
+          createErrorHandler(errMsg => {
+            handleErrorMsg(
+              t(
+                'An error occurred while creating %ss: %s',
+                resourceLabel,
+                JSON.stringify(errMsg),
+              ),
+            );
 
-    return SupersetClient.post({
-      endpoint: `/api/v1/${resourceName}/`,
-      body: JSON.stringify(resource),
-      headers: { 'Content-Type': 'application/json' },
-    })
-      .then(
-        ({ json = {} }) => {
-          updateState({
-            resource: json.result,
-            error: null,
-          });
-          return json.id;
-        },
-        createErrorHandler(errMsg => {
-          handleErrorMsg(
-            t(
-              'An error occurred while creating %ss: %s',
-              resourceLabel,
-              JSON.stringify(errMsg),
-            ),
-          );
+            updateState({
+              error: errMsg,
+            });
+          }),
+        )
+        .finally(() => {
+          updateState({ loading: false });
+        });
+    },
+    [handleErrorMsg, resourceName, resourceLabel],

Review comment:
       noticed a few cases where errors were not resulting in toasts (after 
switching between alerts/reports and opening the modals various times). This 
fixes the error by ensuring we always have a correct reference to the current 
toast function. 




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

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