RocketChat/Rocket.Chat

View on GitHub
apps/meteor/client/views/admin/emailInbox/SendTestButton.tsx

Summary

Maintainability
A
0 mins
Test Coverage
import type { IEmailInboxPayload } from '@rocket.chat/core-typings';
import { Button } from '@rocket.chat/fuselage';
import { useToastMessageDispatch, useEndpoint, useTranslation } from '@rocket.chat/ui-contexts';
import type { ReactElement } from 'react';
import React from 'react';

import { GenericTableCell } from '../../../components/GenericTable';

const SendTestButton = ({ id }: { id: IEmailInboxPayload['_id'] }): ReactElement => {
    const t = useTranslation();
    const dispatchToastMessage = useToastMessageDispatch();
    const sendTest = useEndpoint('POST', '/v1/email-inbox.send-test/:_id', { _id: id });

    const handleOnClick = async (e: React.MouseEvent<HTMLElement, MouseEvent>): Promise<void> => {
        e.preventDefault();
        e.stopPropagation();

        try {
            await sendTest();
            dispatchToastMessage({
                type: 'success',
                message: t('Email_sent'),
            });
        } catch (error) {
            dispatchToastMessage({
                type: 'error',
                message: error,
            });
        }
    };

    return (
        <GenericTableCell withTruncatedText>
            <Button icon='send' small onClick={handleOnClick}>
                {t('Send_Test_Email')}
            </Button>
        </GenericTableCell>
    );
};

export default SendTestButton;