publish to wp

This commit is contained in:
alorig
2025-11-28 12:35:02 +05:00
parent 081f94ffdb
commit f76e791de7
8 changed files with 664 additions and 51 deletions

View File

@@ -10,8 +10,6 @@ import {
Content as ContentType,
ContentFilters,
generateImagePrompts,
publishContent,
unpublishContent,
} from '../../services/api';
import { optimizerApi } from '../../api/optimizer.api';
import { useNavigate } from 'react-router';
@@ -164,40 +162,12 @@ export default function Content() {
const navigate = useNavigate();
const handleRowAction = useCallback(async (action: string, row: ContentType) => {
if (action === 'publish') {
try {
// Check if already published
if (row.external_id) {
toast.warning('Content is already published to WordPress');
return;
}
const result = await publishContent(row.id);
toast.success(`Content published successfully! View at: ${result.external_url}`);
loadContent(); // Reload to show updated external_id
} catch (error: any) {
toast.error(`Failed to publish content: ${error.message}`);
}
} else if (action === 'view_on_wordpress') {
if (action === 'view_on_wordpress') {
if (row.external_url) {
window.open(row.external_url, '_blank');
} else {
toast.warning('WordPress URL not available');
}
} else if (action === 'unpublish') {
try {
// Check if not published
if (!row.external_id) {
toast.warning('Content is not currently published');
return;
}
await unpublishContent(row.id);
toast.success('Content unpublished successfully');
loadContent(); // Reload to show cleared external_id
} catch (error: any) {
toast.error(`Failed to unpublish content: ${error.message}`);
}
} else if (action === 'generate_image_prompts') {
try {
const result = await generateImagePrompts([row.id]);

View File

@@ -13,6 +13,7 @@ import {
generateImages,
bulkUpdateImagesStatus,
ContentImage,
api,
} from '../../services/api';
import { useToast } from '../../components/ui/toast/ToastContainer';
import { FileIcon, DownloadIcon, BoltIcon, TaskIcon, ImageIcon, CheckCircleIcon } from '../../icons';
@@ -206,8 +207,49 @@ export default function Images() {
// Bulk action handler
const handleBulkAction = useCallback(async (action: string, ids: string[]) => {
toast.info(`Bulk action "${action}" for ${ids.length} items`);
}, [toast]);
if (action === 'bulk_publish_wordpress') {
// Filter to only publish items that have images generated and are not already published
const readyItems = images
.filter(item => ids.includes(item.content_id.toString()))
.filter(item => item.status === 'complete' &&
(!item.wordpress_status ||
(item.wordpress_status !== 'published' && item.wordpress_status !== 'publishing')));
if (readyItems.length === 0) {
toast.warning('No items are ready for WordPress publishing. Items must have generated images and not already be published.');
return;
}
try {
const response = await api.post('/api/wordpress/bulk-publish/', {
content_ids: readyItems.map(item => item.content_id.toString())
});
if (response.data.success) {
const results = response.data.data.results;
const successCount = results.filter((r: any) => r.success).length;
const failedCount = results.filter((r: any) => !r.success).length;
if (successCount > 0) {
toast.success(`Successfully published ${successCount} item(s) to WordPress`);
}
if (failedCount > 0) {
toast.warning(`${failedCount} item(s) failed to publish`);
}
// Reload images to reflect the updated WordPress status
loadImages();
} else {
toast.error(`Bulk publish failed: ${response.data.message}`);
}
} catch (error: any) {
console.error('Bulk WordPress publish error:', error);
toast.error(`Failed to bulk publish to WordPress: ${error.message || 'Network error'}`);
}
} else {
toast.info(`Bulk action "${action}" for ${ids.length} items`);
}
}, [images, toast, loadImages]);
// Row action handler
const handleRowAction = useCallback(async (action: string, row: ContentImagesGroup) => {
@@ -215,8 +257,26 @@ export default function Images() {
setStatusUpdateContentId(row.content_id);
setStatusUpdateRecordName(row.content_title || `Content #${row.content_id}`);
setIsStatusModalOpen(true);
} else if (action === 'publish_wordpress') {
// Handle WordPress publishing for individual item
try {
const response = await api.post('/api/wordpress/publish/', {
content_id: row.content_id.toString()
});
if (response.data.success) {
toast.success(`Successfully published "${row.content_title}" to WordPress`);
// Reload images to reflect the updated WordPress status
loadImages();
} else {
toast.error(`Failed to publish: ${response.data.message}`);
}
} catch (error: any) {
console.error('WordPress publish error:', error);
toast.error(`Failed to publish to WordPress: ${error.message || 'Network error'}`);
}
}
}, []);
}, [loadImages, toast]);
// Handle status update confirmation
const handleStatusUpdate = useCallback(async (status: string) => {