update store structure

This commit is contained in:
Eduardo humberto 2017-12-12 16:08:49 -02:00
parent ef7a08f9ab
commit 2ee73b7fe9
12 changed files with 163 additions and 62 deletions

View File

@ -7,7 +7,7 @@
type="checkbox"
:id="option.replace(' ','-') + '-checkbox'"
:value="option"
:checked="isChecked"
:checked="isChecked(option)"
@change="sendValue($event)"> {{ option }} <br>
</label>
</div>

View File

@ -0,0 +1,77 @@
<template>
<div class="component">
<p>{{ name }}</p>
<select
class="form-control"
:disabled="!getOptions"
v-model="manageValue">
<option
v-for="option in getOptions"
:selected="option == ''">
{{ option }}
</option>
</select>
</div>
</template>
<script>
import store from '../../../js/store/store';
export default {
store,
data(){
return {
selected:''
}
},
props: {
name: {
type: String
},
options: {
type: String
},
item_id: {
type: Number
},
metadata_id: {
type: Number
},
value: {
type: [ String,Number ]
},
},
created(){
this.setInitValueOnStore();
},
computed: {
getOptions(){
const values = ( this.options ) ? this.options.split("\n") : false;
return values;
},
manageValue : {
get(){
let metadata = this.$store.getters['item/getMetadata'].find(metadata => metadata.metadata_id === this.metadata_id );
if( metadata ){
return metadata.values;
}else if( this.value ){
return JSON.parse( this.value );
}
},
set( value ){
this.$store.dispatch('item/sendMetadata', { item_id: this.item_id, metadata_id: this.metadata_id, values: value });
}
}
},
methods: {
setInitValueOnStore(){
if ( this.value ){
this.$store.dispatch('item/setSingleMetadata', { item_id: this.item_id, metadata_id: this.metadata_id, values: JSON.parse( this.value ) });
}
}
}
}
</script>
<style scoped>
</style>

View File

@ -0,0 +1,4 @@
export const getItems = () => {
}

View File

@ -0,0 +1,16 @@
import * as actions from './actions';
import * as getters from './getters';
import * as mutations from './mutations';
const state = {
items: []
};
export default {
namespaced: true,
state,
mutations,
actions,
getters
}

View File

@ -1,59 +0,0 @@
import axios from '../../axios/axios';
const state = {
item: null,
metadata:[],
isInit:false,
};
const mutations = {
setItem( state, item){
state.item = item;
},
setMetadata( state, metadata){
state.metadata = metadata;
},
setSingleMetadata( state, metadata){
let index = state.metadata.findIndex(itemMetadata => itemMetadata.metadata_id === metadata.metadata_id);
if ( index >= 0){
state.metadata[index] = metadata;
}else{
state.metadata.push( metadata );
}
},
setInit( state, init){
state.isInit = init;
},
};
const actions = {
sendMetadata: ( { commit }, { item_id, metadata_id, values }) => {
axios.post('/metadata/item/'+item_id, {
metadata_id: metadata_id,
values: values
})
.then( res => {
console.log( res );
commit('setSingleMetadata', { item_id: item_id, metadata_id: metadata_id, values: values });
})
.catch(error => console.log( error ));
},
setSingleMetadata: ({ commit }, { item_id, metadata_id, values }) => {
console.log(item_id, metadata_id, values)
commit('setSingleMetadata', { item_id: item_id, metadata_id: metadata_id, values: values });
}
};
const getters = {
getMetadata: state => {
return state.metadata;
}
};
export default {
namespaced: true,
state,
mutations,
actions,
getters
}

View File

@ -0,0 +1,18 @@
import axios from '../../../axios/axios';
export const sendMetadata = ( { commit }, { item_id, metadata_id, values }) => {
axios.post('/metadata/item/'+item_id, {
metadata_id: metadata_id,
values: values
})
.then( res => {
console.log( res );
commit('setSingleMetadata', { item_id: item_id, metadata_id: metadata_id, values: values });
})
.catch(error => console.log( error ));
}
export const setSingleMetadata = ({ commit }, { item_id, metadata_id, values }) => {
commit('setSingleMetadata', { item_id: item_id, metadata_id: metadata_id, values: values });
};

View File

@ -0,0 +1,4 @@
export const getMetadata = state => {
return state.metadata;
}

View File

@ -0,0 +1,18 @@
import * as actions from './actions';
import * as getters from './getters';
import * as mutations from './mutations';
const state = {
item: null,
metadata:[],
isInit:false,
};
export default {
namespaced: true,
state,
mutations,
actions,
getters
}

View File

@ -0,0 +1,21 @@
export const setItem = ( state, item ) => {
state.item = item;
}
export const setMetadata = ( state, metadata) => {
state.metadata = metadata;
}
export const setSingleMetadata = ( state, metadata) => {
let index = state.metadata.findIndex(itemMetadata => itemMetadata.metadata_id === metadata.metadata_id);
if ( index >= 0){
state.metadata[index] = metadata;
}else{
state.metadata.push( metadata );
}
}
export const setInit = ( state, init) => {
state.isInit = init;
}

View File

@ -1,12 +1,14 @@
import Vue from 'vue';
import Vuex from 'vuex';
import item from './modules/item';
import item from './modules/item/item';
import collection from './modules/collection/collection';
Vue.use(Vuex);
export default new Vuex.Store({
modules: {
item
item,
collection
}
})