Skip to content

Commit

Permalink
dispaly-warning-policy-notification
Browse files Browse the repository at this point in the history
  • Loading branch information
cyril-ui-developer committed Apr 16, 2024
1 parent ca780df commit 7b88167
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ export enum ActionType {
BeginImpersonate = 'beginImpersonate',
EndImpersonate = 'endImpersonate',
SetActiveCluster = 'setActiveCluster',
SetHeaders = 'setHeaders',
}

export const setUser = (userInfo: UserInfo) => action(ActionType.SetUser, { userInfo });
export const beginImpersonate = (kind: string, name: string, subprotocols: string[]) =>
action(ActionType.BeginImpersonate, { kind, name, subprotocols });
export const endImpersonate = () => action(ActionType.EndImpersonate);
export const setHeaders = (response: any) => action(ActionType.SetHeaders, { response });

const coreActions = {
setUser,
beginImpersonate,
endImpersonate,
setHeaders,
};

export type CoreAction = Action<typeof coreActions>;
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ export const coreReducer = (state: CoreState = { user: {} }, action: CoreAction)
subprotocols: action.payload.subprotocols,
},
};

case ActionType.SetHeaders:
return {
...state,
response: {
resp: action.payload.response,
},
};
case ActionType.EndImpersonate: {
const stateKeys = Object.keys(state);
return stateKeys.reduce((acc, key) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type GetUser = (state: SDKStoreState) => UserInfo;
* @returns The the impersonate state.
*/
export const getImpersonate: GetImpersonate = (state) => state.sdkCore.impersonate;

export const getHeaders: any = (state) => state.sdkCore.response;
/**
* It provides impersonation details from the redux store as a props object.
* @param state the root state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type ImpersonateKind = {
export type CoreState = {
user?: UserInfo;
impersonate?: ImpersonateKind;
response?: any;
};

export type SDKStoreState = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import * as _ from 'lodash';
import 'whatwg-fetch';
// import * as UIActions from '@console/internal/actions/ui';
// import store from '@console/internal/redux';
import { getUtilsConfig } from '../../app/configSetup';
import { setHeaders } from '../../app/core/actions';
import storeHandler from '../../app/storeHandler';
// import { getConsoleResponseDetails } from '@console/shared/src/utils/get-console-response-details';
import { ConsoleFetchText, ConsoleFetchJSON, ConsoleFetch } from '../../extensions/console-types';
import { TimeoutError } from '../error/http-error';
import { getConsoleRequestHeaders } from './console-fetch-utils';
Expand Down Expand Up @@ -29,6 +34,7 @@ export const consoleFetch: ConsoleFetch = async (url, options = {}, timeout = 60

const parseData = async (response) => {
const text = await response.text();
// store.dispatch(UIActions.setWarningPolicy('response' as any));
const isPlainText = response.headers.get('Content-Type') === 'text/plain';
if (!text) {
return isPlainText ? '' : {};
Expand All @@ -48,6 +54,8 @@ const consoleFetchCommon = async (
const allOptions = _.defaultsDeep({ method }, options, { headers });
const response = await consoleFetch(url, allOptions, timeout);

const data = storeHandler.getStore();
data.dispatch(setHeaders(response));
return isEntireResponse ? response : parseData(response);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as React from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { getHeaders } from '@console/dynamic-plugin-sdk';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { RootState, useSelector, useDispatch } from 'react-redux';
import * as UIActions from '@console/internal/actions/ui';

type WarningPolicy = {
Expand All @@ -14,8 +15,7 @@ type UseWarningPolicy = [WarningPolicy, (value: UIActions.WarningPolicy) => void

export const useWarningPolicy = (): UseWarningPolicy => {
const dispatch = useDispatch();
const wp = useSelector((state: RootState) => state.UI.get('warningPolicy'));

const wp = useSelector(getHeaders);
const headers = React.useMemo(() => Object.fromEntries(wp?.headers || []), [wp]);
const warningPolicy: WarningPolicy = {
warning: headers?.warning,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as UIActions from '@console/internal/actions/ui';
import { K8sResourceCommon } from '@console/internal/module/k8s';
import store from '@console/internal/redux';

const parseData = async (response) => {
const text = await response.text();
Expand All @@ -20,6 +22,6 @@ export const getConsoleResponseDetails = async <R extends K8sResourceCommon>(
const res = await response;
const data = await parseData(res);
const { headers, status } = res;

store.dispatch(UIActions.setWarningPolicy(response.headers as any));
return { data, headers, status };
};
23 changes: 13 additions & 10 deletions frontend/public/components/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
AppInitSDK,
getUser,
useActivePerspective,
getHeaders,
} from '@console/dynamic-plugin-sdk';
import { initConsolePlugins } from '@console/dynamic-plugin-sdk/src/runtime/plugin-init';
import { GuidedTour } from '@console/app/src/components/tour';
Expand All @@ -48,10 +49,10 @@ import { ModalProvider } from '@console/dynamic-plugin-sdk/src/app/modal-support
import { settleAllPromises } from '@console/dynamic-plugin-sdk/src/utils/promise';
import ToastProvider from '@console/shared/src/components/toast/ToastProvider';
import { useToast } from '@console/shared/src/components/toast';
import { documentationURLs, getDocumentationURL } from '@console/internal/components/utils';
//import { documentationURLs, getDocumentationURL } from '@console/internal/components/utils';
import { useTelemetry } from '@console/shared/src/hooks/useTelemetry';
import { useDebounceCallback } from '@console/shared/src/hooks/debounce';
import { useWarningPolicy } from '@console/shared/src/hooks/useWarningPolicy';
//import { useWarningPolicy } from '@console/shared/src/hooks/useWarningPolicy';
import { LOGIN_ERROR_PATH } from '@console/internal/module/auth';
import { URL_POLL_DEFAULT_DELAY } from '@console/internal/components/utils/url-poll-hook';
import { ThemeProvider } from './ThemeProvider';
Expand Down Expand Up @@ -342,26 +343,28 @@ const CaptureTelemetry = React.memo(function CaptureTelemetry() {
const WarningPolicyAlert = React.memo(function WarningPolicyAlert() {
const { t } = useTranslation();
const toastContext = useToast();
const [warningPolicy] = useWarningPolicy();
// const [warningPolicy] = useWarningPolicy();
const wp = useSelector(getHeaders);
const headers = React.useMemo(() => Object.fromEntries(wp?.resp?.headers || []), [wp]);

React.useEffect(() => {
if (warningPolicy?.warning) {
const docURL = getDocumentationURL(documentationURLs.warningPolicy);
if (headers?.warning) {
// const docURL = getDocumentationURL(documentationURLs.headers);

toastContext.addToast({
variant: AlertVariant.warning,
title: t('public~Warning Policy'),
content: t(`The {{kind}} {{ name }} violates policy {{warning}}`, {
kind: warningPolicy?.kind,
name: warningPolicy?.name,
warning: warningPolicy.warning,
kind: 'warningPolicy?.kind',
name: 'warningPolicy?.name',
warning: headers.warning,
}),
actions: [
{
dismiss: true,
label: t('public~Learn more'),
callback: () => {
window.open(docURL, '_blank');
window.open('docURL', '_blank');
},
component: 'a',
},
Expand All @@ -370,7 +373,7 @@ const WarningPolicyAlert = React.memo(function WarningPolicyAlert() {
dismissible: true,
});
}
}, [warningPolicy, t, toastContext]);
}, [headers, t, toastContext]);

return null;
});
Expand Down

0 comments on commit 7b88167

Please sign in to comment.