message_submit_error.jsx 1.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import PropTypes from 'prop-types';
import React from 'react';
import {FormattedMessage} from 'react-intl';

import {isErrorInvalidSlashCommand} from 'utils/post_utils.jsx';

class MessageSubmitError extends React.PureComponent {
    static propTypes = {
        error: PropTypes.object.isRequired,
        handleSubmit: PropTypes.func.isRequired,
        submittedMessage: PropTypes.string,
    }

    renderSlashCommandError = () => {
        if (!this.props.submittedMessage) {
            return this.props.error.message;
        }

        const command = this.props.submittedMessage.split(' ')[0];
        return (
            <React.Fragment>
                <FormattedMessage
                    id='message_submit_error.invalidCommand'
                    defaultMessage={'Command with a trigger of \'{command}\' not found. '}
                    values={{
                        command,
                    }}
                />
                <a
                    href='#'
                    onClick={this.props.handleSubmit}
                >
                    <FormattedMessage
                        id='message-submit-error.sendAsMessageLink'
                        defaultMessage='Click here to send as a message.'
                    />
                </a>
            </React.Fragment>
        );
    }

    render() {
        const error = this.props.error;

        if (!error) {
            return null;
        }

        let errorContent = error.message;
        if (isErrorInvalidSlashCommand(error)) {
            errorContent = this.renderSlashCommandError();
        }

        return (
            <div className='has-error'>
                <label className='control-label'>
                    {errorContent}
                </label>
            </div>
        );
    }
}

export default MessageSubmitError;