Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: contactlink #808

Open
wants to merge 5 commits into
base: v2
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 75 additions & 54 deletions src/components/ItaliaTheme/View/Commons/ContactLink.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const messages = defineMessages({
id: 'email_label',
defaultMessage: 'E-mail',
},

call: {
id: 'chiama_il_numero',
defaultMessage: 'Chiama il numero',
Expand All @@ -32,70 +31,92 @@ 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 =
"<a href='" +
type +
':' +
v.trim().replace(/-|\/|\s/gm, '') +
"' title='" +
(type === 'tel'
? intl.formatMessage(messages.call)
: intl.formatMessage(messages.call_fax)) +
"' >" +
v +
'</a>';
return r;
});
return newhtml;
}
const formatTel = (str, type) =>
str
.split(/((?:\+?\d{1,3}[-.\s]?)?(?:\(?\d{3}\)?[-.\s]?)?\d{3}[-.\s]?\d+)/gm)
.map((v, i) =>
i % 2 === 0 ? (
<span key={i}>{` ${v} `}</span>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

non ho capito questa cosa del %2.
cosa succede se in un campo scrivono due numeridi telefono?
tipo
"0532123456 0532112233"

Copy link
Author

@mamico mamico Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

la split fa appunto split della stringa, tornando alternativamente i match e le stringhe comprese tra i match, nel tuo caso tornerebbe

image

non sarebbe male qui metterci degli unit, ...

) : (
<a
key={i}
href={`${type}:${v}`}
title={
type === 'tel'
? intl.formatMessage(messages.call)
: intl.formatMessage(messages.call_fax)
}
>
{v}
</a>
),
);

function ReplaceEmails(str) {
let newhtml = str.replace(
/([a-zA-Z0-9+._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi,
function (v) {
let r =
"<a href='mailto:" +
v.trim().replace(/|\/|\s/gm, '') +
"' title='" +
intl.formatMessage(messages.write_to) +
"' >" +
v +
'</a>';
return r;
},
);
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 ? (
<span key={i}>{` ${v} `}</span>
) : (
<a
key={i}
href={`mailto:${v}`}
title={intl.formatMessage(messages.write_to)}
>
{v}
</a>
),
);

if (tel) {
ret_label = intl.formatMessage(messages.telefono);
ret = ReplacePhoneNumbers(tel, 'tel');
return (
<>
{label &&
(strong ? (
<strong>{intl.formatMessage(messages.telefono)}</strong>
) : (
intl.formatMessage(messages.telefono)
))}
{formatTel(tel, 'tel')}
</>
);
} else if (fax) {
ret_label = intl.formatMessage(messages.fax);
ret = ReplacePhoneNumbers(fax, 'fax');
return (
<>
{label &&
(strong ? (
<strong>{intl.formatMessage(messages.fax)}</strong>
) : (
intl.formatMessage(messages.fax)
))}
{formatTel(fax, 'fax')}
</>
);
} else if (email) {
ret_label = intl.formatMessage(messages.email_label);
ret = ReplaceEmails(email);
return (
<>
{label &&
(strong ? (
<strong>{intl.formatMessage(messages.email_label)}</strong>
) : (
intl.formatMessage(messages.email_label)
))}
{formatEmail(email)}
</>
);
} else {
return null;
}
ret_label = label ? <>{ret_label}: </> : null;
ret_label = label ? strong ? <strong>{ret_label}</strong> : ret_label : null;

return ret ? (
<>
{ret_label}
<span dangerouslySetInnerHTML={{ __html: ret }} />
</>
) : null;
};

ContactLink.propTypes = {
tel: PropTypes.string,
fax: PropTypes.string,
email: PropTypes.string,
label: PropTypes.bool,
strong: PropTypes.bool,
};

export default ContactLink;
Loading