import PasswordInput from "@/common/PasswordInput"; import TransitionWrapper from "@/common/TransitionWrapper"; import Button from "@/components/Button"; import { Input } from "@/components/ui/input"; import { spcApi } from "@/lib/api"; import { useSettingsStore } from "@/store/optionsStore"; import { useAppStore } from "@/store/store"; import ManagedSettingNotice from "./ManagedSettingNotice"; import { useConnectionStore } from "./connectionStore"; import { useState } from "@wordpress/element"; import { __ } from "@wordpress/i18n"; import { Globe } from "lucide-react"; const ApiKeyForm = () => { const { setErrorMessage } = useConnectionStore(); const { asyncLocked, lockAsync } = useAppStore(); const { settings, updateSettings, isSettingOverridden } = useSettingsStore(); const storedApiKeyExists = Boolean(window.SPCDash.settings?.cf_apikey?.hasValue); const [isConnecting, setIsConnecting] = useState(false); const [formData, setFormData] = useState({ email: settings.cf_email as string, apiKey: settings.cf_apikey as string, }); const emailManaged = isSettingOverridden('cf_email'); const apiKeyManaged = isSettingOverridden('cf_apikey'); const getZoneDomainList = async (e: React.FormEvent) => { setErrorMessage(''); e.preventDefault(); lockAsync(true); setIsConnecting(true); const data = { auth_mode: 'api_key', email: formData.email, api_key: formData.apiKey, } const response = await spcApi.cloudflareConnect(data); lockAsync(false); setIsConnecting(false); if (response.success) { updateSettings(response.data.settings, response.data.meta); return; } setErrorMessage(response.message); } const isFormInvalid = (!formData.email || !formData.apiKey) || emailManaged || apiKeyManaged; return (
{ setFormData({ ...formData, email: e.target.value }); }} className="w-full max-w-full h-10 m-0" placeholder="user@example.com" />

{__('The email address you use to log in to Cloudflare', 'wp-cloudflare-page-cache')}

{ setFormData({ ...formData, apiKey: e.target.value }); }} />

{__('To get your API credentials:', 'wp-cloudflare-page-cache')}

  1. {__('Log in to your Cloudflare account and click on "My Profile"', 'wp-cloudflare-page-cache')}
  2. {__('Click on API Tokens and scroll to the bottom and click on "View beside Global API Key"', 'wp-cloudflare-page-cache')}
  3. {__('Enter your Cloudflare login password and click on "View"', 'wp-cloudflare-page-cache')}
  4. {__('Copy the API key and paste it in the form below', 'wp-cloudflare-page-cache')}
) } export default ApiKeyForm;