This commit is contained in:
alorig
2025-11-28 12:40:34 +05:00
parent f76e791de7
commit 636b7ddca9
6 changed files with 36 additions and 512 deletions

View File

@@ -13,7 +13,8 @@ import {
generateImages,
bulkUpdateImagesStatus,
ContentImage,
api,
fetchAPI,
publishContent,
} from '../../services/api';
import { useToast } from '../../components/ui/toast/ToastContainer';
import { FileIcon, DownloadIcon, BoltIcon, TaskIcon, ImageIcon, CheckCircleIcon } from '../../icons';
@@ -208,12 +209,10 @@ export default function Images() {
// Bulk action handler
const handleBulkAction = useCallback(async (action: string, ids: string[]) => {
if (action === 'bulk_publish_wordpress') {
// Filter to only publish items that have images generated and are not already published
// Filter to only publish items that have images generated
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')));
.filter(item => item.overall_status === 'complete');
if (readyItems.length === 0) {
toast.warning('No items are ready for WordPress publishing. Items must have generated images and not already be published.');
@@ -221,27 +220,30 @@ export default function Images() {
}
try {
const response = await api.post('/api/wordpress/bulk-publish/', {
content_ids: readyItems.map(item => item.content_id.toString())
});
let successCount = 0;
let failedCount = 0;
const errors: string[] = [];
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`);
// Process each item individually using the existing publishContent function
for (const item of readyItems) {
try {
await publishContent(item.content_id);
successCount++;
} catch (error: any) {
failedCount++;
errors.push(`${item.content_title}: ${error.message}`);
}
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}`);
}
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();
} catch (error: any) {
console.error('Bulk WordPress publish error:', error);
toast.error(`Failed to bulk publish to WordPress: ${error.message || 'Network error'}`);
@@ -258,19 +260,13 @@ export default function Images() {
setStatusUpdateRecordName(row.content_title || `Content #${row.content_id}`);
setIsStatusModalOpen(true);
} else if (action === 'publish_wordpress') {
// Handle WordPress publishing for individual item
// Handle WordPress publishing for individual item using existing publishContent function
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}`);
}
// Use the existing publishContent function from the API
const result = await publishContent(row.content_id);
toast.success(`Successfully published "${row.content_title}" to WordPress! View at: ${result.external_url}`);
// Reload images to reflect the updated WordPress status
loadImages();
} catch (error: any) {
console.error('WordPress publish error:', error);
toast.error(`Failed to publish to WordPress: ${error.message || 'Network error'}`);