Skip to content

Commit

Permalink
feat(ActionBlock): PhoneLink (tel, sms, email)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbmoelker committed Dec 28, 2024
1 parent bd790cc commit e3fcb93
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion datocms-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
* @see docs/getting-started.md on how to use this file
* @see docs/decision-log/2023-10-24-datocms-env-file.md on why file is preferred over env vars
*/
export const datocmsEnvironment = 'external-links';
export const datocmsEnvironment = 'email-and-phone-links';
export const datocmsBuildTriggerId = '30535';
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"jiti": "^1.20.0",
"nanostores": "^0.9.5",
"oembed-providers": "^1.0.20230906",
"phone": "^3.1.56",
"pretty-bytes": "^6.1.1",
"promise-all-props": "^3.0.0",
"regexparam": "^3.0.0",
Expand Down
10 changes: 10 additions & 0 deletions src/blocks/ActionBlock/ActionBlock.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import type { ActionBlockFragment } from '@lib/datocms/types';
import Link from '@components/Link/Link.astro';
import LinkToRecord from '@components/LinkToRecord/LinkToRecord.astro';
import PhoneLink from './PhoneLink.astro';
export interface Props {
block: ActionBlockFragment;
Expand Down Expand Up @@ -29,6 +30,15 @@ const actionClassList = (style: string) => ['action', `action--${style}`];
>
{item.title}
</Link>
) : item.__typename === 'PhoneLinkRecord' ? (
<PhoneLink
action={item.action}
phoneNumber={item.phoneNumber}
text={item.text}
class:list={actionClassList(item.style)}
>
{item.title}
</PhoneLink>
) : (
<Fragment />
)
Expand Down
4 changes: 4 additions & 0 deletions src/blocks/ActionBlock/ActionBlock.fragment.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#import './InternalLink.fragment.graphql'
#import './ExternalLink.fragment.graphql'
#import './PhoneLink.fragment.graphql'

fragment ActionBlock on ActionBlockRecord {
__typename
Expand All @@ -12,5 +13,8 @@ fragment ActionBlock on ActionBlockRecord {
... on ExternalLinkRecord {
...ExternalLink
}
... on PhoneLinkRecord {
...PhoneLink
}
}
}
41 changes: 41 additions & 0 deletions src/blocks/ActionBlock/PhoneLink.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
import type { HTMLAttributes } from 'astro/types';
import type { PhoneLinkFragment } from '@lib/datocms/types';
import { phone } from 'phone';
type PhoneLinkProps = Pick<
PhoneLinkFragment,
'action' | 'phoneNumber' | 'text'
>;
export type Props = HTMLAttributes<'a'> & PhoneLinkProps;
const { action, phoneNumber, text, ...props } = Astro.props;
const formatPhoneNumber = (phoneNumber: string) => {
const output = phone(phoneNumber);
// console.log(output);
return output.isValid ? output.phoneNumber : phoneNumber.replace(/\s/g, '');
};
const getHref = ({ action, phoneNumber, text }: PhoneLinkProps) => {
if (action === 'call') {
return `tel:${formatPhoneNumber(phoneNumber)}`;
}
if (action === 'sms') {
const smsNumber = formatPhoneNumber(phoneNumber);
return text
? `sms:${smsNumber}?body=${encodeURIComponent(text)}`
: `sms:${smsNumber}`;
}
if (action === 'whatsapp') {
const whatsAppNumber = formatPhoneNumber(phoneNumber).replace('+', '');
return text
? `https://wa.me/${whatsAppNumber}?text=${encodeURIComponent(text)}`
: `https://wa.me/${whatsAppNumber}`;
}
return '';
};
const href = getHref({ action, phoneNumber, text });
---

<a {href} {...props}><slot /></a>
9 changes: 9 additions & 0 deletions src/blocks/ActionBlock/PhoneLink.fragment.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fragment PhoneLink on PhoneLinkRecord {
__typename
id
title
phoneNumber
action
text
style
}

0 comments on commit e3fcb93

Please sign in to comment.