From 07d6243aba803c9ec1d842d7c448df067065ae9a Mon Sep 17 00:00:00 2001 From: mateuswetah Date: Wed, 6 Nov 2024 10:37:10 -0300 Subject: [PATCH] Emmits errors of parent compound metadata. --- .../components/edition/item-edition-form.vue | 114 ++++++++---------- src/views/admin/js/item-metadata-mixin.js | 24 ++-- 2 files changed, 70 insertions(+), 68 deletions(-) diff --git a/src/views/admin/components/edition/item-edition-form.vue b/src/views/admin/components/edition/item-edition-form.vue index 5847a0ed9..9bbb2bb95 100644 --- a/src/views/admin/components/edition/item-edition-form.vue +++ b/src/views/admin/components/edition/item-edition-form.vue @@ -1280,17 +1280,7 @@ export default { }) .catch((errors) => { - if (errors.errors) { - for (let error of errors.errors) { - for (let metadatum of Object.keys(error)){ - this.errors.push({ - metadatum_id: metadatum, - errors: error[metadatum] - }); - } - } - this.formErrorMessage = errors.error_message; - } + this.prepareErrors(errors); this.form.status = previousStatus; this.item.status = previousStatus; @@ -1527,16 +1517,7 @@ export default { this.isLoading = false; }) .catch((errors) => { - for (let error of errors.errors) { - for (let metadatum of Object.keys(error)){ - this.errors.push({ - metadatum_id: metadatum, - errors: error[metadatum] - }); - } - } - this.formErrorMessage = errors.error_message; - + this.prepareErrors(errors); this.isLoading = false; }); }, @@ -1610,16 +1591,7 @@ export default { } }) .catch((errors) => { - for (let error of errors.errors) { - for (let metadatum of Object.keys(error)){ - this.errors.push({ - metadatum_id: metadatum, - errors: error[metadatum] - }); - } - } - this.formErrorMessage = errors.error_message; - + this.prepareErrors(errors); this.isLoading = false; }); }, @@ -1638,15 +1610,7 @@ export default { this.shouldLoadAttachments = !this.shouldLoadAttachments; }) .catch((errors) => { - for (let error of errors.errors) { - for (let metadatum of Object.keys(error)){ - this.errors.push({ - metadatum_id: metadatum, - errors: error[metadatum] - }); - } - } - this.formErrorMessage = errors.error_message; + this.prepareErrors(errors); }); }, deleteThumbnail() { @@ -1698,29 +1662,24 @@ export default { this.form.document = file.id + ''; this.updateItemDocument({ item_id: this.itemId, document: this.form.document, document_type: this.form.document_type }) - .then((item) => { - this.isLoading = false; - this.item.document_as_html = item.document_as_html; - this.item.document_mimetype = item.document_mimetype; + .then((item) => { + this.isLoading = false; + this.item.document_as_html = item.document_as_html; + this.item.document_mimetype = item.document_mimetype; - let oldThumbnail = this.item.thumbnail; - if (item.document_type == 'attachment' && oldThumbnail != item.thumbnail ) { - this.item.thumbnail = item.thumbnail; - this.item.thumbnail_id = item.thumbnail_id; - } - - this.shouldLoadAttachments = !this.shouldLoadAttachments; - - }) - .catch((errors) => { - for (let error of errors.errors) { - for (let metadatum of Object.keys(error)){ - this.errors.push({ metadatum_id: metadatum, errors: error[metadatum]}); + let oldThumbnail = this.item.thumbnail; + if (item.document_type == 'attachment' && oldThumbnail != item.thumbnail ) { + this.item.thumbnail = item.thumbnail; + this.item.thumbnail_id = item.thumbnail_id; } - } - this.formErrorMessage = errors.error_message; - this.isLoading = false; - }); + + this.shouldLoadAttachments = !this.shouldLoadAttachments; + + }) + .catch((errors) => { + this.prepareErrors(errors); + this.isLoading = false; + }); } } ); @@ -2024,6 +1983,39 @@ export default { } return enabledMetadata.indexOf(metadatum.id); + }, + prepareErrors(errors) { + if ( errors.errors ) { + for (let error of errors.errors) { + for (let metadatumId of Object.keys(error)) { + + let parentCompoundId = false; + + for (let itemMetadatum of this.itemMetadata) { + + if ( + itemMetadatum.metadatum.metadata_type == 'Tainacan\\Metadata_Types\\Compound' && + itemMetadatum.metadatum.metadata_type_options && + itemMetadatum.metadatum.metadata_type_options.children_objects + ) { + + for (let childMetadatum of itemMetadatum.metadatum.metadata_type_options.children_objects) { + if ( childMetadatum.id == metadatumId ) { + parentCompoundId = itemMetadatum.metadatum.id; + break; + } + } + } + } + + this.errors.push({ + metadatum_id: parentCompoundId ? parentCompoundId : metadatumId, + errors: error[metadatumId] + }); + } + } + this.formErrorMessage = errors.error_message; + } } } } diff --git a/src/views/admin/js/item-metadata-mixin.js b/src/views/admin/js/item-metadata-mixin.js index 7e2fd20b8..7930b4652 100644 --- a/src/views/admin/js/item-metadata-mixin.js +++ b/src/views/admin/js/item-metadata-mixin.js @@ -20,9 +20,10 @@ export const itemMetadataMixin = { handler() { this.hasErrorsOnForm( this.errors.length > 0 && this.errors[0].errors && this.errors[0].errors.length ); - if (this.errors.length > 0 && this.errors[0].errors && this.errors[0].errors.length) { - for (let error of this.errors) + if ( this.errors.length > 0 && this.errors[0].errors && this.errors[0].errors.length ) { + for (let error of this.errors) { this.$emitter.emit('updateErrorMessageOf#' + (error.metadatum_id + (error.parent_meta_id ? '-' + error.parent_meta_id : '')), error); + } } }, deep: true @@ -36,8 +37,8 @@ export const itemMetadataMixin = { 'deleteGroupFromItemSubmissionMetadatum' ]), updateItemMetadataValue({ itemId, metadatumId, values, parentMetaId, parentId }) { - - if (itemId) { + + if ( itemId ) { this.isUpdatingValues = true; @@ -54,15 +55,18 @@ export const itemMetadataMixin = { }) .then(() => { this.isUpdatingValues = false; - let index = this.errors.findIndex( errorItem => errorItem.metadatum_id == metadatumId && (parentMetaId ? errorItem.parent_meta_id == parentMetaId : true )); - if (index >= 0) + let index = this.errors.findIndex( errorItem => ( errorItem.metadatum_id == metadatumId && (parentMetaId ? errorItem.parent_meta_id == parentMetaId : true ) ) || ( errorItem.metadatum_id == parentId ) ); + if ( index >= 0 ) this.errors.splice( index, 1); this.$emitter.emit('updateErrorMessageOf#' + (parentMetaId ? metadatumId + '-' + parentMetaId : metadatumId), this.errors[index]); + + if ( parentId ) + this.$emitter.emit('updateErrorMessageOf#' + parentId ); }) .catch(({ error_message, error, item_metadata }) => { this.isUpdatingValues = false; - let index = this.errors.findIndex( errorItem => errorItem.metadatum_id == metadatumId && (parentMetaId ? errorItem.parent_meta_id == parentMetaId : true )); + let index = this.errors.findIndex( errorItem => ( errorItem.metadatum_id == metadatumId && (parentMetaId ? errorItem.parent_meta_id == parentMetaId : true ) ) || ( errorItem.metadatum_id == parentId ) ); let messages = []; for (let index in error) @@ -71,9 +75,15 @@ export const itemMetadataMixin = { if ( index >= 0) { Object.assign( this.errors, { [index]: { metadatum_id: metadatumId, parent_meta_id: parentMetaId, errors: messages } }); this.$emitter.emit('updateErrorMessageOf#' + (parentMetaId ? metadatumId + '-' + parentMetaId : metadatumId), this.errors[index]); + + if ( parentId ) + this.$emitter.emit('updateErrorMessageOf#' + parentId ); } else { this.errors.push( { metadatum_id: metadatumId, parent_meta_id: parentMetaId, errors: messages } ); this.$emitter.emit('updateErrorMessageOf#' + (parentMetaId ? metadatumId + '-' + parentMetaId : metadatumId), this.errors[0]); + + if ( parentId ) + this.$emitter.emit('updateErrorMessageOf#' + parentId ); } });