/** * WordPress Integration Form Component * Inline form for WordPress integration with API key generation and plugin download */ import React, { useState, useEffect } from 'react'; import { Card } from '../ui/card'; import Button from '../ui/button/Button'; import Label from '../form/Label'; import Input from '../form/input/InputField'; import Checkbox from '../form/input/Checkbox'; import { useToast } from '../ui/toast/ToastContainer'; import { integrationApi, SiteIntegration } from '../../services/integration.api'; import { fetchAPI } from '../../services/api'; import { CheckCircleIcon, AlertIcon, DownloadIcon, PlusIcon, CopyIcon, TrashBinIcon } from '../../icons'; import { Globe, Key, RefreshCw } from 'lucide-react'; interface WordPressIntegrationFormProps { siteId: number; integration: SiteIntegration | null; siteName?: string; siteUrl?: string; onIntegrationUpdate?: (integration: SiteIntegration) => void; } export default function WordPressIntegrationForm({ siteId, integration, siteName, siteUrl, onIntegrationUpdate, }: WordPressIntegrationFormProps) { const toast = useToast(); const [loading, setLoading] = useState(false); const [generatingKey, setGeneratingKey] = useState(false); const [apiKey, setApiKey] = useState(''); const [apiKeyVisible, setApiKeyVisible] = useState(false); // Load API key from integration on mount or when integration changes useEffect(() => { if (integration?.api_key) { setApiKey(integration.api_key); } else { setApiKey(''); } }, [integration]); const handleGenerateApiKey = async () => { try { setGeneratingKey(true); // Call the new generate-api-key endpoint const response = await fetchAPI('/v1/integration/integrations/generate-api-key/', { method: 'POST', body: JSON.stringify({ site_id: siteId }), }); const newKey = response.api_key; setApiKey(newKey); setApiKeyVisible(true); // Trigger integration update if (onIntegrationUpdate && response.integration) { onIntegrationUpdate(response.integration); } toast.success('API key generated successfully'); } catch (error: any) { toast.error(`Failed to generate API key: ${error.message}`); } finally { setGeneratingKey(false); } }; const handleRegenerateApiKey = async () => { if (!confirm('Are you sure you want to regenerate the API key? The old key will no longer work.')) { return; } try { setGeneratingKey(true); // Call the generate-api-key endpoint to create a new key const response = await fetchAPI('/v1/integration/integrations/generate-api-key/', { method: 'POST', body: JSON.stringify({ site_id: siteId }), }); const newKey = response.api_key; setApiKey(newKey); setApiKeyVisible(true); // Trigger integration update if (onIntegrationUpdate && response.integration) { onIntegrationUpdate(response.integration); } toast.success('API key regenerated successfully'); } catch (error: any) { toast.error(`Failed to regenerate API key: ${error.message}`); } finally { setGeneratingKey(false); } }; const handleRevokeApiKey = async () => { if (!confirm('Are you sure you want to revoke the API key? Your WordPress plugin will stop working until you generate a new key.')) { return; } try { setGeneratingKey(true); if (!integration) { toast.error('No integration found'); return; } // Delete the integration to revoke the API key await integrationApi.deleteIntegration(integration.id); setApiKey(''); setApiKeyVisible(false); // Trigger integration update if (onIntegrationUpdate) { onIntegrationUpdate(null as any); } toast.success('API key revoked successfully'); } catch (error: any) { toast.error(`Failed to revoke API key: ${error.message}`); } finally { setGeneratingKey(false); } }; const handleCopyApiKey = () => { if (apiKey) { navigator.clipboard.writeText(apiKey); toast.success('API key copied to clipboard'); } }; const handleDownloadPlugin = () => { const pluginUrl = `https://github.com/igny8/igny8-wp-bridge/releases/latest/download/igny8-wp-bridge.zip`; window.open(pluginUrl, '_blank'); toast.success('Plugin download started'); }; const maskApiKey = (key: string) => { if (!key) return ''; if (key.length <= 12) return key; return key.substring(0, 8) + '**********' + key.substring(key.length - 4); }; // Toggle integration sync enabled status (not creation - that happens automatically) const [integrationEnabled, setIntegrationEnabled] = useState(integration?.sync_enabled ?? false); const handleToggleIntegration = async (enabled: boolean) => { try { setIntegrationEnabled(enabled); if (integration) { // Update existing integration - only toggle sync_enabled, not creation await integrationApi.updateIntegration(integration.id, { sync_enabled: enabled, } as any); toast.success(enabled ? 'Sync enabled' : 'Sync disabled'); // Reload integration const updated = await integrationApi.getWordPressIntegration(siteId); if (onIntegrationUpdate && updated) { onIntegrationUpdate(updated); } } else { // Integration doesn't exist - it should be created automatically by plugin // when user connects from WordPress side toast.info('Integration will be created automatically when you connect from WordPress plugin. Please connect from the plugin first.'); setIntegrationEnabled(false); } } catch (error: any) { toast.error(`Failed to update integration: ${error.message}`); // Revert on error setIntegrationEnabled(!enabled); } }; useEffect(() => { if (integration) { setIntegrationEnabled(integration.sync_enabled ?? false); } }, [integration]); return (
{/* Header with Toggle */}

WordPress Integration

Connect your WordPress site using the IGNY8 WP Bridge plugin

{/* Toggle Switch */} {apiKey && (
{integrationEnabled ? 'Sync Enabled' : 'Sync Disabled'}
)}
{/* API Keys Table */}

API Keys

API keys are used to authenticate requests to the IGNY8 API

{!apiKey && (
)}
{apiKey ? (
Name Status Created Action
Regenerate
Active {new Date().toLocaleDateString('en-US', { day: 'numeric', month: 'short', year: 'numeric' })}
) : (

No API key generated yet. Click "Add API Key" to generate one.

)}
{/* Plugin Download Section */} {apiKey && (

IGNY8 WP Bridge Plugin

Download and install the plugin on your WordPress site

)}
); }