anishgirianish commented on code in PR #68113:
URL: https://github.com/apache/airflow/pull/68113#discussion_r3456187354


##########
airflow-core/src/airflow/ui/src/queries/useTestConnection.ts:
##########
@@ -16,39 +16,81 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-import type { Dispatch, SetStateAction } from "react";
+import { useEffect, useState } from "react";
 import { useTranslation } from "react-i18next";
 
-import { useConnectionServiceTestConnection } from "openapi/queries";
-import type { ConnectionTestResponse } from "openapi/requests/types.gen";
+import {
+  useConnectionServiceEnqueueConnectionTest,
+  useConnectionServiceGetConnectionTest,
+} from "openapi/queries";
+import type { ApiError } from "openapi/requests";
+import type { ConnectionTestRequestBody } from "openapi/requests/types.gen";
 import { toaster } from "src/components/ui";
 
-export const useTestConnection = (setConnected: 
Dispatch<SetStateAction<boolean | undefined>>) => {
+type ConnectionStatus = boolean | undefined;
+
+const ACTIVE_STATES = new Set(["pending", "queued", "running"]);
+const POLL_INTERVAL = 2000;
+
+const isActive = (state: string | undefined) => state !== undefined && 
ACTIVE_STATES.has(state);
+
+export const useTestConnection = (setConnected: (status: ConnectionStatus) => 
void) => {
   const { t: translate } = useTranslation("admin");
+  const [token, setToken] = useState<string | undefined>(undefined);
 
-  const onSuccess = (res: ConnectionTestResponse) => {
-    setConnected(res.status);
-    if (res.status) {
-      toaster.create({
-        description: res.message,
-        title: translate("connections.testSuccess.title"),
-        type: "success",
-      });
-    } else {
+  const enqueue = useConnectionServiceEnqueueConnectionTest({
+    onError: (error) => {
+      setConnected(false);
       toaster.create({
-        description: res.message,
-        title: translate("connections.testError.title"),
+        title:
+          (error as ApiError).status === 409
+            ? translate("connections.testInProgress.title")
+            : translate("connections.testError.title"),
         type: "error",
       });
+    },
+    onSuccess: (response) => {
+      setToken(response.token);
+    },
+  });
+
+  const { data } = useConnectionServiceGetConnectionTest(
+    { airflowConnectionTestToken: token ?? "" },
+    undefined,
+    {
+      enabled: token !== undefined,
+      refetchInterval: (query) => (isActive(query.state.data?.state) ? 
POLL_INTERVAL : false),

Review Comment:
   used useAutoRefresh, thanks



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

Reply via email to