/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import React, { FunctionComponent, useState } from 'react'; import { styled, t } from '@superset-ui/core'; import { NativeGraySelect as Select } from 'src/components/Select'; import Icon from 'src/components/Icon'; import { StyledInputContainer } from '../AlertReportModal'; const StyledNotificationMethod = styled.div` margin-bottom: 10px; .input-container { textarea { height: auto; } } .inline-container { margin-bottom: 10px; .input-container { margin-left: 10px; } > div { margin: 0; } .delete-button { margin-left: 10px; padding-top: 3px; } } `; type NotificationMethod = 'Email' | 'Slack'; type NotificationSetting = { method?: NotificationMethod; recipients: string; options: NotificationMethod[]; }; interface NotificationMethodProps { setting?: NotificationSetting | null; index: number; onUpdate?: (index: number, updatedSetting: NotificationSetting) => void; onRemove?: (index: number) => void; } export const NotificationMethod: FunctionComponent = ({ setting = null, index, onUpdate, onRemove, }) => { const { method, recipients, options } = setting || {}; const [recipientValue, setRecipientValue] = useState( recipients || '', ); if (!setting) { return null; } const onMethodChange = (method: NotificationMethod) => { // Since we're swapping the method, reset the recipients setRecipientValue(''); if (onUpdate) { const updatedSetting = { ...setting, method, recipients: '', }; onUpdate(index, updatedSetting); } }; const onRecipientsChange = ( event: React.ChangeEvent, ) => { const { target } = event; setRecipientValue(target.value); if (onUpdate) { const updatedSetting = { ...setting, recipients: target.value, }; onUpdate(index, updatedSetting); } }; // Set recipients if (!!recipients && recipientValue !== recipients) { setRecipientValue(recipients); } const methodOptions = (options || []).map((method: NotificationMethod) => ( {t(method)} )); return (
{method !== undefined && !!onRemove ? ( onRemove(index)} > ) : null}
{method !== undefined ? (
{t(method)}