2021-07-08 12:02:08 +00:00
|
|
|
import React, { useRef, useState } from "react";
|
2021-11-03 03:54:19 +00:00
|
|
|
import styled, { createGlobalStyle } from "styled-components";
|
|
|
|
|
import Interweave from "interweave";
|
2020-02-12 08:23:50 +00:00
|
|
|
import {
|
|
|
|
|
IButtonProps,
|
|
|
|
|
MaybeElement,
|
2020-03-30 08:55:52 +00:00
|
|
|
Button,
|
2021-08-24 13:53:15 +00:00
|
|
|
Alignment,
|
2021-08-17 11:26:05 +00:00
|
|
|
Position,
|
2020-02-12 08:23:50 +00:00
|
|
|
} from "@blueprintjs/core";
|
2021-11-03 03:54:19 +00:00
|
|
|
import { Popover2 } from "@blueprintjs/popover2";
|
2021-08-24 13:53:15 +00:00
|
|
|
import { IconName } from "@blueprintjs/icons";
|
|
|
|
|
|
2021-09-09 15:10:22 +00:00
|
|
|
import { ComponentProps } from "widgets/BaseComponent";
|
|
|
|
|
|
2020-11-30 07:23:05 +00:00
|
|
|
import { useScript, ScriptStatus } from "utils/hooks/useScript";
|
2020-10-12 13:01:19 +00:00
|
|
|
import {
|
|
|
|
|
GOOGLE_RECAPTCHA_KEY_ERROR,
|
|
|
|
|
GOOGLE_RECAPTCHA_DOMAIN_ERROR,
|
2021-03-13 14:24:45 +00:00
|
|
|
createMessage,
|
2020-10-12 13:01:19 +00:00
|
|
|
} from "constants/messages";
|
2021-08-24 13:53:15 +00:00
|
|
|
import { ThemeProp, Variant } from "components/ads/common";
|
2020-11-24 07:01:37 +00:00
|
|
|
import { Toaster } from "components/ads/Toast";
|
2021-09-09 15:10:22 +00:00
|
|
|
|
2021-07-08 12:02:08 +00:00
|
|
|
import ReCAPTCHA from "react-google-recaptcha";
|
2021-09-23 15:14:24 +00:00
|
|
|
import { Colors } from "../../../constants/Colors";
|
2021-09-07 13:12:04 +00:00
|
|
|
import _ from "lodash";
|
2021-08-24 13:53:15 +00:00
|
|
|
import {
|
|
|
|
|
ButtonBoxShadow,
|
|
|
|
|
ButtonBoxShadowTypes,
|
|
|
|
|
ButtonBorderRadius,
|
|
|
|
|
ButtonBorderRadiusTypes,
|
2021-09-09 15:10:22 +00:00
|
|
|
ButtonVariant,
|
|
|
|
|
ButtonVariantTypes,
|
2021-12-08 13:11:13 +00:00
|
|
|
ButtonPlacement,
|
2021-09-09 15:10:22 +00:00
|
|
|
} from "components/constants";
|
2021-10-06 12:57:05 +00:00
|
|
|
import {
|
|
|
|
|
getCustomBackgroundColor,
|
|
|
|
|
getCustomBorderColor,
|
|
|
|
|
getCustomHoverColor,
|
|
|
|
|
getCustomTextColor,
|
2021-12-08 13:11:13 +00:00
|
|
|
getCustomJustifyContent,
|
|
|
|
|
getAlignText,
|
2021-10-06 12:57:05 +00:00
|
|
|
} from "widgets/WidgetUtils";
|
2021-08-24 13:53:15 +00:00
|
|
|
|
|
|
|
|
const RecaptchaWrapper = styled.div`
|
|
|
|
|
position: relative;
|
|
|
|
|
.grecaptcha-badge {
|
|
|
|
|
visibility: hidden;
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2021-08-17 11:26:05 +00:00
|
|
|
const ToolTipWrapper = styled.div`
|
|
|
|
|
height: 100%;
|
2021-11-03 03:54:19 +00:00
|
|
|
&& .bp3-popover2-target {
|
2021-08-17 11:26:05 +00:00
|
|
|
height: 100%;
|
2021-11-03 03:54:19 +00:00
|
|
|
width: 100%;
|
2021-08-17 11:26:05 +00:00
|
|
|
& > div {
|
|
|
|
|
height: 100%;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2021-11-03 03:54:19 +00:00
|
|
|
const TooltipStyles = createGlobalStyle`
|
|
|
|
|
.btnTooltipContainer {
|
|
|
|
|
.bp3-popover2-content {
|
|
|
|
|
max-width: 350px;
|
|
|
|
|
overflow-wrap: anywhere;
|
|
|
|
|
padding: 10px 12px;
|
|
|
|
|
border-radius: 0px;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2021-11-18 10:22:04 +00:00
|
|
|
type ButtonContainerProps = {
|
|
|
|
|
disabled?: boolean;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const ButtonContainer = styled.div<ButtonContainerProps>`
|
2021-08-24 13:53:15 +00:00
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
justify-content: center;
|
|
|
|
|
height: 100%;
|
2021-11-18 10:22:04 +00:00
|
|
|
${({ disabled }) => disabled && "cursor: not-allowed;"}
|
2021-07-08 12:02:08 +00:00
|
|
|
|
2021-08-24 13:53:15 +00:00
|
|
|
& > button {
|
|
|
|
|
height: 100%;
|
2021-07-08 12:02:08 +00:00
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2021-09-07 13:12:04 +00:00
|
|
|
const StyledButton = styled((props) => (
|
|
|
|
|
<Button
|
|
|
|
|
{..._.omit(props, [
|
|
|
|
|
"borderRadius",
|
|
|
|
|
"boxShadow",
|
|
|
|
|
"boxShadowColor",
|
|
|
|
|
"buttonColor",
|
|
|
|
|
"buttonVariant",
|
|
|
|
|
])}
|
|
|
|
|
/>
|
|
|
|
|
))<ThemeProp & ButtonStyleProps>`
|
2021-08-24 13:53:15 +00:00
|
|
|
height: 100%;
|
|
|
|
|
background-image: none !important;
|
|
|
|
|
font-weight: ${(props) => props.theme.fontWeights[2]};
|
|
|
|
|
outline: none;
|
|
|
|
|
padding: 0px 10px;
|
2019-10-21 15:12:45 +00:00
|
|
|
|
2021-09-23 15:14:24 +00:00
|
|
|
${({ buttonColor, buttonVariant, theme }) => `
|
2021-08-24 13:53:15 +00:00
|
|
|
&:enabled {
|
|
|
|
|
background: ${
|
2021-09-23 15:14:24 +00:00
|
|
|
getCustomBackgroundColor(buttonVariant, buttonColor) !== "none"
|
|
|
|
|
? getCustomBackgroundColor(buttonVariant, buttonColor)
|
2021-10-12 08:04:51 +00:00
|
|
|
: buttonVariant === ButtonVariantTypes.PRIMARY
|
2021-11-06 08:00:57 +00:00
|
|
|
? theme.colors.button.primary.primary.bgColor
|
2021-08-24 13:53:15 +00:00
|
|
|
: "none"
|
|
|
|
|
} !important;
|
2020-01-17 12:34:58 +00:00
|
|
|
}
|
2021-08-24 13:53:15 +00:00
|
|
|
|
|
|
|
|
&:hover:enabled, &:active:enabled {
|
|
|
|
|
background: ${
|
2021-09-23 15:14:24 +00:00
|
|
|
getCustomHoverColor(theme, buttonVariant, buttonColor) !== "none"
|
|
|
|
|
? getCustomHoverColor(theme, buttonVariant, buttonColor)
|
2021-10-12 08:04:51 +00:00
|
|
|
: buttonVariant === ButtonVariantTypes.SECONDARY
|
2021-11-06 08:00:57 +00:00
|
|
|
? theme.colors.button.primary.secondary.hoverColor
|
2021-10-12 08:04:51 +00:00
|
|
|
: buttonVariant === ButtonVariantTypes.TERTIARY
|
2021-11-06 08:00:57 +00:00
|
|
|
? theme.colors.button.primary.tertiary.hoverColor
|
|
|
|
|
: theme.colors.button.primary.primary.hoverColor
|
2021-08-24 13:53:15 +00:00
|
|
|
} !important;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
&:disabled {
|
|
|
|
|
background-color: ${theme.colors.button.disabled.bgColor} !important;
|
2021-11-18 10:22:04 +00:00
|
|
|
color: ${theme.colors.button.disabled.textColor} !important;
|
|
|
|
|
pointer-events: none;
|
2021-11-15 06:29:06 +00:00
|
|
|
border-color: ${theme.colors.button.disabled.bgColor} !important;
|
|
|
|
|
> span {
|
|
|
|
|
color: ${theme.colors.button.disabled.textColor} !important;
|
|
|
|
|
}
|
2021-08-24 13:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
border: ${
|
2021-09-23 15:14:24 +00:00
|
|
|
getCustomBorderColor(buttonVariant, buttonColor) !== "none"
|
|
|
|
|
? `1px solid ${getCustomBorderColor(buttonVariant, buttonColor)}`
|
2021-10-12 08:04:51 +00:00
|
|
|
: buttonVariant === ButtonVariantTypes.SECONDARY
|
2021-11-06 08:00:57 +00:00
|
|
|
? `1px solid ${theme.colors.button.primary.secondary.borderColor}`
|
2021-08-24 13:53:15 +00:00
|
|
|
: "none"
|
|
|
|
|
} !important;
|
|
|
|
|
|
|
|
|
|
& > span {
|
|
|
|
|
max-height: 100%;
|
2020-01-20 08:07:00 +00:00
|
|
|
max-width: 99%;
|
2020-12-07 07:18:53 +00:00
|
|
|
text-overflow: ellipsis;
|
|
|
|
|
overflow: hidden;
|
|
|
|
|
display: -webkit-box;
|
|
|
|
|
-webkit-line-clamp: 1;
|
|
|
|
|
-webkit-box-orient: vertical;
|
2021-08-16 12:00:11 +00:00
|
|
|
|
2021-08-24 13:53:15 +00:00
|
|
|
color: ${
|
2021-10-12 08:04:51 +00:00
|
|
|
buttonVariant === ButtonVariantTypes.PRIMARY
|
2021-09-23 15:14:24 +00:00
|
|
|
? getCustomTextColor(theme, buttonColor)
|
2021-10-12 08:04:51 +00:00
|
|
|
: getCustomBackgroundColor(ButtonVariantTypes.PRIMARY, buttonColor)
|
2021-08-24 13:53:15 +00:00
|
|
|
} !important;
|
2019-10-21 15:12:45 +00:00
|
|
|
}
|
2021-08-24 13:53:15 +00:00
|
|
|
`}
|
2020-08-10 06:26:41 +00:00
|
|
|
|
2021-08-24 13:53:15 +00:00
|
|
|
border-radius: ${({ borderRadius }) =>
|
|
|
|
|
borderRadius === ButtonBorderRadiusTypes.ROUNDED ? "5px" : 0};
|
|
|
|
|
|
|
|
|
|
box-shadow: ${({ boxShadow, boxShadowColor, theme }) =>
|
|
|
|
|
boxShadow === ButtonBoxShadowTypes.VARIANT1
|
|
|
|
|
? `0px 0px 4px 3px ${boxShadowColor ||
|
|
|
|
|
theme.colors.button.boxShadow.default.variant1}`
|
|
|
|
|
: boxShadow === ButtonBoxShadowTypes.VARIANT2
|
|
|
|
|
? `3px 3px 4px ${boxShadowColor ||
|
|
|
|
|
theme.colors.button.boxShadow.default.variant2}`
|
|
|
|
|
: boxShadow === ButtonBoxShadowTypes.VARIANT3
|
|
|
|
|
? `0px 1px 3px ${boxShadowColor ||
|
|
|
|
|
theme.colors.button.boxShadow.default.variant3}`
|
|
|
|
|
: boxShadow === ButtonBoxShadowTypes.VARIANT4
|
|
|
|
|
? `2px 2px 0px ${boxShadowColor ||
|
|
|
|
|
theme.colors.button.boxShadow.default.variant4}`
|
|
|
|
|
: boxShadow === ButtonBoxShadowTypes.VARIANT5
|
|
|
|
|
? `-2px -2px 0px ${boxShadowColor ||
|
|
|
|
|
theme.colors.button.boxShadow.default.variant5}`
|
|
|
|
|
: "none"} !important;
|
2021-12-08 13:11:13 +00:00
|
|
|
|
|
|
|
|
${({ placement }) =>
|
|
|
|
|
placement
|
|
|
|
|
? `
|
|
|
|
|
justify-content: ${getCustomJustifyContent(placement)};
|
|
|
|
|
& > span.bp3-button-text {
|
|
|
|
|
flex: unset !important;
|
|
|
|
|
}
|
|
|
|
|
`
|
|
|
|
|
: ""}
|
2021-08-24 13:53:15 +00:00
|
|
|
`;
|
2019-10-21 15:12:45 +00:00
|
|
|
|
|
|
|
|
type ButtonStyleProps = {
|
2021-08-24 13:53:15 +00:00
|
|
|
buttonColor?: string;
|
|
|
|
|
buttonVariant?: ButtonVariant;
|
|
|
|
|
boxShadow?: ButtonBoxShadow;
|
|
|
|
|
boxShadowColor?: string;
|
|
|
|
|
borderRadius?: ButtonBorderRadius;
|
|
|
|
|
iconName?: IconName;
|
|
|
|
|
iconAlign?: Alignment;
|
2021-12-08 13:11:13 +00:00
|
|
|
placement?: ButtonPlacement;
|
2019-10-21 15:12:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// To be used in any other part of the app
|
2021-04-28 10:28:39 +00:00
|
|
|
export function BaseButton(props: IButtonProps & ButtonStyleProps) {
|
2021-08-24 13:53:15 +00:00
|
|
|
const {
|
|
|
|
|
borderRadius,
|
|
|
|
|
boxShadow,
|
|
|
|
|
boxShadowColor,
|
|
|
|
|
buttonColor,
|
|
|
|
|
buttonVariant,
|
|
|
|
|
className,
|
|
|
|
|
disabled,
|
|
|
|
|
icon,
|
|
|
|
|
iconAlign,
|
|
|
|
|
iconName,
|
|
|
|
|
loading,
|
|
|
|
|
onClick,
|
2021-12-08 13:11:13 +00:00
|
|
|
placement,
|
2021-08-24 13:53:15 +00:00
|
|
|
rightIcon,
|
|
|
|
|
text,
|
|
|
|
|
} = props;
|
|
|
|
|
|
2021-12-08 13:11:13 +00:00
|
|
|
const isRightAlign = iconAlign === Alignment.RIGHT;
|
2021-08-24 13:53:15 +00:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<StyledButton
|
2021-12-08 13:11:13 +00:00
|
|
|
alignText={getAlignText(isRightAlign, iconName)}
|
2021-08-24 13:53:15 +00:00
|
|
|
borderRadius={borderRadius}
|
|
|
|
|
boxShadow={boxShadow}
|
|
|
|
|
boxShadowColor={boxShadowColor}
|
|
|
|
|
buttonColor={buttonColor}
|
|
|
|
|
buttonVariant={buttonVariant}
|
|
|
|
|
className={className}
|
2021-10-12 08:04:51 +00:00
|
|
|
data-test-variant={buttonVariant}
|
2021-08-24 13:53:15 +00:00
|
|
|
disabled={disabled}
|
|
|
|
|
fill
|
2021-12-08 13:11:13 +00:00
|
|
|
icon={isRightAlign ? icon : iconName || icon}
|
2021-08-24 13:53:15 +00:00
|
|
|
loading={loading}
|
|
|
|
|
onClick={onClick}
|
2021-12-08 13:11:13 +00:00
|
|
|
placement={placement}
|
|
|
|
|
rightIcon={isRightAlign ? iconName || rightIcon : rightIcon}
|
2021-08-24 13:53:15 +00:00
|
|
|
text={text}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2021-04-28 10:28:39 +00:00
|
|
|
}
|
2019-10-21 15:12:45 +00:00
|
|
|
|
|
|
|
|
BaseButton.defaultProps = {
|
2021-09-23 15:14:24 +00:00
|
|
|
buttonColor: Colors.GREEN,
|
2021-10-12 08:04:51 +00:00
|
|
|
buttonVariant: ButtonVariantTypes.PRIMARY,
|
Property Pane Controls
- Fixes #121, #122, #123, #124, #90, #46, #65, #100, #101, #68, #102
2019-10-24 05:24:45 +00:00
|
|
|
disabled: false,
|
2019-10-21 15:12:45 +00:00
|
|
|
text: "Button Text",
|
|
|
|
|
minimal: true,
|
|
|
|
|
};
|
|
|
|
|
|
2020-03-06 09:45:21 +00:00
|
|
|
export enum ButtonType {
|
|
|
|
|
SUBMIT = "submit",
|
|
|
|
|
RESET = "reset",
|
|
|
|
|
BUTTON = "button",
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-12 13:01:19 +00:00
|
|
|
interface RecaptchaProps {
|
|
|
|
|
googleRecaptchaKey?: string;
|
|
|
|
|
clickWithRecaptcha: (token: string) => void;
|
2021-08-03 13:36:03 +00:00
|
|
|
handleRecaptchaV2Loading?: (isLoading: boolean) => void;
|
2021-07-08 12:02:08 +00:00
|
|
|
recaptchaV2?: boolean;
|
2020-10-12 13:01:19 +00:00
|
|
|
}
|
|
|
|
|
|
2021-08-24 13:53:15 +00:00
|
|
|
interface ButtonComponentProps extends ComponentProps {
|
2020-01-03 13:40:31 +00:00
|
|
|
text?: string;
|
2021-08-24 13:53:15 +00:00
|
|
|
icon?: IconName | MaybeElement;
|
2021-08-17 11:26:05 +00:00
|
|
|
tooltip?: string;
|
2019-10-21 15:12:45 +00:00
|
|
|
onClick?: (event: React.MouseEvent<HTMLElement>) => void;
|
2021-08-24 13:53:15 +00:00
|
|
|
isDisabled?: boolean;
|
2019-12-03 04:41:10 +00:00
|
|
|
isLoading: boolean;
|
2020-02-12 08:23:50 +00:00
|
|
|
rightIcon?: IconName | MaybeElement;
|
2020-03-06 09:45:21 +00:00
|
|
|
type: ButtonType;
|
2021-08-24 13:53:15 +00:00
|
|
|
buttonColor?: string;
|
|
|
|
|
buttonVariant?: ButtonVariant;
|
|
|
|
|
borderRadius?: ButtonBorderRadius;
|
|
|
|
|
boxShadow?: ButtonBoxShadow;
|
|
|
|
|
boxShadowColor?: string;
|
|
|
|
|
iconName?: IconName;
|
|
|
|
|
iconAlign?: Alignment;
|
2021-12-08 13:11:13 +00:00
|
|
|
placement?: ButtonPlacement;
|
2019-10-21 15:12:45 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-08 12:02:08 +00:00
|
|
|
function RecaptchaV2Component(
|
2020-10-12 13:01:19 +00:00
|
|
|
props: {
|
|
|
|
|
children: any;
|
|
|
|
|
onClick?: (event: React.MouseEvent<HTMLElement>) => void;
|
2021-07-08 12:02:08 +00:00
|
|
|
recaptchaV2?: boolean;
|
|
|
|
|
handleError: (event: React.MouseEvent<HTMLElement>, error: string) => void;
|
2020-10-12 13:01:19 +00:00
|
|
|
} & RecaptchaProps,
|
2021-04-28 10:28:39 +00:00
|
|
|
) {
|
2021-07-08 12:02:08 +00:00
|
|
|
const recaptchaRef = useRef<ReCAPTCHA>(null);
|
|
|
|
|
const [isInvalidKey, setInvalidKey] = useState(false);
|
2021-08-03 13:36:03 +00:00
|
|
|
const handleRecaptchaLoading = (isloading: boolean) => {
|
|
|
|
|
props.handleRecaptchaV2Loading && props.handleRecaptchaV2Loading(isloading);
|
|
|
|
|
};
|
2021-07-08 12:02:08 +00:00
|
|
|
const handleBtnClick = async (event: React.MouseEvent<HTMLElement>) => {
|
|
|
|
|
if (isInvalidKey) {
|
|
|
|
|
// Handle incorrent google recaptcha site key
|
|
|
|
|
props.handleError(event, createMessage(GOOGLE_RECAPTCHA_KEY_ERROR));
|
|
|
|
|
} else {
|
2021-08-03 13:36:03 +00:00
|
|
|
handleRecaptchaLoading(true);
|
2021-07-08 12:02:08 +00:00
|
|
|
try {
|
2021-08-03 13:36:03 +00:00
|
|
|
await recaptchaRef?.current?.reset();
|
2021-07-08 12:02:08 +00:00
|
|
|
const token = await recaptchaRef?.current?.executeAsync();
|
|
|
|
|
if (token) {
|
|
|
|
|
props.clickWithRecaptcha(token);
|
|
|
|
|
} else {
|
|
|
|
|
// Handle incorrent google recaptcha site key
|
|
|
|
|
props.handleError(event, createMessage(GOOGLE_RECAPTCHA_KEY_ERROR));
|
|
|
|
|
}
|
2021-08-03 13:36:03 +00:00
|
|
|
handleRecaptchaLoading(false);
|
2021-07-08 12:02:08 +00:00
|
|
|
} catch (err) {
|
2021-08-03 13:36:03 +00:00
|
|
|
handleRecaptchaLoading(false);
|
2021-07-08 12:02:08 +00:00
|
|
|
// Handle error due to google recaptcha key of different domain
|
|
|
|
|
props.handleError(event, createMessage(GOOGLE_RECAPTCHA_DOMAIN_ERROR));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
return (
|
|
|
|
|
<RecaptchaWrapper onClick={handleBtnClick}>
|
|
|
|
|
{props.children}
|
|
|
|
|
<ReCAPTCHA
|
|
|
|
|
onErrored={() => setInvalidKey(true)}
|
|
|
|
|
ref={recaptchaRef}
|
|
|
|
|
sitekey={props.googleRecaptchaKey || ""}
|
|
|
|
|
size="invisible"
|
|
|
|
|
/>
|
|
|
|
|
</RecaptchaWrapper>
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-05-20 19:28:02 +00:00
|
|
|
|
2021-07-08 12:02:08 +00:00
|
|
|
function RecaptchaV3Component(
|
|
|
|
|
props: {
|
|
|
|
|
children: any;
|
|
|
|
|
onClick?: (event: React.MouseEvent<HTMLElement>) => void;
|
|
|
|
|
recaptchaV2?: boolean;
|
|
|
|
|
handleError: (event: React.MouseEvent<HTMLElement>, error: string) => void;
|
|
|
|
|
} & RecaptchaProps,
|
|
|
|
|
) {
|
2021-05-20 19:28:02 +00:00
|
|
|
// Check if a string is a valid JSON string
|
|
|
|
|
const checkValidJson = (inputString: string): boolean => {
|
2021-08-09 12:18:58 +00:00
|
|
|
return !inputString.includes('"');
|
2021-05-20 19:28:02 +00:00
|
|
|
};
|
|
|
|
|
|
2021-07-08 12:02:08 +00:00
|
|
|
const handleBtnClick = (event: React.MouseEvent<HTMLElement>) => {
|
|
|
|
|
if (status === ScriptStatus.READY) {
|
|
|
|
|
(window as any).grecaptcha.ready(() => {
|
|
|
|
|
try {
|
|
|
|
|
(window as any).grecaptcha
|
|
|
|
|
.execute(props.googleRecaptchaKey, {
|
|
|
|
|
action: "submit",
|
|
|
|
|
})
|
|
|
|
|
.then((token: any) => {
|
|
|
|
|
props.clickWithRecaptcha(token);
|
|
|
|
|
})
|
|
|
|
|
.catch(() => {
|
|
|
|
|
// Handle incorrent google recaptcha site key
|
|
|
|
|
props.handleError(
|
|
|
|
|
event,
|
|
|
|
|
createMessage(GOOGLE_RECAPTCHA_KEY_ERROR),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
// Handle error due to google recaptcha key of different domain
|
|
|
|
|
props.handleError(
|
|
|
|
|
event,
|
|
|
|
|
createMessage(GOOGLE_RECAPTCHA_DOMAIN_ERROR),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-20 19:28:02 +00:00
|
|
|
let validGoogleRecaptchaKey = props.googleRecaptchaKey;
|
2021-08-09 12:18:58 +00:00
|
|
|
if (validGoogleRecaptchaKey && !checkValidJson(validGoogleRecaptchaKey)) {
|
2021-05-20 19:28:02 +00:00
|
|
|
validGoogleRecaptchaKey = undefined;
|
|
|
|
|
}
|
2020-10-12 13:01:19 +00:00
|
|
|
const status = useScript(
|
2021-05-20 19:28:02 +00:00
|
|
|
`https://www.google.com/recaptcha/api.js?render=${validGoogleRecaptchaKey}`,
|
2020-10-12 13:01:19 +00:00
|
|
|
);
|
2021-07-08 12:02:08 +00:00
|
|
|
return <div onClick={handleBtnClick}>{props.children}</div>;
|
2021-04-28 10:28:39 +00:00
|
|
|
}
|
2020-10-12 13:01:19 +00:00
|
|
|
|
2021-04-28 10:28:39 +00:00
|
|
|
function BtnWrapper(
|
2020-10-12 13:01:19 +00:00
|
|
|
props: {
|
|
|
|
|
children: any;
|
|
|
|
|
onClick?: (event: React.MouseEvent<HTMLElement>) => void;
|
|
|
|
|
} & RecaptchaProps,
|
2021-04-28 10:28:39 +00:00
|
|
|
) {
|
2020-10-12 13:01:19 +00:00
|
|
|
if (!props.googleRecaptchaKey)
|
|
|
|
|
return <div onClick={props.onClick}>{props.children}</div>;
|
2021-07-08 12:02:08 +00:00
|
|
|
else {
|
|
|
|
|
const handleError = (
|
|
|
|
|
event: React.MouseEvent<HTMLElement>,
|
|
|
|
|
error: string,
|
|
|
|
|
) => {
|
|
|
|
|
Toaster.show({
|
|
|
|
|
text: error,
|
|
|
|
|
variant: Variant.danger,
|
|
|
|
|
});
|
|
|
|
|
props.onClick && props.onClick(event);
|
|
|
|
|
};
|
|
|
|
|
if (props.recaptchaV2) {
|
|
|
|
|
return <RecaptchaV2Component {...props} handleError={handleError} />;
|
|
|
|
|
} else {
|
|
|
|
|
return <RecaptchaV3Component {...props} handleError={handleError} />;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-28 10:28:39 +00:00
|
|
|
}
|
2020-10-12 13:01:19 +00:00
|
|
|
|
2019-10-21 15:12:45 +00:00
|
|
|
// To be used with the canvas
|
2021-08-24 13:53:15 +00:00
|
|
|
function ButtonComponent(props: ButtonComponentProps & RecaptchaProps) {
|
2021-08-17 11:26:05 +00:00
|
|
|
const btnWrapper = (
|
2020-10-12 13:01:19 +00:00
|
|
|
<BtnWrapper
|
|
|
|
|
clickWithRecaptcha={props.clickWithRecaptcha}
|
2021-04-28 10:28:39 +00:00
|
|
|
googleRecaptchaKey={props.googleRecaptchaKey}
|
2021-08-03 13:36:03 +00:00
|
|
|
handleRecaptchaV2Loading={props.handleRecaptchaV2Loading}
|
2019-11-13 07:00:25 +00:00
|
|
|
onClick={props.onClick}
|
2021-07-08 12:02:08 +00:00
|
|
|
recaptchaV2={props.recaptchaV2}
|
2020-10-12 13:01:19 +00:00
|
|
|
>
|
2021-11-18 10:22:04 +00:00
|
|
|
<ButtonContainer disabled={props.isDisabled}>
|
2021-08-24 13:53:15 +00:00
|
|
|
<BaseButton
|
|
|
|
|
borderRadius={props.borderRadius}
|
|
|
|
|
boxShadow={props.boxShadow}
|
|
|
|
|
boxShadowColor={props.boxShadowColor}
|
|
|
|
|
buttonColor={props.buttonColor}
|
|
|
|
|
buttonVariant={props.buttonVariant}
|
|
|
|
|
disabled={props.isDisabled}
|
|
|
|
|
icon={props.icon}
|
|
|
|
|
iconAlign={props.iconAlign}
|
|
|
|
|
iconName={props.iconName}
|
|
|
|
|
loading={props.isLoading}
|
2021-12-08 13:11:13 +00:00
|
|
|
placement={props.placement}
|
2021-08-24 13:53:15 +00:00
|
|
|
rightIcon={props.rightIcon}
|
|
|
|
|
text={props.text}
|
|
|
|
|
type={props.type}
|
|
|
|
|
/>
|
|
|
|
|
</ButtonContainer>
|
2020-10-12 13:01:19 +00:00
|
|
|
</BtnWrapper>
|
2019-10-21 15:12:45 +00:00
|
|
|
);
|
2021-08-17 11:26:05 +00:00
|
|
|
if (props.tooltip) {
|
|
|
|
|
return (
|
|
|
|
|
<ToolTipWrapper>
|
2021-11-03 03:54:19 +00:00
|
|
|
<TooltipStyles />
|
|
|
|
|
<Popover2
|
|
|
|
|
autoFocus={false}
|
|
|
|
|
content={<Interweave content={props.tooltip} />}
|
2021-10-08 14:28:59 +00:00
|
|
|
disabled={props.isDisabled}
|
2021-08-17 11:26:05 +00:00
|
|
|
hoverOpenDelay={200}
|
2021-11-03 03:54:19 +00:00
|
|
|
interactionKind="hover"
|
|
|
|
|
portalClassName="btnTooltipContainer"
|
2021-08-17 11:26:05 +00:00
|
|
|
position={Position.TOP}
|
|
|
|
|
>
|
|
|
|
|
{btnWrapper}
|
2021-11-03 03:54:19 +00:00
|
|
|
</Popover2>
|
2021-08-17 11:26:05 +00:00
|
|
|
</ToolTipWrapper>
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
return btnWrapper;
|
|
|
|
|
}
|
2021-04-28 10:28:39 +00:00
|
|
|
}
|
2019-10-21 15:12:45 +00:00
|
|
|
|
2021-08-24 13:53:15 +00:00
|
|
|
export default ButtonComponent;
|