import React from 'react';
import PropTypes from 'prop-types';
import { withTranslation } from 'react-i18next';
const ApiErrorMessage = (props) => {
const {
t, errors, targetPath,
} = props;
function reload() {
window.location.reload();
}
function renderMessage(err) {
function renderMessageByErrorCode() {
switch (err.code) {
case 'already_exists':
return (
<>
{ t('page_api_error.already_exists') }
{targetPath}
>
);
case 'notfound_or_forbidden':
return (
{ t('page_api_error.notfound_or_forbidden') }
);
case 'user_not_admin':
return (
{ t('page_api_error.user_not_admin') }
);
case 'outdated':
return (
<>
{ t('page_api_error.outdated') }
{ t('Load latest') }
>
);
case 'invalid_path':
return (
Invalid path
);
default:
return (
Unknown error occured
);
}
}
if (err.code != null) {
return (
{renderMessageByErrorCode()}
);
}
if (err.message != null) {
return (
{err.message}
);
}
// render null if no error has occurred
return null;
}
return (
<>
{errors.map((error) => {
return renderMessage(error);
})}
>
);
};
ApiErrorMessage.propTypes = {
t: PropTypes.func.isRequired, // i18next
errors: PropTypes.array,
targetPath: PropTypes.string,
};
export default withTranslation()(ApiErrorMessage);