class CustomUpload extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
`;
}
connectedCallback() {
this.shadowRoot.querySelector('form').addEventListener('submit', this.handleSubmit.bind(this));
const fileInput = this.shadowRoot.getElementById('fileInput');
const dropZone = this.shadowRoot.getElementById('dropZone');
fileInput.addEventListener('change', () => this.updateFileDisplay());
// Drag and drop handlers
dropZone.addEventListener('dragover', (e) => {
e.preventDefault();
dropZone.classList.add('drag-over');
});
['dragleave', 'dragend'].forEach(type => {
dropZone.addEventListener(type, () => {
dropZone.classList.remove('drag-over');
});
});
dropZone.addEventListener('drop', (e) => {
e.preventDefault();
dropZone.classList.remove('drag-over');
if (e.dataTransfer.files.length) {
fileInput.files = e.dataTransfer.files;
this.updateFileDisplay();
}
});
dropZone.addEventListener('click', () => fileInput.click());
// Initialize Feather icons
feather.replace({ width: 20, height: 20 });
}
updateFileDisplay() {
const fileInput = this.shadowRoot.getElementById('fileInput');
const dropZone = this.shadowRoot.getElementById('dropZone');
if (fileInput.files.length > 0) {
const file = fileInput.files[0];
dropZone.innerHTML = `
${file.name}
${(file.size / 1024).toFixed(2)} KB
`;
feather.replace({ width: 20, height: 20 });
}
}
async handleSubmit(e) {
e.preventDefault();
const batchType = this.shadowRoot.getElementById('batchType').value;
const fileInput = this.shadowRoot.getElementById('fileInput');
const statusMessage = this.shadowRoot.getElementById('statusMessage');
if (!fileInput.files.length) {
statusMessage.textContent = 'Please select a file first';
statusMessage.className = 'bg-red-50 text-red-700';
statusMessage.classList.remove('hidden');
return;
}
const file = fileInput.files[0];
if (!file.name.endsWith('.csv')) {
statusMessage.textContent = 'Only CSV files are allowed';
statusMessage.className = 'bg-red-50 text-red-700';
statusMessage.classList.remove('hidden');
return;
}
statusMessage.textContent = 'Processing your file...';
statusMessage.className = 'bg-blue-50 text-blue-700';
statusMessage.classList.remove('hidden');
try {
const batch = await window.batchService.createBatch(batchType, file);
statusMessage.textContent = `Batch #${batch.id} created successfully!`;
statusMessage.className = 'bg-green-50 text-green-700';
// Dispatch event to notify list component
document.dispatchEvent(new CustomEvent('batch-created'));
// Reset form
fileInput.value = '';
this.updateFileDisplay();
} catch (error) {
statusMessage.textContent = 'Error creating batch: ' + error.message;
statusMessage.className = 'bg-red-50 text-red-700';
}
}
}
customElements.define('custom-upload', CustomUpload);