Adds oldValues attribute to metadata updtate on Actions and Taincacan Form Item.

This commit is contained in:
mateuswetah 2018-02-15 09:28:14 -02:00
parent 195133a02c
commit dd58b7a2df
5 changed files with 46 additions and 8 deletions

View File

@ -20,6 +20,11 @@
</div>
</div>
<footer class="card-footer">
<router-link
class="card-footer-item"
:to="{ path: `/collections/${collection.id}/edit` }">
Editar Coleção
</router-link>
<router-link
class="card-footer-item"
:to="{ path: `/collections/${collection.id}/items/new`, params: { collection_id: collection.id }}">

View File

@ -190,7 +190,7 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_REST_Controller {
$field = $this->field_repository->fetch( $field_id );
$item_metadata = new Entities\Item_Metadata_Entity( $item, $field );
$item_metadata->set_value( ( is_array($value) ) ? array_filter($value) : $value );
$item_metadata->set_value( $value );
if ( $item_metadata->validate() ) {
$field_updated = $this->item_metadata_repository->update( $item_metadata );

View File

@ -25,6 +25,7 @@
data(){
return {
inputs: [],
oldInputs: [],
fieldTypeMessage:''
}
},
@ -48,13 +49,15 @@
},
created(){
this.getValue();
},
methods: {
changeValue(){
eventBus.$emit('input', { item_id: this.field.item.id, field_id: this.field.field.id, values: this.inputs, instance: this } );
//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 } );
},
getValue(){
getValue(){
if (this.field.value instanceof Array) {
this.inputs = this.field.value;
if (this.inputs.length == 0)
@ -62,6 +65,11 @@
} else {
this.field.value == null || this.field.value == undefined ? this.inputs.push('') : 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('\\');

View File

@ -25,7 +25,7 @@ export const eventBus = new Vue({
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 });
{ 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( [] );
@ -43,7 +43,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 });
{ item_id: data.item_id, field_id: data.field_id, values: data.values, old_values: data.old_values });
promisse.then( response => {
let index = this.errors.findIndex( errorItem => errorItem.field_id === data.field_id );
if ( index >= 0){

View File

@ -17,11 +17,36 @@ export const sendField = ( { commit }, { item_id, field_id, values }) => {
};
export const updateMetadata = ({ commit }, { item_id, field_id, values }) => {
export const updateMetadata = ({ commit }, { item_id, field_id, values, old_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: values
values: metadata_values,
})
.then( res => {
let field = res.data;