UNPKG

@craftercms/studio-ui

Version:

Services, components, models & utils to build CrafterCMS authoring extensions.

82 lines (80 loc) 2.99 kB
/* * Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License version 3 as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ /* * Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import { nnou } from '../../utils/object'; import Button from '@mui/material/Button'; import OpenInNewIcon from '@mui/icons-material/OpenInNew'; import React from 'react'; import { defineMessages, useIntl } from 'react-intl'; import ErrorState from '../ErrorState/ErrorState'; let UND; const messages = defineMessages({ moreInfo: { id: 'common.moreInfo', defaultMessage: 'More info' }, error: { id: 'words.error', defaultMessage: 'Error' }, back: { id: 'words.back', defaultMessage: 'Back' } }); export function createErrorStatePropsFromApiResponse(apiResponse, formatMessage) { const { code, message = '', remedialAction, documentationUrl } = apiResponse; return { title: nnou(code) ? `${formatMessage(messages.error)}${code ? ` ${code}` : ''}` : '', message: message + (message.endsWith('.') || !remedialAction ? '' : '.') + (remedialAction ? ` ${remedialAction}` : '') + (remedialAction && message ? (remedialAction.endsWith('.') ? '' : '.') : ''), children: documentationUrl ? React.createElement( Button, { href: documentationUrl, target: '_blank', rel: 'noreferrer', variant: 'text' }, formatMessage(messages.moreInfo), ' ', React.createElement(OpenInNewIcon, null) ) : UND }; } export function ApiResponseErrorState(props) { const { error } = props; const { formatMessage } = useIntl(); return React.createElement( ErrorState, Object.assign({}, props, createErrorStatePropsFromApiResponse(error, formatMessage)) ); } export default ApiResponseErrorState;