import PropTypes from 'prop-types';
import React from 'react';
import InfoModal from './InfoModal';

import { ERROR_RESPONSE_TYPE_START } from '../../constants/config';


export default function ServerErrorModal({ clearErrorResponse, errorResponse }){
    if (!errorResponse) {
        return null;
    }
    
    let buttonText = 'Try Again';
    let iconClass = 'ni-warning';
    let text = 'There was an error. Please try again.';
    
    if (errorResponse.type == ERROR_RESPONSE_TYPE_START) {
        text = errorResponse.message;
        buttonText = 'Try Another Time';
        iconClass = 'ni-calendar-times-o';
    } else {
        if (errorResponse.message) {
            text = errorResponse.message;
        }
    }
    
    return (
        <InfoModal
            buttonText={buttonText}
            contentLabel="Error Modal"
            closeModal={clearErrorResponse}
            iconClass={iconClass}
            isOpen={!!errorResponse}
            text={text}
            title="Sorry!"
        />
    );
}

ServerErrorModal.propTypes = {
    clearErrorResponse: PropTypes.func.isRequired,
    errorResponse: PropTypes.object
};
