diff --git a/components/dms/dialogs/DMSUploadDialog.vue b/components/dms/dialogs/DMSUploadDialog.vue index 267762c..cb76d99 100644 --- a/components/dms/dialogs/DMSUploadDialog.vue +++ b/components/dms/dialogs/DMSUploadDialog.vue @@ -13,7 +13,7 @@ const props = defineProps({ } }); -const emit = defineEmits(['close', 'upload']); +const emit = defineEmits(['close', 'upload', 'update:visible']); // Store const dmsStore = useDmsStore(); @@ -45,14 +45,15 @@ const customFields = computed(() => { // Get tag suggestions const tagSuggestions = ref([]); +// Add a ref for the file input +const fileInput = ref(null); + // Methods const openFileDialog = () => { - const input = document.createElement('input'); - input.type = 'file'; - input.multiple = true; - input.accept = dmsStore.systemSettings.upload.allowedFileTypes.map(ext => `.${ext}`).join(','); - input.onchange = (e) => handleFiles(Array.from(e.target.files)); - input.click(); + // Use the ref instead of creating a new input element + if (fileInput.value) { + fileInput.value.click(); + } }; const handleFiles = (files) => { @@ -69,7 +70,8 @@ const handleFiles = (files) => { fileMetadata.value[file.name] = { ...template, title: file.name.split('.')[0], - author: 'Current User' // Get from auth store + author: dmsStore.currentUser.name || 'Current User', + tags: [] // Ensure tags is always initialized as an array }; } else { errors[file.name] = validation.errors; @@ -116,6 +118,11 @@ const uploadFiles = async () => { const file = selectedFiles.value[i]; const metadata = fileMetadata.value[file.name]; + // Ensure all required fields are present + if (!metadata.tags) metadata.tags = []; + if (!metadata.author) metadata.author = dmsStore.currentUser.name || 'Current User'; + if (!metadata.department) metadata.department = dmsStore.currentUser.department || ''; + // Upload with enhanced metadata await dmsStore.uploadFileWithMetadata(file, metadata, props.currentPath); @@ -126,7 +133,7 @@ const uploadFiles = async () => { closeDialog(); } catch (error) { console.error('Upload failed:', error); - // Show error to user + alert(`Upload failed: ${error.message || 'Unknown error'}`); } finally { isUploading.value = false; } @@ -137,6 +144,7 @@ const closeDialog = () => { fileMetadata.value = {}; validationErrors.value = {}; uploadProgress.value = 0; + emit('update:visible', false); emit('close'); }; @@ -178,14 +186,25 @@ const handleTagInput = (fileName, input) => { }; const addTag = (fileName, tag) => { - if (!fileMetadata.value[fileName].tags.includes(tag)) { - fileMetadata.value[fileName].tags.push(tag); + if (!tag || tag.trim() === '') return; + + // Initialize tags array if it doesn't exist + if (!fileMetadata.value[fileName].tags) { + fileMetadata.value[fileName].tags = []; } + + // Only add if tag isn't empty and not already in the array + if (!fileMetadata.value[fileName].tags.includes(tag.trim())) { + fileMetadata.value[fileName].tags.push(tag.trim()); + } + tagSuggestions.value = []; }; const removeTag = (fileName, tagIndex) => { - fileMetadata.value[fileName].tags.splice(tagIndex, 1); + if (fileMetadata.value[fileName]?.tags) { + fileMetadata.value[fileName].tags.splice(tagIndex, 1); + } }; // Watch for template changes @@ -222,10 +241,35 @@ const getFieldComponent = (fieldType) => { return 'text'; } }; + +onMounted(() => { + // Ensure fileMetadata is initialized correctly for each selected file + selectedFiles.value.forEach(file => { + if (!fileMetadata.value[file.name]) { + const template = dmsStore.metadataTemplates[metadataTemplate.value]; + fileMetadata.value[file.name] = { + ...template, + title: file.name.split('.')[0], + tags: [], + author: dmsStore.currentUser.name || 'Current User' + }; + } + }); +});