2022-02-10 10:25:38 +01:00
|
|
|
import {
|
|
|
|
|
BubbleStep,
|
|
|
|
|
BubbleStepType,
|
|
|
|
|
Edge,
|
|
|
|
|
EmailInputStep,
|
|
|
|
|
InputStepType,
|
|
|
|
|
PhoneNumberInputStep,
|
2022-03-01 07:13:09 +01:00
|
|
|
Step,
|
2022-02-10 10:25:38 +01:00
|
|
|
UrlInputStep,
|
|
|
|
|
Variable,
|
|
|
|
|
} from 'models'
|
|
|
|
|
import { isPossiblePhoneNumber } from 'react-phone-number-input'
|
|
|
|
|
import { isInputStep } from 'utils'
|
|
|
|
|
import { parseVariables } from './variable'
|
|
|
|
|
|
|
|
|
|
const emailRegex =
|
|
|
|
|
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
|
|
|
|
|
const urlRegex =
|
2022-04-28 12:34:39 -07:00
|
|
|
/^(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})$/
|
2022-02-10 10:25:38 +01:00
|
|
|
|
|
|
|
|
export const isInputValid = (
|
|
|
|
|
inputValue: string,
|
|
|
|
|
type: InputStepType
|
|
|
|
|
): boolean => {
|
|
|
|
|
switch (type) {
|
|
|
|
|
case InputStepType.EMAIL:
|
|
|
|
|
return emailRegex.test(inputValue)
|
|
|
|
|
case InputStepType.PHONE:
|
|
|
|
|
return isPossiblePhoneNumber(inputValue)
|
|
|
|
|
case InputStepType.URL:
|
|
|
|
|
return urlRegex.test(inputValue)
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const stepCanBeRetried = (
|
2022-03-01 07:13:09 +01:00
|
|
|
step: Step
|
2022-02-10 10:25:38 +01:00
|
|
|
): step is EmailInputStep | UrlInputStep | PhoneNumberInputStep =>
|
|
|
|
|
isInputStep(step) && 'retryMessageContent' in step.options
|
|
|
|
|
|
|
|
|
|
export const parseRetryStep = (
|
|
|
|
|
step: EmailInputStep | UrlInputStep | PhoneNumberInputStep,
|
|
|
|
|
variables: Variable[],
|
|
|
|
|
createEdge: (edge: Edge) => void
|
|
|
|
|
): BubbleStep => {
|
|
|
|
|
const content = parseVariables(variables)(step.options.retryMessageContent)
|
|
|
|
|
const newStepId = step.id + Math.random() * 1000
|
|
|
|
|
const newEdge: Edge = {
|
|
|
|
|
id: (Math.random() * 1000).toString(),
|
|
|
|
|
from: { stepId: newStepId, blockId: step.blockId },
|
|
|
|
|
to: { blockId: step.blockId, stepId: step.id },
|
|
|
|
|
}
|
|
|
|
|
createEdge(newEdge)
|
|
|
|
|
return {
|
|
|
|
|
blockId: step.blockId,
|
|
|
|
|
id: newStepId,
|
|
|
|
|
type: BubbleStepType.TEXT,
|
|
|
|
|
content: {
|
|
|
|
|
html: `<div>${content}</div>`,
|
|
|
|
|
richText: [],
|
|
|
|
|
plainText: content,
|
|
|
|
|
},
|
|
|
|
|
outgoingEdgeId: newEdge.id,
|
|
|
|
|
}
|
|
|
|
|
}
|