-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add phone and email link block (#2142)
--------- Co-authored-by: Johannes Obermair <[email protected]> Co-authored-by: Johannes Obermair <[email protected]>
- Loading branch information
1 parent
9055ff7
commit 73dfb61
Showing
25 changed files
with
398 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"@comet/cms-admin": minor | ||
"@comet/blocks-api": minor | ||
"@comet/cms-site": minor | ||
"@comet/cms-api": minor | ||
--- | ||
|
||
Add `PhoneLinkBlock` and `EmailLinkBlock` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@comet/cms-api": minor | ||
--- | ||
|
||
Add `IsPhoneNumber` and `isPhoneNumber` validators to validate phone numbers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,14 @@ | ||
import { ExternalLinkBlock } from "@comet/blocks-api"; | ||
import { createLinkBlock, DamFileDownloadLinkBlock, InternalLinkBlock } from "@comet/cms-api"; | ||
import { createLinkBlock, DamFileDownloadLinkBlock, EmailLinkBlock, InternalLinkBlock, PhoneLinkBlock } from "@comet/cms-api"; | ||
import { NewsLinkBlock } from "@src/news/blocks/news-link.block"; | ||
|
||
export const LinkBlock = createLinkBlock({ | ||
supportedBlocks: { internal: InternalLinkBlock, external: ExternalLinkBlock, news: NewsLinkBlock, damFileDownload: DamFileDownloadLinkBlock }, | ||
supportedBlocks: { | ||
internal: InternalLinkBlock, | ||
external: ExternalLinkBlock, | ||
news: NewsLinkBlock, | ||
damFileDownload: DamFileDownloadLinkBlock, | ||
phone: PhoneLinkBlock, | ||
email: EmailLinkBlock, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Field, FinalFormInput } from "@comet/admin"; | ||
import { BlockCategory, BlockInterface, BlocksFinalForm, createBlockSkeleton, SelectPreviewComponent } from "@comet/blocks-admin"; | ||
import { isEmail } from "class-validator"; | ||
import * as React from "react"; | ||
import { FormattedMessage } from "react-intl"; | ||
|
||
import { EmailLinkBlockData, EmailLinkBlockInput } from "../blocks.generated"; | ||
|
||
export const EmailLinkBlock: BlockInterface<EmailLinkBlockData, EmailLinkBlockData, EmailLinkBlockInput> = { | ||
...createBlockSkeleton(), | ||
|
||
name: "EmailLink", | ||
|
||
displayName: <FormattedMessage id="comet.blocks.link.email" defaultMessage="Email" />, | ||
|
||
defaultValues: () => ({ email: undefined }), | ||
|
||
category: BlockCategory.Navigation, | ||
|
||
isValid: (state) => { | ||
return state.email ? isEmail(state.email) : true; | ||
}, | ||
|
||
AdminComponent: ({ state, updateState }) => { | ||
return ( | ||
<SelectPreviewComponent> | ||
<BlocksFinalForm onSubmit={updateState} initialValues={state}> | ||
<Field | ||
label={<FormattedMessage id="comet.blocks.link.email" defaultMessage="Email" />} | ||
name="email" | ||
component={FinalFormInput} | ||
fullWidth | ||
validate={(email: string) => { | ||
if (email && !isEmail(email)) { | ||
return <FormattedMessage id="comet.blocks.link.email.invalid" defaultMessage="Invalid e-mail address" />; | ||
} | ||
}} | ||
/> | ||
</BlocksFinalForm> | ||
</SelectPreviewComponent> | ||
); | ||
}, | ||
|
||
previewContent: (state) => { | ||
return state.email ? [{ type: "text", content: state.email }] : []; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Field, FinalFormInput } from "@comet/admin"; | ||
import { BlockCategory, BlockInterface, BlocksFinalForm, createBlockSkeleton, SelectPreviewComponent } from "@comet/blocks-admin"; | ||
import * as React from "react"; | ||
import { FormattedMessage } from "react-intl"; | ||
|
||
import { PhoneLinkBlockData, PhoneLinkBlockInput } from "../blocks.generated"; | ||
import { isPhoneNumber } from "../validation/isPhoneNumber"; | ||
import { validatePhoneNumber } from "../validation/validatePhoneNumber"; | ||
|
||
export const PhoneLinkBlock: BlockInterface<PhoneLinkBlockData, PhoneLinkBlockData, PhoneLinkBlockInput> = { | ||
...createBlockSkeleton(), | ||
|
||
name: "PhoneLink", | ||
|
||
displayName: <FormattedMessage id="comet.blocks.link.phone" defaultMessage="Phone Number" />, | ||
|
||
defaultValues: () => ({ phone: "" }), | ||
|
||
category: BlockCategory.Navigation, | ||
|
||
isValid: (state) => { | ||
return state.phone ? isPhoneNumber(state.phone) : true; | ||
}, | ||
|
||
AdminComponent: ({ state, updateState }) => { | ||
return ( | ||
<SelectPreviewComponent> | ||
<BlocksFinalForm onSubmit={updateState} initialValues={state}> | ||
<Field | ||
label={<FormattedMessage id="comet.blocks.link.phone" defaultMessage="Phone Number" />} | ||
name="phone" | ||
component={FinalFormInput} | ||
fullWidth | ||
validate={validatePhoneNumber} | ||
/> | ||
</BlocksFinalForm> | ||
</SelectPreviewComponent> | ||
); | ||
}, | ||
|
||
previewContent: (state) => { | ||
return state.phone ? [{ type: "text", content: state.phone }] : []; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const PHONE_NUMBER_REGEX = /^\+?[0-9\s]+$/; | ||
|
||
export function isPhoneNumber(value: string): boolean { | ||
return PHONE_NUMBER_REGEX.test(value); | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/admin/cms-admin/src/validation/validatePhoneNumber.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import React from "react"; | ||
import { FormattedMessage } from "react-intl"; | ||
|
||
import { isPhoneNumber } from "./isPhoneNumber"; | ||
|
||
export function validatePhoneNumber(value?: string) { | ||
if (value && !isPhoneNumber(value)) { | ||
return <FormattedMessage id="comet.validation.validatePhoneNumber.invalid" defaultMessage="Invalid phone number" />; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
packages/api/blocks-api/src/blocks/validator/is-phone-number.validator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const PHONE_NUMBER_REGEX = /^\+?[0-9\s]+$/; | ||
|
||
export const isPhoneNumber = (value: string): boolean => { | ||
return PHONE_NUMBER_REGEX.test(value); | ||
}; |
Oops, something went wrong.