/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ import { EuiBasicTable, EuiButton, EuiEmptyPrompt, EuiHorizontalRule, EuiTableFieldDataColumnType, EuiTableSortingType, SortDirection, } from '@elastic/eui'; import { Criteria } from '@elastic/eui/src/components/basic_table/basic_table'; import { Pagination } from '@elastic/eui/src/components/basic_table/pagination_bar'; import _ from 'lodash'; import React, { Component } from 'react'; import { CoreStart } from '../../../../../../../src/core/public'; import { SenderItemType, TableState } from '../../../../../models/interfaces'; import { ContentPanel, ContentPanelActions, } from '../../../../components/ContentPanel'; import { ModalConsumer } from '../../../../components/Modal'; import { ServicesContext } from '../../../../services'; import { ENCRYPTION_TYPE, ROUTES } from '../../../../utils/constants'; import { getErrorMessage } from '../../../../utils/helpers'; import { DEFAULT_PAGE_SIZE_OPTIONS } from '../../../Notifications/utils/constants'; import { DeleteSenderModal } from '../modals/DeleteSenderModal'; import { SendersTableControls, SendersTableControlsFilterType, } from './SendersTableControls'; interface SendersTableProps { coreContext: CoreStart; } interface SendersTableState extends TableState { filters: SendersTableControlsFilterType; } export class SendersTable extends Component< SendersTableProps, SendersTableState > { static contextType = ServicesContext; columns: EuiTableFieldDataColumnType[]; constructor(props: SendersTableProps) { super(props); this.state = { total: 0, from: 0, size: 5, search: '', sortField: 'name', sortDirection: SortDirection.ASC, items: [], selectedItems: [], loading: true, filters: { encryptionMethod: [], }, }; this.columns = [ { field: 'name', name: 'Name', sortable: true, truncateText: true, width: '200px', }, { field: 'smtp_account.from_address', name: 'Outbound email address', sortable: true, truncateText: true, width: '200px', }, { field: 'smtp_account.host', name: 'Host', sortable: true, truncateText: true, width: '200px', }, { field: 'smtp_account.port', name: 'Port', sortable: false, truncateText: true, width: '200px', }, { field: 'smtp_account.method', name: 'Encryption method', sortable: true, truncateText: true, width: '200px', render: (method: string) => _.get(ENCRYPTION_TYPE, method, '-'), }, ]; this.refresh = this.refresh.bind(this); } async componentDidMount() { await this.refresh(); } async componentDidUpdate( prevProps: SendersTableProps, prevState: SendersTableState ) { const prevQuery = SendersTable.getQueryObjectFromState(prevState); const currQuery = SendersTable.getQueryObjectFromState(this.state); if (!_.isEqual(prevQuery, currQuery)) { await this.refresh(); } } static getQueryObjectFromState(state: SendersTableState) { const queryObject: any = { from_index: state.from, max_items: state.size, query: state.search, config_type: 'smtp_account', sort_field: state.sortField, sort_order: state.sortDirection, }; if (state.filters.encryptionMethod.length > 0) { queryObject['smtp_account.method'] = state.filters.encryptionMethod; } return queryObject; } async refresh() { this.setState({ loading: true }); try { const queryObject = SendersTable.getQueryObjectFromState(this.state); const senders = await this.context.notificationService.getSenders( queryObject ); this.setState({ items: senders.items, total: senders.total }); } catch (error) { this.props.coreContext.notifications.toasts.addDanger( getErrorMessage(error, 'There was a problem loading SMTP senders.') ); } this.setState({ loading: false }); } onTableChange = ({ page: tablePage, sort, }: Criteria): void => { const { index: page, size } = tablePage!; const { field: sortField, direction: sortDirection } = sort!; this.setState({ from: page * size, size, sortField, sortDirection }); }; onSelectionChange = (selectedItems: SenderItemType[]): void => { this.setState({ selectedItems }); }; onSearchChange = (search: string): void => { this.setState({ from: 0, search }); }; render() { const page = Math.floor(this.state.from / this.state.size); const pagination: Pagination = { pageIndex: page, pageSize: this.state.size, pageSizeOptions: DEFAULT_PAGE_SIZE_OPTIONS, totalItemCount: this.state.total, }; const sorting: EuiTableSortingType = { sort: { direction: this.state.sortDirection, field: this.state.sortField, }, }; const selection = { selectable: () => true, onSelectionChange: this.onSelectionChange, }; return ( {({ onShow }) => ( onShow(DeleteSenderModal, { senders: this.state.selectedItems, refresh: this.refresh, }) } > Delete )} ), }, { component: ( location.assign( `#${ROUTES.EDIT_SENDER}/${this.state.selectedItems[0]?.config_id}` ) } > Edit ), }, { component: ( Create SMTP sender ), }, ]} /> } bodyStyles={{ padding: 'initial' }} title="SMTP senders" titleSize="m" total={this.state.total} > this.setState({ filters })} /> No SMTP senders to display} body="Set up an outbound email server by creating a sender. You will select a sender when configuring email channels." actions={ Create SMTP sender } /> } onChange={this.onTableChange} pagination={pagination} sorting={sorting} loading={this.state.loading} /> ); } }