@appbuckets/react-ui-smart-components
Version:
UI Extended Components that work with @appbuckets/react-client and @appbuckets/react-ui
82 lines (79 loc) • 2.5 kB
JavaScript
import { __spreadArray, __read } from '../../_virtual/_tslib.js';
import * as React from 'react';
import isObject from '../../utils/isObject.js';
function getToastProps(content) {
var _a;
/** Assert is not null or undefined */
if (content === null || content === undefined) {
return undefined;
}
/** Transform plain text into simple toast props */
if (typeof content === 'string') {
return { header: content };
}
/** Check if is an object */
if (isObject(content)) {
/** Try to check if is a ClientRequestError */
var maybeClientError_1 = content;
if (
typeof (maybeClientError_1 === null || maybeClientError_1 === void 0
? void 0
: maybeClientError_1.statusCode) === 'number' &&
typeof (maybeClientError_1 === null || maybeClientError_1 === void 0
? void 0
: maybeClientError_1.error) === 'string'
) {
/** Check if is AspNet Core validation error object */
if (
/validation errors/.test(maybeClientError_1.error) &&
maybeClientError_1.statusCode === 400
) {
/** Build the message list */
var messageList_1 = [];
if (
isObject(
(_a = maybeClientError_1.response) === null || _a === void 0
? void 0
: _a.errors
)
) {
Object.keys(maybeClientError_1.response.errors).forEach(function (
key
) {
var errors = maybeClientError_1.response.errors[key];
messageList_1.push.apply(
messageList_1,
__spreadArray([], __read(errors), false)
);
});
}
return {
header: 'Errori nei dati inviati',
content:
!!messageList_1.length &&
React.createElement(
'div',
{ className: 'mt-2' },
React.createElement(
'ul',
null,
messageList_1.map(function (message) {
return React.createElement('li', { key: message }, message);
})
)
),
};
}
/** Return Json Problem Details */
return {
header: maybeClientError_1.error,
content: Array.isArray(maybeClientError_1.message)
? maybeClientError_1.message.join('\n')
: maybeClientError_1.message,
};
}
}
/** Fallback to undefined */
return undefined;
}
export { getToastProps };