migrate/resources/js/attributes.js

119 lines
4.0 KiB
JavaScript

// Attributes page JavaScript
let routes = {};
let csrfToken = '';
// Initialize on page load
document.addEventListener('DOMContentLoaded', function() {
if (window.attributeRoutes) {
routes = window.attributeRoutes;
csrfToken = document.querySelector('meta[name="csrf-token"]')?.content || '';
}
});
function migrateAttribute(attributeId, attributeCode) {
if (!confirm(`Are you sure you want to migrate the attribute "${attributeCode}" from Magento 1 to Magento 2?`)) {
return;
}
const button = document.getElementById(`migrate-btn-${attributeId}`);
const originalText = button.textContent;
button.disabled = true;
button.textContent = 'Migrating...';
const url = routes.migrateAttribute.replace(':id', attributeId);
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': csrfToken
}
})
.then(response => response.json())
.then(data => {
if (data.success) {
const row = document.getElementById(`attr-row-${attributeId}`);
if (row) {
row.style.opacity = '0.5';
row.style.background = '#d4edda';
button.textContent = 'Migrated ✓';
button.style.background = '#28a745';
button.disabled = true;
const totalSpan = document.getElementById('missingAttributesTotal');
if (totalSpan) {
const currentCount = parseInt(totalSpan.textContent);
if (currentCount > 0) {
totalSpan.textContent = currentCount - 1;
}
}
}
alert('Attribute migrated successfully!');
} else {
button.disabled = false;
button.textContent = originalText;
alert('Error: ' + (data.message || 'Failed to migrate attribute'));
}
})
.catch(error => {
button.disabled = false;
button.textContent = originalText;
alert('Error: ' + error.message);
});
}
function migrateAttributeGroup(groupId, setId, groupName) {
if (!confirm(`Are you sure you want to migrate the attribute group "${groupName}" from Magento 1 to Magento 2?\n\nThis will also migrate any attributes in this group that exist in Magento 2.`)) {
return;
}
const button = document.getElementById(`migrate-group-btn-${groupId}-${setId}`);
const originalText = button.textContent;
button.disabled = true;
button.textContent = 'Migrating...';
const url = routes.migrateAttributeGroup
.replace(':groupId', groupId)
.replace(':setId', setId);
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': csrfToken
}
})
.then(response => response.json())
.then(data => {
if (data.success) {
const row = document.getElementById(`group-row-${groupId}-${setId}`);
if (row) {
row.style.opacity = '0.5';
row.style.background = '#d4edda';
button.textContent = 'Migrated ✓';
button.style.background = '#28a745';
button.disabled = true;
}
const message = data.attributes_migrated !== undefined
? `Attribute group migrated successfully! ${data.attributes_migrated} attribute(s) were also migrated to the group.`
: 'Attribute group migrated successfully!';
alert(message);
} else {
button.disabled = false;
button.textContent = originalText;
alert('Error: ' + (data.message || 'Failed to migrate attribute group'));
}
})
.catch(error => {
button.disabled = false;
button.textContent = originalText;
alert('Error: ' + error.message);
});
}
// Make functions available globally
window.migrateAttribute = migrateAttribute;
window.migrateAttributeGroup = migrateAttributeGroup;