Skip to content

Commit

Permalink
shell-ui: Add enabled option in query object
Browse files Browse the repository at this point in the history
To disable the query if token is not provided

Refs: #3257
  • Loading branch information
ChengYanJin committed Apr 15, 2021
1 parent 1878730 commit d6a94e3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
8 changes: 6 additions & 2 deletions shell-ui/src/alerts/services/alertHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ export const getServicesAlertName = (): FilterLabels => {

/**
*
* @param {FilterLabels} filterss
* @param {useContext} useContext
* @param {FilterLabels} filters
* @returns An array of alerts with the highest severity
*/
export const useHighestSeverityAlerts = (filters: FilterLabels): Alert[] => {
export const useHighestSeverityAlerts = (
useContext: typeof useContext,
filters: FilterLabels,
): Alert[] => {
const query = useAlerts(useContext)(filters);
const filteredAlerts = query && query.alerts;

Expand Down
6 changes: 3 additions & 3 deletions shell-ui/src/alerts/services/alertHooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ describe('useHighestSeverityAlerts hook', () => {
afterEach(() => server.resetHandlers());

const alertLibrary = window.shellUIAlerts[version];
console.log('window.shellUIAlerts', window.shellUIAlerts);
alertLibrary.createAlertContext(createContext);

const wrapper = ({ children }) => (
<QueryClientProvider client={new QueryClient()}>
<AlertProvider alertManagerUrl={testService}>{children}</AlertProvider>
Expand All @@ -80,7 +80,7 @@ describe('useHighestSeverityAlerts hook', () => {
it('should only get the VolumeAtRisk alert when both VolumeAtRisk and VolumeDegraded are active', async () => {
const { result, waitForNextUpdate } = renderHook(
() =>
useHighestSeverityAlerts({
useHighestSeverityAlerts(useContext, {
alertname: ['VolumeAtRisk', 'VolumeDegraded'],
}),
{ wrapper },
Expand All @@ -95,7 +95,7 @@ describe('useHighestSeverityAlerts hook', () => {
it('should get empty array', async () => {
const { result, waitForNextUpdate } = renderHook(
() =>
useHighestSeverityAlerts({
useHighestSeverityAlerts(useContext, {
alertname: ['NodeAtRisk', 'NodeDegraded'],
}),
{ wrapper },
Expand Down
27 changes: 12 additions & 15 deletions shell-ui/src/platform/service/k8s.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//@flow
import { useState, useEffect } from 'react';
import { useQuery } from 'react-query';
import axios from 'axios';
import { AUTHENTICATED_EVENT } from '../../navbar/events';

const useGetNodesCount = (useQuery: typeof useQuery, k8sUrl: string) => {
Expand Down Expand Up @@ -64,14 +63,15 @@ export const getNodesCountQuery = (
k8sUrl: string,
token?: string | null,
): typeof useQuery => {
if (!token) {
console.error('K8s API Not authenticated');
return;
}
return {
cacheKey: 'countNodes',
queryKey: 'countNodes',
queryFn: () =>
fetch(`${k8sUrl}/api/v1/nodes`)
fetch(`${k8sUrl}/api/v1/nodes`, {
headers: {
// $FlowFixMe - if token is null, the query will not perform
authorization: `bearer ${token}`,
},
})
.then((r) => {
if (r.ok) {
return r.json();
Expand All @@ -80,24 +80,21 @@ export const getNodesCountQuery = (
.then((res) => {
return res.items.length;
}),
options: { refetchInterval: 10000 },
options: { refetchInterval: 10000, enabled: token },
};
};

export const getVolumesCountQuery = (
k8sUrl: string,
token?: string | null,
): typeof useQuery => {
if (!token) {
console.error('K8s API Not authenticated');
return;
}
return {
cacheKey: 'countVolumes',
queryKey: 'countVolumes',
queryFn: () =>
fetch(`${k8sUrl}/api/v1/persistentvolumes`, {
headers: {
authorisation: `bearer ${token}`,
// $FlowFixMe - if token is null, the query will not perform
authorization: `bearer ${token}`,
},
})
.then((r) => {
Expand All @@ -108,6 +105,6 @@ export const getVolumesCountQuery = (
.then((res) => {
return res.items.length;
}),
options: { refetchInterval: 10000 },
options: { refetchInterval: 10000, enabled: token },
};
};

0 comments on commit d6a94e3

Please sign in to comment.