import { useState } from "react"; import { AdminButton } from "@/components/admin/design-system"; import { type WholesaleSettings, type NotificationRecipient, saveWholesaleSettings, } from "@/actions/wholesale"; import AddRecipientForm from "./AddRecipientForm"; import WebhookSettingsSection from "./WebhookSettingsSection"; import type { MsgFn } from "./types"; interface SettingsTabProps { settings: WholesaleSettings | null; brandId: string; onMsg: MsgFn; onRefresh: () => void; canManageSettings: boolean; } // Wholesale portal settings: approval flow, payments, pickup location, invoice // details, Square sync toggle, team notification recipients, and outbound webhook. export default function SettingsTab({ settings, brandId, onMsg, onRefresh, canManageSettings }: SettingsTabProps) { // Hooks must be called unconditionally - always declare them first const [form, setForm] = useState({ requireApproval: settings?.require_approval ?? true, minOrderAmount: settings?.min_order_amount ?? "", onlinePaymentEnabled: settings?.online_payment_enabled ?? false, wholesaleEnabled: settings?.wholesale_enabled ?? true, squareSyncEnabled: settings?.square_sync_enabled ?? false, pickupLocation: settings?.pickup_location ?? "", fobLocation: settings?.fob_location ?? "", fromEmail: settings?.from_email ?? "", invoiceBusinessName: settings?.invoice_business_name ?? "", invoiceBusinessAddress: settings?.invoice_business_address ?? "", invoiceBusinessPhone: settings?.invoice_business_phone ?? "", invoiceBusinessEmail: settings?.invoice_business_email ?? "", invoiceBusinessWebsite: settings?.invoice_business_website ?? "", notificationRecipients: settings?.notification_recipients ?? [], }); const [saving, setSaving] = useState(false); // Permission check after hooks if (!canManageSettings) { return (

You do not have permission to manage settings.

); } async function handleSave() { setSaving(true); const result = await saveWholesaleSettings({ brandId, requireApproval: form.requireApproval, minOrderAmount: form.minOrderAmount ? Number(form.minOrderAmount) : undefined, onlinePaymentEnabled: form.onlinePaymentEnabled, wholesaleEnabled: form.wholesaleEnabled, squareSyncEnabled: form.squareSyncEnabled, pickupLocation: form.pickupLocation, fobLocation: form.fobLocation, fromEmail: form.fromEmail, invoiceBusinessName: form.invoiceBusinessName, invoiceBusinessAddress: form.invoiceBusinessAddress, invoiceBusinessPhone: form.invoiceBusinessPhone, invoiceBusinessWebsite: form.invoiceBusinessWebsite, notificationRecipients: form.notificationRecipients, }); setSaving(false); if (result.success) { onMsg("success", "Settings saved."); onRefresh(); } else { onMsg("error", result.error ?? "Failed to save settings."); } } // ── Notification Recipients helpers ──────────────────────────────────────── function addRecipient(email: string, name: string = "") { setForm(f => ({ ...f, notificationRecipients: [ ...f.notificationRecipients, { email, name, active: true }, ], })); } function removeRecipient(idx: number) { setForm(f => ({ ...f, notificationRecipients: f.notificationRecipients.filter((_: unknown, i: number) => i !== idx), })); } function toggleRecipient(idx: number) { setForm(f => ({ ...f, notificationRecipients: f.notificationRecipients.map((r: NotificationRecipient, i: number) => i === idx ? { ...r, active: !r.active } : r ), })); } function updateRecipientName(idx: number, name: string) { setForm(f => ({ ...f, notificationRecipients: f.notificationRecipients.map((r: NotificationRecipient, i: number) => i === idx ? { ...r, name } : r ), })); } return (

Wholesale Settings

Require Approval

New registrations must be manually approved.

Wholesale Portal

Show the Wholesale Portal card on this brand's storefront page.

Square Sync for Wholesale

When enabled, wholesale product changes are automatically pushed to Square. {form.squareSyncEnabled ? " Auto-sync is active." : " Auto-sync is disabled — changes will not be pushed to Square."}

setForm(f => ({ ...f, minOrderAmount: e.target.value }))} className="w-full rounded-xl border border-[var(--admin-border)] px-3 py-2 text-sm outline-none focus:border-[var(--admin-accent)]" step="0.01" placeholder="No minimum" aria-label="No minimum"/>
setForm(f => ({ ...f, fromEmail: e.target.value }))} className="w-full rounded-xl border border-[var(--admin-border)] px-3 py-2 text-sm outline-none focus:border-[var(--admin-accent)]" />