/** * Registration Form Settings Tab * * Global settings for the registration form editor */ import { useState, useEffect } from "react" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Switch } from "@/components/ui/switch" import { Button } from "@/components/ui/button" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select" import { Separator } from "@/components/ui/separator" import { Settings, Monitor, Layout, FileUp, Zap, ShieldCheck, UserCog, Plus, Trash2, } from "lucide-react" import { usePro } from "@/contexts/pro-context" import { ProIndicator } from "@/components/pro/pro-indicator" import { RegistrationFormSettings, LayoutStyle, FieldSpacing, LabelPosition, RequiredIndicatorPosition, RedirectType, RoleAssignmentRule, RoleAssignmentOperator, RegistrationField, } from "../../config" interface Page { id: number title: string } interface SettingsTabProps { settings: RegistrationFormSettings fields: RegistrationField[] onUpdateSettings: (updates: Partial) => void } interface WPRole { key: string name: string } export function SettingsTab({ settings, fields, onUpdateSettings }: SettingsTabProps) { const { isPro } = usePro() const [pages, setPages] = useState([]) const [loadingPages, setLoadingPages] = useState(false) const [roles, setRoles] = useState([]) const [loadingRoles, setLoadingRoles] = useState(false) // Load pages for redirect selection useEffect(() => { const loadPages = async () => { setLoadingPages(true) try { const apiUrl = window.swiftCommerceData?.apiUrl || '/wp-json/swift-commerce/v1' const response = await fetch(`${apiUrl}/pages`, { headers: { 'X-WP-Nonce': window.swiftCommerceData?.restNonce || '', }, credentials: 'same-origin', }) if (response.ok) { const data = await response.json() setPages(data.pages || []) } } catch (error) { console.error('Failed to load pages:', error) } finally { setLoadingPages(false) } } loadPages() }, []) // Load roles for role assignment useEffect(() => { if (!isPro) return const loadRoles = async () => { setLoadingRoles(true) try { const apiUrl = window.swiftCommerceData?.apiUrl || '/wp-json/swift-commerce/v1' const response = await fetch(`${apiUrl}/registration-form/roles`, { headers: { 'X-WP-Nonce': window.swiftCommerceData?.restNonce || '', }, credentials: 'same-origin', }) if (response.ok) { const data = await response.json() setRoles(Array.isArray(data) ? data : []) } } catch (error) { console.error('Failed to load roles:', error) } finally { setLoadingRoles(false) } } loadRoles() }, [isPro]) // Custom fields available for role rules (non-core, non-content fields) const assignableFields = fields.filter( (f) => !f.isCore && f.enabled && !['heading', 'paragraph', 'divider', 'file'].includes(f.type) ) const addRoleRule = () => { const newRule: RoleAssignmentRule = { id: `rule_${Date.now()}`, fieldKey: assignableFields[0]?.key || '', operator: 'is_not_empty', value: '', role: roles[0]?.key || 'subscriber', } onUpdateSettings({ roleAssignment: { ...settings.roleAssignment, rules: [...settings.roleAssignment.rules, newRule], }, }) } const updateRoleRule = (ruleId: string, updates: Partial) => { onUpdateSettings({ roleAssignment: { ...settings.roleAssignment, rules: settings.roleAssignment.rules.map((r) => r.id === ruleId ? { ...r, ...updates } : r ), }, }) } const deleteRoleRule = (ruleId: string) => { onUpdateSettings({ roleAssignment: { ...settings.roleAssignment, rules: settings.roleAssignment.rules.filter((r) => r.id !== ruleId), }, }) } return (
{/* Left Column */}
{/* Context Settings */} Form Context Choose where the custom registration form should be applied.

Apply to wp-login.php?action=register

onUpdateSettings({ applyToWordPress: checked })} />
{isPro && (

Apply to WooCommerce My Account registration form

onUpdateSettings({ applyToWooCommerce: checked })} />
)} {isPro && ( <>

Apply custom fields during checkout registration

onUpdateSettings({ applyToCheckout: checked })} />
)} {isPro && ( <>

Show custom fields in My Account details page

onUpdateSettings({ applyToMyAccount: checked })} />
)}
{/* Display Settings */} Display Settings Configure how fields are displayed in the registration form.
onUpdateSettings({ showLabels: checked })} />
onUpdateSettings({ showPlaceholders: checked })} />
onUpdateSettings({ showDescriptions: checked })} />
onUpdateSettings({ requiredIndicator: e.target.value })} placeholder="*" />
{/* Layout Settings */} Layout Settings Customize the layout and appearance of the registration form.
{/* Right Column */}
{/* Behavior Settings - Pro Only */} {isPro && ( Behavior Settings Configure post-registration behavior and features.
{settings.redirectType === 'url' && (
onUpdateSettings({ redirectAfterRegister: e.target.value })} placeholder="https://example.com/welcome" />
)} {settings.redirectType === 'page' && (
)}

Automatically log in users after successful registration

onUpdateSettings({ autoLogin: checked })} />
)} {/* Advanced Settings - Pro Only */} {isPro && ( Advanced Settings Configure file upload settings for registration forms.

Allow file upload fields in registration form

onUpdateSettings({ enableFileUploads: checked })} />
{settings.enableFileUploads && (
onUpdateSettings({ maxFileSize: parseInt(e.target.value) || 5 })} />
onUpdateSettings({ allowedFileTypes: e.target.value })} placeholder="jpg,jpeg,png,pdf" />
)}
)} {/* CAPTCHA Protection - Pro Only */} {isPro && ( CAPTCHA Protection Add CAPTCHA verification to registration forms using your configured reCAPTCHA, Turnstile, or hCaptcha provider.

Display a CAPTCHA challenge on all active registration forms

onUpdateSettings({ enableCaptcha: checked })} />
{settings.enableCaptcha && (

The CAPTCHA provider is configured in the reCAPTCHA feature settings. Make sure you have a provider set up with valid API keys.

)}
)} {/* Role Assignment - Pro Only */} {isPro && ( Role Assignment Automatically assign user roles based on registration form field values. Rules are evaluated top-to-bottom — first match wins.

Assign roles based on form data (e.g., Tax ID → Wholesale)

onUpdateSettings({ roleAssignment: { ...settings.roleAssignment, enabled: checked }, }) } />
{settings.roleAssignment.enabled && (
{/* Default role */}

Leave empty to use the WordPress default role setting.

{/* Rules list */}
{assignableFields.length === 0 && (

Add custom fields to the form first, then create rules here.

)} {settings.roleAssignment.rules.map((rule) => (
{/* Field */}
{/* Operator */}
{/* Value (hidden for empty/checked operators) */} {!['is_empty', 'is_not_empty', 'is_checked', 'is_not_checked'].includes( rule.operator ) && (
updateRoleRule(rule.id, { value: e.target.value })} placeholder="Match value" />
)} {/* Target role */}
{/* Delete */}
))}
)}
)}
) }