Skip to content

Commit

Permalink
refresh event sources list if empty
Browse files Browse the repository at this point in the history
  • Loading branch information
rottencandy committed Aug 11, 2020
1 parent d09a574 commit 6dc29d7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
9 changes: 0 additions & 9 deletions frontend/packages/knative-plugin/src/plugin.tsx
Expand Up @@ -40,12 +40,8 @@ import {
} from './utils/get-knative-resources';
import { getKebabActionsForKind } from './utils/kebab-actions';
import {
fetchEventSourcesCrd,
fetchChannelsCrd,
getDynamicEventSourcesResourceList,
getDynamicChannelResourceList,
hideDynamicEventSourceCard,
hideDynamicChannelCard,
} from './utils/fetch-dynamic-eventsources-utils';
import { TopologyConsumedExtensions, topologyPlugin } from './topology/topology-plugin';
import * as eventSourceIcon from './imgs/event-source.svg';
Expand All @@ -67,9 +63,6 @@ type ConsumedExtensions =
| AddAction
| TopologyConsumedExtensions;

// Added it to perform discovery of Dynamic event sources on cluster on app load as kebab option needed models upfront
fetchEventSourcesCrd();
fetchChannelsCrd();
const plugin: Plugin<ConsumedExtensions> = [
{
type: 'ModelDefinition',
Expand Down Expand Up @@ -408,7 +401,6 @@ const plugin: Plugin<ConsumedExtensions> = [
description:
'Create an event source to register interest in a class of events from a particular system',
icon: eventSourceIcon,
hide: hideDynamicEventSourceCard,
},
},
{
Expand All @@ -423,7 +415,6 @@ const plugin: Plugin<ConsumedExtensions> = [
description:
'Create a Knative Channel to create an event forwarding and persistence layer with in-memory and reliable implementations',
icon: channelIcon,
hide: hideDynamicChannelCard,
},
},
...topologyPlugin,
Expand Down
Expand Up @@ -35,8 +35,14 @@ import {
import {
getDynamicEventSourcesWatchers,
getDynamicEventingChannelWatchers,
fetchEventSourcesCrd,
fetchChannelsCrd,
} from '../utils/fetch-dynamic-eventsources-utils';

// Added it to perform discovery of Dynamic event sources on cluster on app load as kebab option needed models upfront
fetchEventSourcesCrd();
fetchChannelsCrd();

export const getKnativeResources = (namespace: string) => {
return {
...knativeServingResourcesRevisionWatchers(namespace),
Expand Down
Expand Up @@ -3,7 +3,7 @@ import * as _ from 'lodash';
import { safeLoad } from 'js-yaml';
import { checkAccess } from '@console/internal/components/utils';
import { useSafetyFirst } from '@console/internal/components/safety-first';
import { getDynamicChannelModelRefs } from './fetch-dynamic-eventsources-utils';
import { useChannelResourcesList } from './fetch-dynamic-eventsources-utils';
import {
getGroupVersionKind,
modelFor,
Expand Down Expand Up @@ -36,7 +36,7 @@ export const getChannelKind = (ref: string): string => {

export const useChannelList = (namespace: string): ChannelListProps => {
const [accessData, setAccessData] = useSafetyFirst({ loaded: false, channelList: [] });
const channelResourcesList = getDynamicChannelModelRefs();
const channelResourcesList = useChannelResourcesList();
const accessList = [];
React.useEffect(() => {
_.forIn(channelResourcesList, (channelRef: string) => {
Expand Down Expand Up @@ -66,8 +66,7 @@ export const useChannelList = (namespace: string): ChannelListProps => {
})
// eslint-disable-next-line no-console
.catch((err) => console.warn(err.message));
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}, [accessList, channelResourcesList, namespace, setAccessData]);

return accessData;
};
Expand Down
Expand Up @@ -76,7 +76,7 @@ export const fetchEventSourcesCrd = async () => {
export const useEventSourceModels = (): EventSourcetData => {
const [modelsData, setModelsData] = useSafetyFirst({ loaded: false, eventSourceModels: [] });
useEffect(() => {
if (!eventSourceData.loaded) {
if (eventSourceData.eventSourceModels.length === 0) {
fetchEventSourcesCrd()
.then((data) => {
setModelsData({ loaded: true, eventSourceModels: data });
Expand Down Expand Up @@ -137,9 +137,6 @@ export const isDynamicEventResourceKind = (resourceRef: string): boolean => {
return index !== -1;
};

export const hideDynamicEventSourceCard = () =>
eventSourceData.eventSourceModels && eventSourceData.eventSourceModels.length > 0;

export const fetchChannelsCrd = async () => {
const url = 'api/console/knative-channels';
try {
Expand Down Expand Up @@ -206,14 +203,33 @@ export const getDynamicEventingChannelWatchers = (namespace: string) => {
return acc;
}, {});
};
export const useChannelResourcesList = (): string[] => {
const [modelRefs, setModelRefs] = useSafetyFirst<string[]>([]);
useEffect(() => {
if (eventSourceData.eventSourceChannels.length === 0) {
fetchChannelsCrd()
.then((data) => {
setModelRefs(data.map((model: K8sKind) => referenceForModel(model)));
})
.catch((err) => {
setModelRefs([]);
// eslint-disable-next-line no-console
console.warn('Error fetching CRDs for dynamic channel model refs', err);
});
} else {
setModelRefs(
eventSourceData.eventSourceChannels.map((model: K8sKind) => referenceForModel(model)),
);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return modelRefs;
};

export const getDynamicChannelModelRefs = (): string[] => {
return eventSourceData.eventSourceChannels.map((model: K8sKind) => referenceForModel(model));
};

export const hideDynamicChannelCard = () =>
eventSourceData.eventSourceChannels && eventSourceData.eventSourceChannels.length > 0;

export const isEventingChannelResourceKind = (resourceRef: string): boolean => {
const index = eventSourceData.eventSourceChannels.findIndex(
(model: K8sKind) => referenceForModel(model) === resourceRef,
Expand Down

0 comments on commit 6dc29d7

Please sign in to comment.