From 4cb00cf510a8d27db3e4b796ecad252ad5dda497 Mon Sep 17 00:00:00 2001 From: mateuswetah Date: Thu, 15 Feb 2018 11:28:44 -0200 Subject: [PATCH] Removes OldValues logic for receiving new multiple metadata update routine. --- ...tainacan-rest-item-metadata-controller.php | 2 +- .../field-types/tainacan-form-item.vue | 30 +++++++------------ .../class-tainacan-item-metadata.php | 2 ++ src/js/event-bus-web-components.js | 19 ++++-------- src/js/store/modules/item/actions.js | 30 ++----------------- 5 files changed, 21 insertions(+), 62 deletions(-) diff --git a/src/api/endpoints/class-tainacan-rest-item-metadata-controller.php b/src/api/endpoints/class-tainacan-rest-item-metadata-controller.php index 16a67ccce..c39233949 100644 --- a/src/api/endpoints/class-tainacan-rest-item-metadata-controller.php +++ b/src/api/endpoints/class-tainacan-rest-item-metadata-controller.php @@ -206,7 +206,7 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_REST_Controller { } return new WP_REST_Response([ - 'error_message' => __('The body could not be empty', 'tainacan'), + 'error_message' => 'The body could not be empty', 'body' => $body ], 400); } diff --git a/src/classes/field-types/tainacan-form-item.vue b/src/classes/field-types/tainacan-form-item.vue index f932c9053..eeb574747 100644 --- a/src/classes/field-types/tainacan-form-item.vue +++ b/src/classes/field-types/tainacan-form-item.vue @@ -3,10 +3,10 @@ :message="getErrorMessage" :type="fieldTypeMessage">
- +
- - + -
+
@@ -25,7 +25,6 @@ data(){ return { inputs: [], - oldInputs: [], fieldTypeMessage:'' } }, @@ -52,28 +51,19 @@ }, methods: { changeValue(){ - //console.log(this.inputs); - //console.log(this.oldInputs); - eventBus.$emit('input', { item_id: this.field.item.id, field_id: this.field.field.id, values: this.inputs, old_values: this.oldInputs } ); + eventBus.$emit('input', { item_id: this.field.item.id, field_id: this.field.field.id, values: this.inputs } ); }, - getValue(){ - + getValue(){ if (this.field.value instanceof Array) { this.inputs = this.field.value; - if (this.inputs.length == 0) - this.inputs.push(''); + if (this.inputs.length == 0) + this.inputs.push(''); } else { - this.field.value == null || this.field.value == undefined ? this.inputs.push('') : this.inputs.push(this.field.value); + if (this.field.value == null || this.field.value == undefined) + this.inputs.push(''); + else + this.inputs.push(this.field.value); } - // Can't use this.oldInputs = this.inputs, would still affect oldInputs after inputs change - this.oldInputs = this.inputs; - // for (let input of this.inputs) { - // this.oldInputs.push(input); - // } - }, - extractFieldType(field_type) { - let parts = field_type.split('\\'); - return 'tainacan-' + parts.pop().toLowerCase(); }, addInput(){ this.inputs.push(''); diff --git a/src/classes/repositories/class-tainacan-item-metadata.php b/src/classes/repositories/class-tainacan-item-metadata.php index 06d65028b..2ac2e3994 100644 --- a/src/classes/repositories/class-tainacan-item-metadata.php +++ b/src/classes/repositories/class-tainacan-item-metadata.php @@ -67,7 +67,9 @@ class Item_Metadata extends Repository { do_action('tainacan-update', $item_metadata); do_action('tainacan-update-Item_Metadata_Entity', $item_metadata); + $value = get_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id()); $item_metadata = new Entities\Item_Metadata_Entity($item_metadata->get_item(), $item_metadata->get_field()); + $item_metadata->set_value($value); return $item_metadata; } diff --git a/src/js/event-bus-web-components.js b/src/js/event-bus-web-components.js index 65776b482..644fe00b6 100644 --- a/src/js/event-bus-web-components.js +++ b/src/js/event-bus-web-components.js @@ -24,18 +24,11 @@ export const eventBus = new Vue({ for (let eventElement of components){ eventElement.addEventListener('input', (event) => { if (event.detail && event.detail[0] ){ - const promisse = this.$store.dispatch('item/updateMetadata', - { item_id: $(eventElement).attr("item_id"), field_id: $(eventElement).attr("field_id"), values: event.detail, old_values: $(eventElement).attr("old_values") }); - - promisse.then( response => { - // eventElement.errorsMsg = JSON.stringify( [] ); - // eventElement.value = response.value; - $(eventElement).val(response.value); - }, error => { - const field = this.errors.find(error => error.field_id === event.detail[0].field_id ); - // eventElement.errorsMsg = JSON.stringify( field.error ); - // eventElement.value = event.detail[0].values; - }); + this.updateValue({ + item_id: $(eventElement).attr("item_id"), + field_id: $(eventElement).attr("field_id"), + values: event.detail + }) } }); } @@ -43,7 +36,7 @@ export const eventBus = new Vue({ updateValue(data){ if ( data.item_id ){ const promisse = this.$store.dispatch('item/updateMetadata', - { item_id: data.item_id, field_id: data.field_id, values: data.values, old_values: data.old_values }); + { item_id: data.item_id, field_id: data.field_id, values: data.values }); promisse.then( response => { let index = this.errors.findIndex( errorItem => errorItem.field_id === data.field_id ); if ( index >= 0){ diff --git a/src/js/store/modules/item/actions.js b/src/js/store/modules/item/actions.js index d1c1fb252..6a5466d23 100644 --- a/src/js/store/modules/item/actions.js +++ b/src/js/store/modules/item/actions.js @@ -17,36 +17,10 @@ export const sendField = ( { commit }, { item_id, field_id, values }) => { }; -export const updateMetadata = ({ commit }, { item_id, field_id, values, old_values }) => { +export const updateMetadata = ({ commit }, { item_id, field_id, values }) => { return new Promise((resolve, reject) => { - - let metadata_values = []; - - // values = ["a", "b", "c"] - // old_values = ["a", "b"] - if (values.lenght >= old_values.length) { - // New values in metadata array - for (let i = 0; i < values.length; i++) { - if (old_values[i]) - metadata_values.push({"new": values[i], "prev": old_values[i]}); - else - metadata_values.push({"new": values[i], "prev": null }); - } - } - // values = ["a", "c"] - // old_values = ["a", "b", "c"] - else { - // Values removed from metadata array - for (let i = 0; i < old_values.length; i++) { - if (values[i]) - metadata_values.push({"new": values[i], "prev": old_values[i]}); - else - metadata_values.push({"new": null, "prev": old_values[i]}); - } - } - //console.log(metadata_values); axios.patch(`/item/${item_id}/metadata/${field_id}`, { - values: metadata_values, + values: values, }) .then( res => { let field = res.data;