"use client"; import React, { useEffect, useState } from "react"; import { Input, Button, AccordionItem, Accordion, Switch, } from "@nextui-org/react"; import { Eye, EyeOff } from "lucide-react"; import { StripeConfig } from "@/types/payment"; import { useTranslation } from "@/shared/lib/useTranslation"; interface StripeConfigFormProps { config: StripeConfig; onSave: (config: StripeConfig) => void; onEnable: (channelId: string) => Promise; onDisable: (channelId: string) => Promise; isLoading?: boolean; labelPlacement?: "outside" | "inside"; } export const StripeConfigForm: React.FC = ({ config, onSave, onEnable, onDisable, isLoading = false, labelPlacement = "inside", }) => { const { t } = useTranslation(); const [formData, setFormData] = useState(config); const [showSecretKey, setShowSecretKey] = useState(false); const [showWebhookSecret, setShowWebhookSecret] = useState(false); useEffect(() => { setFormData(config); }, [config]); const handleInputChange = ( field: keyof StripeConfig, value: string | boolean ) => { setFormData((prev) => ({ ...prev, [field]: value, })); }; const handleEnableDisable = async (value: boolean) => { let result: boolean | null = null; if (value) { result = await onEnable("stripe"); } else { result = await onDisable("stripe"); } if (result) { setFormData((prev) => ({ ...prev, is_enabled: value, })); } }; const handleSave = () => { onSave(formData); }; const isFormValid = formData?.secret?.trim() !== "" && formData?.webhook_secret?.trim() !== ""; const panelContent = () => { return ( <>
{/* Secret Key */} handleInputChange("secret", e.target.value)} type={showSecretKey ? "text" : "password"} endContent={ } isRequired autoComplete="off" isDisabled={!formData.is_enabled} labelPlacement="outside" /> {/* Webhook Secret (Optional) */} handleInputChange("webhook_secret", e.target.value) } type={showWebhookSecret ? "text" : "password"} isRequired endContent={ } autoComplete="off" isDisabled={!formData.is_enabled} labelPlacement="outside" />
{/* Action Buttons */} {formData.is_enabled && (
)} ); }; if (labelPlacement === "outside") { return (
{/* Enable/Disable Toggle */}
{t("Enable Stripe Payment")} {t("Toggle to enable or disable Stripe payment interface")}
{panelContent()}
); } return (

{t("Stripe Configuration")}

{t("Configure Stripe payment interface")}

} > {panelContent()}
); };