PromucFlow_constructor/app/client/src/widgets/FormButtonWidget.tsx
satbir121 8bf80fe507
Google Recaptcha integration for button and form button widgets. (#1118)
* Adding base code for google re-captcha.

* Removing recaptcha as well

* Adding recaptchaToken on Button.

* Fixing updateMetaProperty errors.

* Handling recaptcha generation failed case.

* Adding a message for recaptcha token gen fail

* Rename setRecaptchaToken

* Adding loading state for recaptcha button

* Adding googleRecaptchaKey as a Btn prop

* Adding the bound functions in widgets

* Removing unused vars.

* Handling google recaptcha key error.

* Adding proper messages for
2020-10-12 18:31:19 +05:30

153 lines
4.0 KiB
TypeScript

import React from "react";
import BaseWidget, { WidgetProps, WidgetState } from "./BaseWidget";
import { WidgetType } from "constants/WidgetConstants";
import ButtonComponent, {
ButtonType,
} from "components/designSystems/blueprint/ButtonComponent";
import { EventType, ExecutionResult } from "constants/ActionConstants";
import {
BASE_WIDGET_VALIDATION,
WidgetPropertyValidationType,
} from "utils/ValidationFactory";
import { VALIDATION_TYPES } from "constants/WidgetValidation";
import { TriggerPropertiesMap } from "utils/WidgetFactory";
import * as Sentry from "@sentry/react";
import withMeta, { WithMeta } from "./MetaHOC";
class FormButtonWidget extends BaseWidget<
FormButtonWidgetProps,
FormButtonWidgetState
> {
onButtonClickBound: (event: React.MouseEvent<HTMLElement>) => void;
clickWithRecaptchaBound: (token: string) => void;
constructor(props: FormButtonWidgetProps) {
super(props);
this.onButtonClickBound = this.onButtonClick.bind(this);
this.clickWithRecaptchaBound = this.clickWithRecaptcha.bind(this);
this.state = {
isLoading: false,
};
}
static getMetaPropertiesMap(): Record<string, any> {
return {
recaptchaToken: undefined,
};
}
static getPropertyValidationMap(): WidgetPropertyValidationType {
return {
...BASE_WIDGET_VALIDATION,
text: VALIDATION_TYPES.TEXT,
disabledWhenInvalid: VALIDATION_TYPES.BOOLEAN,
buttonStyle: VALIDATION_TYPES.TEXT,
buttonType: VALIDATION_TYPES.TEXT,
// onClick: VALIDATION_TYPES.ACTION_SELECTOR,
};
}
static getTriggerPropertyMap(): TriggerPropertiesMap {
return {
onClick: true,
};
}
clickWithRecaptcha(token: string) {
if (this.props.onClick) {
this.setState({
isLoading: true,
});
}
this.props.updateWidgetMetaProperty("recaptchaToken", token, {
dynamicString: this.props.onClick,
event: {
type: EventType.ON_CLICK,
callback: this.handleActionResult,
},
});
}
onButtonClick() {
if (this.props.onClick) {
this.setState({
isLoading: true,
});
super.executeAction({
dynamicString: this.props.onClick,
event: {
type: EventType.ON_CLICK,
callback: this.handleActionResult,
},
});
} else if (this.props.resetFormOnClick && this.props.onReset) {
this.props.onReset();
}
}
handleActionResult = (result: ExecutionResult) => {
this.setState({
isLoading: false,
});
if (result.success) {
if (this.props.resetFormOnClick && this.props.onReset)
this.props.onReset();
}
};
getPageView() {
const disabled =
this.props.disabledWhenInvalid &&
"isFormValid" in this.props &&
!this.props.isFormValid;
return (
<ButtonComponent
buttonStyle={this.props.buttonStyle}
widgetId={this.props.widgetId}
key={this.props.widgetId}
widgetName={this.props.widgetName}
text={this.props.text}
disabled={disabled}
onClick={this.onButtonClickBound}
isLoading={this.props.isLoading || this.state.isLoading}
type={this.props.buttonType || ButtonType.BUTTON}
googleRecaptchaKey={this.props.googleRecaptchaKey}
clickWithRecaptcha={this.clickWithRecaptchaBound}
/>
);
}
getWidgetType(): WidgetType {
return "FORM_BUTTON_WIDGET";
}
}
export type ButtonStyle =
| "PRIMARY_BUTTON"
| "SECONDARY_BUTTON"
| "SUCCESS_BUTTON"
| "DANGER_BUTTON";
export interface FormButtonWidgetProps extends WidgetProps, WithMeta {
text?: string;
buttonStyle?: ButtonStyle;
onClick?: string;
isVisible?: boolean;
buttonType: ButtonType;
isFormValid?: boolean;
resetFormOnClick?: boolean;
onReset?: () => void;
disabledWhenInvalid?: boolean;
googleRecaptchaKey?: string;
}
export interface FormButtonWidgetState extends WidgetState {
isLoading: boolean;
}
export default FormButtonWidget;
export const ProfiledFormButtonWidget = Sentry.withProfiler(
withMeta(FormButtonWidget),
);