This commit is contained in:
alorig
2025-11-22 21:10:05 +05:00
parent d4990fb088
commit 9ee03f4f7f
3 changed files with 47 additions and 45 deletions

View File

@@ -178,46 +178,29 @@ export default function WordPressIntegrationForm({
return key.substring(0, 8) + '**********' + key.substring(key.length - 4);
};
// Toggle integration enabled status
const [integrationEnabled, setIntegrationEnabled] = useState(integration?.is_active ?? true);
// 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
// Update existing integration - only toggle sync_enabled, not creation
await integrationApi.updateIntegration(integration.id, {
is_active: enabled,
sync_enabled: enabled,
} as any);
toast.success(enabled ? 'Integration enabled' : 'Integration disabled');
toast.success(enabled ? 'Sync enabled' : 'Sync disabled');
// Reload integration
const updated = await integrationApi.getWordPressIntegration(siteId);
if (onIntegrationUpdate && updated) {
onIntegrationUpdate(updated);
}
} else if (enabled && apiKey) {
// Create integration when enabling for first time
// Use API key-only authentication (no username/password required)
await integrationApi.saveWordPressIntegration(siteId, {
url: siteUrl || '',
username: '', // Optional when using API key
app_password: '', // Optional when using API key
api_key: apiKey,
is_active: enabled,
sync_enabled: true,
});
toast.success('Integration created and enabled');
// Reload integration
const updated = await integrationApi.getWordPressIntegration(siteId);
if (onIntegrationUpdate && updated) {
onIntegrationUpdate(updated);
}
} else if (enabled && !apiKey) {
// Toggle enabled but no API key - show error
toast.error('API key is required to enable WordPress integration');
} 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) {
@@ -229,7 +212,7 @@ export default function WordPressIntegrationForm({
useEffect(() => {
if (integration) {
setIntegrationEnabled(integration.is_active ?? true);
setIntegrationEnabled(integration.sync_enabled ?? false);
}
}, [integration]);
@@ -255,7 +238,7 @@ export default function WordPressIntegrationForm({
{apiKey && (
<div className="flex items-center gap-3">
<span className="text-sm font-medium text-gray-700 dark:text-gray-300">
{integrationEnabled ? 'Enabled' : 'Disabled'}
{integrationEnabled ? 'Sync Enabled' : 'Sync Disabled'}
</span>
<button
type="button"