Skip to content

Commit

Permalink
feat: added role permission on create-notification-btn and disabled o…
Browse files Browse the repository at this point in the history
…n loading
  • Loading branch information
SagarRajput-7 committed Jun 13, 2024
1 parent 98beab8 commit 50e8069
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
27 changes: 22 additions & 5 deletions frontend/src/container/FormAlertRules/BasicInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import './FormAlertRules.styles.scss';

import { PlusOutlined } from '@ant-design/icons';
import { Button, Form, Select, Switch, Tooltip } from 'antd';
import getChannels from 'api/channels/getAll';
import ROUTES from 'constants/routes';
import useComponentPermission from 'hooks/useComponentPermission';
import useFetch from 'hooks/useFetch';
import { useCallback, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';
import { AppState } from 'store/reducers';
import { AlertDef, Labels } from 'types/api/alerts/def';
import AppReducer from 'types/reducer/app';
import { requireErrorMessage } from 'utils/form/requireErrorMessage';
import { popupContainer } from 'utils/selectPopupContainer';

Expand Down Expand Up @@ -36,6 +42,11 @@ function BasicInfo({
const { t } = useTranslation('alerts');

const channels = useFetch(getChannels);
const { role } = useSelector<AppState, AppReducer>((state) => state.app);
const [addNewChannelPermission] = useComponentPermission(
['add_new_channel'],
role,
);

const [
shouldBroadCastToAllChannels,
Expand All @@ -60,7 +71,7 @@ function BasicInfo({
});
};

const noChannels = !channels.payload?.length;
const noChannels = channels.payload?.length === 0;
const handleCreateNewChannels = useCallback(() => {
window.open(ROUTES.CHANNELS_NEW, '_blank');
}, []);
Expand Down Expand Up @@ -159,7 +170,7 @@ function BasicInfo({
<Switch
checked={shouldBroadCastToAllChannels}
onChange={handleBroadcastToAllChannels}
disabled={noChannels}
disabled={noChannels || !!channels.loading}
/>
</Tooltip>
</FormItemMedium>
Expand All @@ -182,7 +193,9 @@ function BasicInfo({
]}
>
<ChannelSelect
disabled={shouldBroadCastToAllChannels || noChannels}
disabled={
shouldBroadCastToAllChannels || noChannels || !!channels.loading
}
currentValue={alertDef.preferredChannels}
channels={channels}
onSelectChannels={(preferredChannels): void => {
Expand All @@ -196,8 +209,12 @@ function BasicInfo({
</Tooltip>
)}

{noChannels && (
<Button onClick={handleCreateNewChannels} icon={<PlusOutlined />}>
{noChannels && addNewChannelPermission && (
<Button
onClick={handleCreateNewChannels}
icon={<PlusOutlined />}
className="create-notification-btn"
>
Create a notification channel
</Button>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@
margin-top: 20px;
width: 100%;
}

.create-notification-btn {
box-shadow: none;
}

0 comments on commit 50e8069

Please sign in to comment.