diff --git a/provider-ui/lib/data-fetch.js b/provider-ui/lib/data-fetch.js index 5d25f7ee1f0..a1ca30f4b3a 100644 --- a/provider-ui/lib/data-fetch.js +++ b/provider-ui/lib/data-fetch.js @@ -1,4 +1,4 @@ -import fetch from 'isomorphic-unfetch' +import fetch from "isomorphic-unfetch"; // This can be migrated as a custom hook in React const dataFetch = (url, options = {}, successFn, errorFn) => { @@ -6,29 +6,35 @@ const dataFetch = (url, options = {}, successFn, errorFn) => { // const signal = controller.signal; // options.signal = signal; // setTimeout(() => controller.abort(), 10000); // nice to have but will mess with the load test + if (errorFn === undefined) { + errorFn = (err) => { + console.error(`Error fetching ${url} --DataFetch`, err); + }; + } fetch(url, options) - .then(res => { + .then((res) => { if (res.status === 401 || res.redirected) { - if (window.location.host.endsWith('3000')) { - window.location = '/user/login' // for local dev thru node server + if (window.location.host.endsWith("3000")) { + window.location = "/user/login"; // for local dev thru node server } else { - window.location.reload() // for use with Go server + window.location.reload(); // for use with Go server } } - let result + let result; if (res.ok) { // console.log(`res type: ${res.type}`); try { - result = res.json() + result = res.json(); } catch (e) { - result = res.text() + result = res.text(); } - return result + return result; } else { - res.text().then(errorFn) + res.text().then(errorFn); } - }).then(successFn) - .catch(errorFn) -} + }) + .then(successFn) + .catch(errorFn); +}; -export default dataFetch +export default dataFetch; diff --git a/ui/components/ConnectionWizard/helpers/common.js b/ui/components/ConnectionWizard/helpers/common.js index a8b196e5e8a..4ef17d7ec87 100644 --- a/ui/components/ConnectionWizard/helpers/common.js +++ b/ui/components/ConnectionWizard/helpers/common.js @@ -9,14 +9,21 @@ export const closeButtonForSnackbarAction = (closeSnackbar) => (key) => ( ) + + export const successHandlerGenerator = (notify, msg, cb) => (res) => { + if (res !== undefined) { if (cb !== undefined) cb(res) - notify({ message : `${msg}: ${res}`, event_type : EVENT_TYPES.SUCCESS }) + if (typeof res == "object") { + res = JSON.stringify(res) + } + notify({ message : `${msg}`, details : `${res}`, event_type : EVENT_TYPES.SUCCESS }) } } export const errorHandlerGenerator = (notify, msg, cb) => (err) => { if (cb !== undefined) cb(err) - notify({ message : `${msg}: ${err}`, event_type : EVENT_TYPES.ERROR }) -} + err = typeof err !== "string" ? err.toString() : err + notify({ message : `${msg}`, details : err, event_type : EVENT_TYPES.ERROR }) +} \ No newline at end of file diff --git a/ui/components/ConnectionWizard/helpers/kubernetesHelpers.js b/ui/components/ConnectionWizard/helpers/kubernetesHelpers.js index aa33a5bbf38..a8f84d82c78 100644 --- a/ui/components/ConnectionWizard/helpers/kubernetesHelpers.js +++ b/ui/components/ConnectionWizard/helpers/kubernetesHelpers.js @@ -1,5 +1,4 @@ import dataFetch from "../../../lib/data-fetch"; -import { updateProgress } from "../../../lib/store"; import { EVENT_TYPES } from "../../../lib/event-types"; /** @@ -7,9 +6,9 @@ import { EVENT_TYPES } from "../../../lib/event-types"; * @param {(res) => void} successHandler * @param {(err) => void} errorHandler */ -export const pingKubernetes = (successHandler,errorHandler, context) => { +export const pingKubernetes = (successHandler, errorHandler, connectionId) => { dataFetch( - "/api/system/kubernetes/ping?connection_id=" + context, + "/api/system/kubernetes/ping?connection_id=" + connectionId, { credentials : "include" }, successHandler, errorHandler @@ -28,9 +27,9 @@ export const pingKubernetes = (successHandler,errorHandler, context) => { * * @return {true|false} */ -export const isKubernetesConnected = (isClusterConfigured,kubernetesPingStatus) => { +export const isKubernetesConnected = (isClusterConfigured, kubernetesPingStatus) => { - if (isClusterConfigured){ + if (isClusterConfigured) { if (kubernetesPingStatus) return true } @@ -38,13 +37,13 @@ export const isKubernetesConnected = (isClusterConfigured,kubernetesPingStatus) } -export const deleteKubernetesConfig = (successCb,errorCb, id) => +export const deleteKubernetesConfig = (successCb, errorCb, connectionId) => dataFetch( - "/api/system/kubernetes/contexts/" + id, + "/api/system/kubernetes/contexts/" + connectionId, { method : "DELETE", - credentials : "include", }, - updateProgress({ showProgress : false }), + credentials : "include", + }, successCb, errorCb ) @@ -113,13 +112,15 @@ export const submitConfig = (notify, updateProgress, updateK8SConfig, contextNam updateProgress({ showProgress : false }); if (typeof result !== "undefined") { notify({ message : "Kubernetes config was validated!", event_type : EVENT_TYPES.SUCCESS }); - updateK8SConfig({ k8sConfig : { - inClusterConfig : inClusterConfigForm, - k8sfile, - contextName : result.contextName, - clusterConfigured : true, - configuredServer : result.configuredServer, - }, }); + updateK8SConfig({ + k8sConfig : { + inClusterConfig : inClusterConfigForm, + k8sfile, + contextName : result.contextName, + clusterConfigured : true, + configuredServer : result.configuredServer, + }, + }); } }, (err) => alert(err) diff --git a/ui/components/Header.js b/ui/components/Header.js index 3a045d837b7..85d92b6957e 100644 --- a/ui/components/Header.js +++ b/ui/components/Header.js @@ -45,8 +45,7 @@ import RemoteComponent from './RemoteComponent'; import { CapabilitiesRegistry } from "../utils/disabledComponents"; import ExtensionPointSchemaValidator from '../utils/ExtensionPointSchemaValidator'; import dataFetch from '../lib/data-fetch'; -import { withNotify } from '../utils/hooks/useNotification'; -import { useNotification } from '../utils/hooks/useNotification'; +import { useNotification, withNotify } from '../utils/hooks/useNotification'; const lightColor = 'rgba(255, 255, 255, 0.7)'; const styles = (theme) => ({ @@ -270,8 +269,7 @@ function K8sContextMenu({ const [showFullContextMenu, setShowFullContextMenu] = React.useState(false); const [transformProperty, setTransformProperty] = React.useState(100); const deleteCtxtRef = React.createRef(); - const { notify } = useNotification(); - + const { notify } = useNotification() const styleSlider = { position : "absolute", left : "-5rem", @@ -336,12 +334,13 @@ function K8sContextMenu({ return STATUS.NOT_CONNECTED; } - const handleKubernetesClick = (id) => { + const handleKubernetesClick = (name, connectionID) => { + updateProgress({ showProgress : true }) pingKubernetes( - successHandlerGenerator (notify, "Kubernetes pinged", () => updateProgress({ showProgress : false })), - errorHandlerGenerator(notify, "Kubernetes not pinged", () => updateProgress({ showProgress : false })), - id + successHandlerGenerator(notify, `Kubernetes pinged: ${name}`, () => updateProgress({ showProgress : false }),), + errorHandlerGenerator(notify, `Not able to ping kubernetes: ${name}`, () => updateProgress({ showProgress : false })), + connectionID ) } @@ -359,8 +358,8 @@ function K8sContextMenu({ } } deleteKubernetesConfig( - successHandlerGenerator(notify, "Kubernetes config removed", successCallback), - errorHandlerGenerator(notify, "Not able to remove config"), + successHandlerGenerator(notify, `Kubernetes config removed for ${name}`, successCallback), + errorHandlerGenerator(notify, `Not able to remove config for ${name}`), connectionID ) } @@ -377,7 +376,7 @@ function K8sContextMenu({ }, []) return ( <> -