diff --git a/src/components/ItaliaTheme/View/Commons/ContactLink.jsx b/src/components/ItaliaTheme/View/Commons/ContactLink.jsx index 180a6f76a..8a8ed7403 100644 --- a/src/components/ItaliaTheme/View/Commons/ContactLink.jsx +++ b/src/components/ItaliaTheme/View/Commons/ContactLink.jsx @@ -15,7 +15,6 @@ const messages = defineMessages({ id: 'email_label', defaultMessage: 'E-mail', }, - call: { id: 'chiama_il_numero', defaultMessage: 'Chiama il numero', @@ -32,70 +31,90 @@ const messages = defineMessages({ const ContactLink = ({ tel, fax, email, label = true, strong = false }) => { const intl = useIntl(); - let ret_label = null; - let ret = null; - - function ReplacePhoneNumbers(str, type) { - // eslint-disable-next-line no-useless-escape - let newhtml = str.replace(/\+?[0-9]( ?[0-9\/-]+)+.?[0-9]*/gm, function (v) { - let r = - "" + - v + - ''; - return r; - }); - return newhtml; - } - function ReplaceEmails(str) { - let newhtml = str.replace( - /([a-zA-Z0-9+._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi, - function (v) { - let r = - "" + - v + - ''; - return r; - }, + const formatTel = (str, type) => + str.split(/\+?[0-9]( ?[0-9/-]+)+.?[0-9]*/gm).map((v, i) => + i % 2 === 0 ? ( + {` ${v} `} + ) : ( + + {v} + + ), ); - return newhtml; - } + + const formatEmail = (str) => + str + .split(/([a-zA-Z0-9+._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi) + .map((v, i) => + i % 2 === 0 ? ( + {` ${v} `} + ) : ( + + {v} + + ), + ); if (tel) { - ret_label = intl.formatMessage(messages.telefono); - ret = ReplacePhoneNumbers(tel, 'tel'); + return ( + <> + {label && + (strong ? ( + {intl.formatMessage(messages.telefono)} + ) : ( + intl.formatMessage(messages.telefono) + ))} + {formatTel(tel, 'tel')} + + ); } else if (fax) { - ret_label = intl.formatMessage(messages.fax); - ret = ReplacePhoneNumbers(fax, 'fax'); + return ( + <> + {label && + (strong ? ( + {intl.formatMessage(messages.fax)} + ) : ( + intl.formatMessage(messages.fax) + ))} + {formatTel(fax, 'fax')} + + ); } else if (email) { - ret_label = intl.formatMessage(messages.email_label); - ret = ReplaceEmails(email); + return ( + <> + {label && + (strong ? ( + {intl.formatMessage(messages.email_label)} + ) : ( + intl.formatMessage(messages.email_label) + ))} + {formatEmail(email)} + + ); + } else { + return null; } - ret_label = label ? <>{ret_label}: : null; - ret_label = label ? strong ? {ret_label} : ret_label : null; - - return ret ? ( - <> - {ret_label} - - - ) : null; }; ContactLink.propTypes = { tel: PropTypes.string, + fax: PropTypes.string, + email: PropTypes.string, + label: PropTypes.bool, + strong: PropTypes.bool, }; export default ContactLink;