-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds error notification UI container and updates snapshots (#47)
Signed-off-by: Drew Baugher <[email protected]>
- Loading branch information
Showing
11 changed files
with
512 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
public/pages/VisualCreatePolicy/containers/ErrorNotification/ErrorNotification.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
import React from "react"; | ||
import "@testing-library/jest-dom/extend-expect"; | ||
import { render } from "@testing-library/react"; | ||
import ErrorNotification from "./ErrorNotification"; | ||
import { ServicesConsumer, ServicesContext } from "../../../../services"; | ||
import { browserServicesMock } from "../../../../../test/mocks"; | ||
import { BrowserServices } from "../../../../models/interfaces"; | ||
import { ErrorNotification as IErrorNotification } from "../../../../../models/interfaces"; | ||
|
||
function renderErrorNotification(errorNotification: IErrorNotification) { | ||
return { | ||
...render( | ||
<ServicesContext.Provider value={browserServicesMock}> | ||
<ServicesConsumer> | ||
{(services: BrowserServices | null) => | ||
services && ( | ||
<ErrorNotification | ||
errorNotification={errorNotification} | ||
errorNotificationJsonString={""} | ||
onChangeChannelId={() => {}} | ||
onChangeMessage={() => {}} | ||
onChangeErrorNotificationJsonString={() => {}} | ||
onSwitchToChannels={() => {}} | ||
notificationService={services?.notificationService} | ||
/> | ||
) | ||
} | ||
</ServicesConsumer> | ||
</ServicesContext.Provider> | ||
), | ||
}; | ||
} | ||
|
||
describe("<ErrorNotification /> spec", () => { | ||
it("renders the component", () => { | ||
const { container } = render( | ||
<ErrorNotification | ||
errorNotification={{ channel: { id: "some_id" }, message_template: { source: "some source message" } }} | ||
errorNotificationJsonString={""} | ||
onChangeChannelId={() => {}} | ||
onChangeMessage={() => {}} | ||
onChangeErrorNotificationJsonString={() => {}} | ||
onSwitchToChannels={() => {}} | ||
notificationService={browserServicesMock.notificationService} | ||
/> | ||
); | ||
expect(container.firstChild).toMatchSnapshot(); | ||
}); | ||
|
||
it("renders the channel ui editor for channels", () => { | ||
const errorNotification = { channel: { id: "some_id" }, message_template: { source: "some source message" } }; | ||
const { queryByTestId } = renderErrorNotification(errorNotification); | ||
|
||
expect(queryByTestId("channel-notification-refresh")).not.toBeNull(); | ||
expect(queryByTestId("create-policy-legacy-notification")).toBeNull(); | ||
}); | ||
|
||
it("renders the json legacy editor for destinations", () => { | ||
const errorNotification = { destination: { slack: { url: "https://slack.com" } }, message_template: { source: "some source message" } }; | ||
const { queryByTestId } = renderErrorNotification(errorNotification); | ||
|
||
expect(queryByTestId("channel-notification-refresh")).toBeNull(); | ||
expect(queryByTestId("create-policy-legacy-notification")).not.toBeNull(); | ||
}); | ||
}); |
140 changes: 140 additions & 0 deletions
140
public/pages/VisualCreatePolicy/containers/ErrorNotification/ErrorNotification.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
import React, { ChangeEvent, Component } from "react"; | ||
import { EuiLink, EuiIcon, EuiFlexGroup, EuiFlexItem, EuiText } from "@elastic/eui"; | ||
import { ContentPanel } from "../../../../components/ContentPanel"; | ||
import "brace/theme/github"; | ||
import "brace/mode/json"; | ||
import { FeatureChannelList } from "../../../../../server/models/interfaces"; | ||
import { NotificationService } from "../../../../services"; | ||
import { ErrorNotification as IErrorNotification } from "../../../../../models/interfaces"; | ||
import { getErrorMessage } from "../../../../utils/helpers"; | ||
import { CoreServicesContext } from "../../../../components/core_services"; | ||
import ChannelNotification from "../../components/ChannelNotification"; | ||
import LegacyNotification from "../../components/LegacyNotification"; | ||
import { DOCUMENTATION_URL } from "../../../../utils/constants"; | ||
|
||
interface ErrorNotificationProps { | ||
errorNotification: IErrorNotification | undefined; | ||
errorNotificationJsonString: string; | ||
onChangeChannelId: (value: ChangeEvent<HTMLSelectElement>) => void; | ||
onChangeMessage: (value: ChangeEvent<HTMLTextAreaElement>) => void; | ||
onChangeErrorNotificationJsonString: (str: string) => void; | ||
onSwitchToChannels: () => void; | ||
notificationService: NotificationService; | ||
} | ||
|
||
interface ErrorNotificationState { | ||
channels: FeatureChannelList[]; | ||
loadingChannels: boolean; | ||
} | ||
|
||
export default class ErrorNotification extends Component<ErrorNotificationProps, ErrorNotificationState> { | ||
static contextType = CoreServicesContext; | ||
constructor(props: ErrorNotificationProps) { | ||
super(props); | ||
|
||
this.state = { | ||
channels: [], | ||
loadingChannels: true, | ||
}; | ||
} | ||
|
||
componentDidMount = async (): Promise<void> => { | ||
await this.getChannels(); | ||
}; | ||
|
||
getChannels = async (): Promise<void> => { | ||
this.setState({ loadingChannels: true }); | ||
try { | ||
const { notificationService } = this.props; | ||
const response = await notificationService.getChannels(); | ||
if (response.ok) { | ||
this.setState({ channels: response.response.feature_channel_list }); | ||
} else { | ||
this.context.notifications.toasts.addDanger(`Could not load notification channels: ${response.error}`); | ||
} | ||
} catch (err) { | ||
this.context.notifications.toasts.addDanger(getErrorMessage(err, "Could not load the notification channels")); | ||
} | ||
this.setState({ loadingChannels: false }); | ||
}; | ||
|
||
render() { | ||
const { | ||
errorNotification, | ||
errorNotificationJsonString, | ||
onChangeChannelId, | ||
onChangeMessage, | ||
onChangeErrorNotificationJsonString, | ||
onSwitchToChannels, | ||
} = this.props; | ||
const { channels, loadingChannels } = this.state; | ||
const hasDestination = !!errorNotification?.destination; | ||
|
||
let content = ( | ||
<ChannelNotification | ||
channelId={errorNotification?.channel?.id || ""} | ||
channels={channels} | ||
loadingChannels={loadingChannels} | ||
message={errorNotification?.message_template?.source || ""} | ||
onChangeChannelId={onChangeChannelId} | ||
onChangeMessage={onChangeMessage} | ||
getChannels={this.getChannels} | ||
/> | ||
); | ||
|
||
// If we have a destination in the error notification then it's either an older policy or they created through the API | ||
if (hasDestination) { | ||
content = ( | ||
<LegacyNotification | ||
notificationJsonString={errorNotificationJsonString} | ||
onChangeNotificationJsonString={onChangeErrorNotificationJsonString} | ||
onSwitchToChannels={onSwitchToChannels} | ||
/> | ||
); | ||
} | ||
|
||
return ( | ||
<ContentPanel | ||
bodyStyles={{ padding: "initial" }} | ||
title={ | ||
<EuiFlexGroup gutterSize="xs" alignItems="center"> | ||
<EuiFlexItem grow={false}> | ||
<EuiText> | ||
<h3>Error notification</h3> | ||
</EuiText> | ||
</EuiFlexItem> | ||
<EuiFlexItem> | ||
<EuiText color="subdued"> | ||
<i> - optional</i> | ||
</EuiText> | ||
</EuiFlexItem> | ||
</EuiFlexGroup> | ||
} | ||
titleSize="s" | ||
subTitleText={ | ||
<EuiText size="s" style={{ padding: "5px 0px" }}> | ||
<p style={{ color: "grey", fontWeight: 200 }}> | ||
You can set up an error notification for when a policy execution fails to notify you.{" "} | ||
<EuiLink href={DOCUMENTATION_URL} target="_blank"> | ||
Learn more <EuiIcon type="popout" size="s" /> | ||
</EuiLink> | ||
</p> | ||
</EuiText> | ||
} | ||
> | ||
<div style={{ padding: "10px 0px 0px 10px" }}>{content}</div> | ||
</ContentPanel> | ||
); | ||
} | ||
} |
Oops, something went wrong.