Skip to content

Commit

Permalink
feat: add forms configs to PageConstructorProvider (#858)
Browse files Browse the repository at this point in the history
* feat: add forms configs to PageConstructorProvider

* fixup! feat: add forms configs to PageConstructorProvider
  • Loading branch information
aapolkovsky authored Mar 13, 2024
1 parent 884af20 commit 2736947
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/components/InnerForm/InnerForm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import React, {useEffect} from 'react';
import React, {useContext, useEffect} from 'react';

import {YandexForm} from '..';
import {
FormsContext,
HubspotFormsContextProps,
YandexFormsContextProps,
} from '../../context/formsContext/FormsContext';
import {FormBlockData, isHubspotDataForm, isYandexDataForm} from '../../models';
import {HubspotForm} from '../../sub-blocks';

Expand All @@ -12,6 +17,7 @@ interface InnerFormProps {

const InnerForm: React.FC<InnerFormProps> = (props) => {
const {formData, onContentLoad, className} = props;
const formsConfig = useContext(FormsContext);

useEffect(() => {
if (isHubspotDataForm(formData)) {
Expand All @@ -21,9 +27,11 @@ const InnerForm: React.FC<InnerFormProps> = (props) => {

if (isYandexDataForm(formData)) {
const {onLoad, ...rest} = formData.yandex;

return (
<div className={className}>
<YandexForm
{...(formsConfig.yandex as YandexFormsContextProps | undefined)}
{...rest}
onLoad={() => {
onContentLoad();
Expand All @@ -35,7 +43,13 @@ const InnerForm: React.FC<InnerFormProps> = (props) => {
}

if (isHubspotDataForm(formData)) {
return <HubspotForm createDOMElement={true} {...formData.hubspot} />;
return (
<HubspotForm
createDOMElement={true}
{...(formsConfig.hubspot as HubspotFormsContextProps | undefined)}
{...formData.hubspot}
/>
);
}

return null;
Expand Down
8 changes: 8 additions & 0 deletions src/containers/PageConstructor/Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import React, {Fragment} from 'react';

import {DEFAULT_THEME} from '../../components/constants';
import {AnalyticsContext, AnalyticsContextProps} from '../../context/analyticsContext';
import {
DEFAULT_FORMS_CONTEXT_VALUE,
FormsContext,
FormsContextProps,
} from '../../context/formsContext/FormsContext';
import {ImageContext, ImageContextProps} from '../../context/imageContext';
import {LocaleContext, LocaleContextProps} from '../../context/localeContext';
import {LocationContext, LocationContextProps} from '../../context/locationContext';
Expand All @@ -24,6 +29,7 @@ export interface PageConstructorProviderProps {
mapsContext?: MapsContextType;
projectSettings?: ProjectSettingsContextProps;
analytics?: AnalyticsContextProps;
forms?: FormsContextProps;
image?: ImageContextProps;
}

Expand All @@ -39,6 +45,7 @@ export const PageConstructorProvider = (props: WithChildren<PageConstructorProvi
theme = DEFAULT_THEME,
children,
image = {},
forms = DEFAULT_FORMS_CONTEXT_VALUE,
} = props;

/* eslint-disable react/jsx-key */
Expand All @@ -51,6 +58,7 @@ export const PageConstructorProvider = (props: WithChildren<PageConstructorProvi
<MobileContext.Provider value={Boolean(isMobile)} />,
<MapsContext.Provider value={mapsContext} />,
<AnalyticsContext.Provider value={analytics} />,
<FormsContext.Provider value={forms} />,
<SSRContext.Provider value={{isServer: ssrConfig?.isServer}} />,
].reduceRight((prev, provider) => React.cloneElement(provider, {}, prev), children);
/* eslint-enable react/jsx-key */
Expand Down
19 changes: 19 additions & 0 deletions src/context/formsContext/FormsContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react';

import {YandexFormProps} from '../../models/constructor-items/common';
import {HubspotFormProps} from '../../models/constructor-items/sub-blocks';

export const DEFAULT_FORMS_CONTEXT_VALUE: FormsContextProps = {};

export interface FormsContextProps {
yandex?: YandexFormsContextProps;
hubspot?: HubspotFormsContextProps;
}

export interface YandexFormsContextProps
extends Partial<Pick<YandexFormProps, 'customFormOrigin'>> {}

export interface HubspotFormsContextProps
extends Partial<Pick<HubspotFormProps, 'createDOMElement'>> {}

export const FormsContext = React.createContext<FormsContextProps>(DEFAULT_FORMS_CONTEXT_VALUE);
1 change: 1 addition & 0 deletions src/context/formsContext/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './FormsContext';

0 comments on commit 2736947

Please sign in to comment.