/* eslint-disable react-native/no-inline-styles */ import { ImageBackground, Pressable, ScrollView, View } from 'react-native'; import { useEffect } from 'react'; import type { ReactNode } from 'react'; import type { ContentItem, SectionProperty } from '../types'; import { RenderElement } from './element'; import { onClick } from '../utils/onClick'; import { Fragment } from 'react/jsx-runtime'; import { getLayoutStyles, getViewStyles } from '../utils/styleKeys'; import { type ContentViewItem } from '../store/contentModel'; import { getChildren } from '../utils'; import Resync from 'resync-javascript'; import { FormRenderer } from '../FormElements'; export const RenderSection = ({ section, contentItem, dataItem, formValues, formHandlers, formState, }: { section?: ContentItem; contentItem?: ContentViewItem; dataItem?: Record; formValues?: Record; formHandlers?: { handleChange: (field: string) => (value: any) => void; handleBlur: (field: string) => () => void; handleSubmit: () => void; setFieldTouched: (field: string) => void; }; formState?: { errors: Record; touched: Record; isSubmitting: boolean; }; }): ReactNode => { useEffect(() => { if (section?.event && contentItem?.content?.id) { if (section.eventConfig?.action === 'view') { Resync.logEvent({ eventId: section.event?.eventId || '', logId: section.eventConfig?.logId || '', }); if (contentItem?.logAnalytics && section.eventConfig?.logId) { contentItem.logAnalytics(section.eventConfig?.logId, { itemId: section?.itemId, contentViewId: contentItem?.content?.id, ...section.eventConfig, }); } } } // eslint-disable-next-line react-hooks/exhaustive-deps }, []); if (!section || !contentItem) return null; const sectionData = section?.data as SectionProperty; const handleClick = (itemData?: Record) => { if (section.eventConfig?.action === 'click' && contentItem?.content?.id) { Resync.logEvent({ eventId: section.event?.eventId || '', logId: section.eventConfig?.logId || '', }); if (contentItem?.logAnalytics && section.eventConfig?.logId) { contentItem.logAnalytics(section.eventConfig?.logId, { itemId: section?.itemId, ...section.eventConfig, contentViewId: contentItem?.content?.id, metadata: itemData || dataItem, }); } } onClick({ clickAction: sectionData?.clickAction || null, functionRegistry: contentItem?.functionRegistry, navigationRegistry: contentItem?.navigationRegistry, dataItem: itemData || dataItem, }); }; // Get child elements of this section that are visible const children = getChildren(section?.itemId, contentItem?.content); // Check if section is scrollable const isScrollable = section.isScrollable; const scrollOptions = sectionData.scrollOptions || { scrollType: 'vertical' }; // Render children const renderChildren = () => ( <> {children.map((child) => ( {child.type === 'section' ? ( ) : [ 'input', 'select', 'checkbox', 'submit', 'radio', 'textarea', ].includes(child.elementType as string) ? ( // Render form element with form state {})} onBlur={() => formHandlers?.setFieldTouched(child.name) || (() => {}) } onSubmit={formHandlers?.handleSubmit || (() => {})} error={formState?.errors[child.name] as string} touched={formState?.touched[child.name] as boolean | undefined} disabled={formState?.isSubmitting} loading={formState?.isSubmitting} contentItem={contentItem} /> ) : ( )} ))} ); if (sectionData.backgroundImage) { return ( {renderChildren()} ); } // If scrollable, wrap in ScrollView if (isScrollable) { return ( {renderChildren()} ); } if (sectionData.clickAction) { return ( handleClick()} key={section.itemId} style={[ getLayoutStyles(sectionData.styles), getLayoutStyles(sectionData?.customStyles || {}), getViewStyles(sectionData.styles), getViewStyles(sectionData?.customStyles || {}), ]} > {renderChildren()} ); } // Non-scrollable section return ( {renderChildren()} ); };