| | |
| | | this.queue.subscribe((event, operation) => { |
| | | if ((event === 'operation-status' || event === 'cancel-operation') |
| | | && ['image_upload', 'video_upload', 'document_upload'].includes(operation.type)) { |
| | | const data = operation.data instanceof FormData |
| | | ? this.stores.uploads.formDataToObject(operation.data) |
| | | : operation.data; |
| | | let uploadIds = []; |
| | | |
| | | let uploads = data['upload_ids']; |
| | | if (!uploads || uploads.length === 0) return; |
| | | if (event === 'cancel-operation') return this.handleOperationCancelled(uploads); |
| | | this.setBulkUpload(uploads, 'status', operation.status).then(()=>{}); |
| | | if (operation.data) { |
| | | // Handle FormData |
| | | if (operation.data instanceof FormData) { |
| | | const dataObj = this.stores.uploads.formDataToObject(operation.data); |
| | | uploadIds = dataObj['upload_ids'] || []; |
| | | } |
| | | // Handle regular object |
| | | else { |
| | | uploadIds = operation.data['upload_ids'] || []; |
| | | } |
| | | } |
| | | |
| | | // If not in data, check result (for completed operations from backend) |
| | | if (uploadIds.length === 0 && operation.result && operation.result.upload_ids) { |
| | | uploadIds = operation.result.upload_ids; |
| | | } |
| | | |
| | | // Still no upload_ids? Log warning and bail |
| | | if (!uploadIds || uploadIds.length === 0) { |
| | | console.warn('[UploadManager] No upload_ids found for operation:', { |
| | | id: operation.id, |
| | | type: operation.type, |
| | | status: operation.status, |
| | | hasData: !!operation.data, |
| | | hasResult: !!operation.result |
| | | }); |
| | | return; |
| | | } |
| | | |
| | | // Handle cancellation |
| | | if (event === 'cancel-operation') { |
| | | return this.handleOperationCancelled(uploadIds); |
| | | } |
| | | |
| | | // Update upload status based on operation status |
| | | this.setBulkUpload(uploadIds, 'status', operation.status).then(() => { |
| | | // Log for debugging |
| | | console.log(`[UploadManager] Updated ${uploadIds.length} uploads to status: ${operation.status}`); |
| | | }); |
| | | |
| | | // Handle completion |
| | | if (operation.status === 'completed') { |
| | | uploads.forEach(upload => { |
| | | this.removeUpload(upload).then(()=>{}); |
| | | // For group uploads, mark as processed but keep for reference |
| | | if (operation.type === 'process_upload_groups') { |
| | | uploadIds.forEach(uploadId => { |
| | | this.setBulkUpload([uploadId], 'serverProcessed', true).then(() => {}); |
| | | }); |
| | | |
| | | // Log created posts if available |
| | | if (operation.result && operation.result.created_posts) { |
| | | console.log('[UploadManager] Created posts:', operation.result.created_posts); |
| | | } |
| | | |
| | | // Remove uploads after a delay to allow UI to update |
| | | setTimeout(() => { |
| | | uploadIds.forEach(uploadId => { |
| | | this.removeUpload(uploadId).then(() => {}); |
| | | }); |
| | | }, 2000); |
| | | } |
| | | // For direct uploads, remove immediately |
| | | else { |
| | | uploadIds.forEach(uploadId => { |
| | | this.removeUpload(uploadId).then(() => {}); |
| | | }); |
| | | } |
| | | } |
| | | |
| | | // Handle failures |
| | | if (operation.status === 'failed' || operation.status === 'failed_permanent') { |
| | | console.error('[UploadManager] Operation failed:', { |
| | | id: operation.id, |
| | | type: operation.type, |
| | | uploadIds: uploadIds, |
| | | error: operation.error_message |
| | | }); |
| | | } |
| | | } |
| | |
| | | fields: { |
| | | field: '[data-upload-field]', |
| | | input: 'input[type="file"]', |
| | | dropZone: '.file-upload-container', |
| | | dropZone: '.file-upload-wrapper', |
| | | preview: '.preview-wrap', |
| | | grid: '.item-grid.preview', |
| | | progress: { |
| | |
| | | if (details) { |
| | | details.open = false; |
| | | } |
| | | |
| | | |
| | | this.notify('groups_uploaded', { |
| | | fieldId: fieldId, |
| | | posts: posts, |
| | | content: field.config.content, |
| | | }); |
| | | } |
| | | if (operationId) { |
| | | field.operationId = operationId; |
| | |
| | | canMerge: mergable, |
| | | sendNow: endpoint === 'uploads/groups', |
| | | headers: { |
| | | 'action_nonce': window.auth.getNonce('dash') |
| | | 'X-Action-Nonce': window.auth.getNonce('dash') |
| | | }, |
| | | append: '_upload' |
| | | } |
| | |
| | | const fields = this.collectGroupFieldsFromDOM(groupElement, group.id); |
| | | |
| | | const post = { |
| | | groupId: group.id, |
| | | images: [], |
| | | fields: fields |
| | | }; |
| | |
| | | const remaining = uploads.filter(u => !u.group); |
| | | for (const upload of remaining) { |
| | | const post = { |
| | | groupId: window.generateID('group'), |
| | | images: [], |
| | | fields: {} |
| | | }; |