import { useEffect, useState } from "react" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Switch } from "@/components/ui/switch" import { Label } from "@/components/ui/label" import { Checkbox } from "@/components/ui/checkbox" import { Badge } from "@/components/ui/badge" import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select" import { ShoppingCart, ExternalLink, KeyRound } from "lucide-react" import { SettingsGrid } from "@/components/settings-layout" import { usePro } from "@/contexts/pro-context" import { ProIndicator } from "@/components/pro/pro-indicator" import { Input } from "@/components/ui/input" import { EmailVerificationSettings, expiryOptions, autoDeleteOptions, resendLimitOptions, wordpressRoles, verificationModeOptions, } from "../../config" interface SettingsTabProps { settings: EmailVerificationSettings updateSettings: (key: K, value: EmailVerificationSettings[K]) => void toggleRole: (role: string) => void } interface RegistrationFormState { enabled: boolean applyToWordPress: boolean applyToWooCommerce: boolean } export function SettingsTab({ settings, updateSettings, toggleRole }: SettingsTabProps) { const { isPro } = usePro() const [registrationForm, setRegistrationForm] = useState({ enabled: false, applyToWordPress: false, applyToWooCommerce: false, }) useEffect(() => { const loadRegistrationFormSettings = async () => { try { const apiUrl = window.swiftCommerceData?.apiUrl || '/wp-json/swift-commerce/v1' const response = await fetch(`${apiUrl}/registration-form/settings`, { headers: { 'X-WP-Nonce': window.swiftCommerceData?.restNonce || '', }, credentials: 'same-origin', }) if (!response.ok) return const data = await response.json() if (data.success && data.settings) { setRegistrationForm({ enabled: Boolean(data.settings.enabled), applyToWordPress: Boolean(data.settings.applyToWordPress), applyToWooCommerce: Boolean(data.settings.applyToWooCommerce), }) } } catch (error) { console.error('Failed to load registration form status:', error) } } loadRegistrationFormSettings() }, []) const hasRegistrationFormConflict = settings.enabled && registrationForm.enabled && ( (settings.enableForRegistration && registrationForm.applyToWordPress) || (settings.enableForWooCommerceRegistration && registrationForm.applyToWooCommerce) ) const showsPasswordFlowNotice = settings.enabled && settings.verificationMode === 'email_verification' && (settings.enableForRegistration || settings.enableForWooCommerceRegistration) return ( {/* Verification Mode - FREE shows Email Verification only, Pro adds Admin Approval */} Verification Mode Choose how new user registrations are verified
{verificationModeOptions.map((mode) => { const isAdminApproval = mode.value === 'admin_approval' // Hide Admin Approval option entirely for Free users if (isAdminApproval && !isPro) return null const isSelected = settings.verificationMode === mode.value return (
updateSettings('verificationMode', mode.value as EmailVerificationSettings['verificationMode'])} >
{isSelected &&
}
{mode.label} {isAdminApproval && }

{mode.description}

) })}
{/* Core Settings - FREE */} Verification Settings Configure when email verification is required {hasRegistrationFormConflict && (

When enabled, Registration Form field order and custom fields appear on active registration forms. When disabled, Email Verification shows its default password setup fields.

updateSettings('registrationFormMode', checked ? 'registration_form' : 'email_verification')} />

Active form: {settings.registrationFormMode === 'registration_form' ? 'Registration Form fields' : 'Email Verification default fields'}

)} {showsPasswordFlowNotice && ( Password setup is handled during registration Swift Commerce asks new users to create their password before sending the verification email. After they verify their email, they can log in with that password. )} {/* Enable for WordPress Registration - FREE */}

Require email verification for WordPress default registration form

updateSettings('enableForRegistration', checked)} />
{/* Enable for WooCommerce My Account - FREE */}
WooCommerce

Require email verification for WooCommerce My Account registration

updateSettings('enableForWooCommerceRegistration', checked)} />
{/* Enable for Logged-in Checkout - Pro only, hidden for Free */} {isPro && (
WooCommerce

Require email verification for logged-in users at checkout

updateSettings('enableForLoggedInCheckout', checked)} />
)}
{/* Skip Roles - FREE */} Skip Verification for Roles Select user roles that should bypass email verification
{wordpressRoles.map((role) => { const isChecked = settings.skipRoles.includes(role.value) return (
toggleRole(role.value)} > toggleRole(role.value)} />
) })}

Users with selected roles will not need to verify their email address.

{/* Link Settings - Pro only, hidden for Free */} {isPro && ( Link & Limits Configure verification link expiry and limits {/* Link Expiry */}

How long the verification link remains valid

{/* Resend Limit */}

Maximum number of times a user can request a new verification email

{/* Auto Delete */}

Automatically delete users who don't verify their email within this time

{/* Redirect After Verification */}
updateSettings('redirectAfterVerification', e.target.value)} />

Custom URL to redirect users to after they verify their email. Leave empty to use the default login or My Account page.

)} ) }