import _ from 'lodash'; import axios from "@/axios"; const state = { loading: false, searchQuery: undefined, page: 1, count:0, rows: [] }; const mutations = { ADD_ITEM(state, data) { state.rows.push(data); }, INJECT(state , {rows , keys}){ state.rows = rows || []; state.keys = keys || []; }, LOADING(state, data) { state.loading = data; }, PAGE_SET(state, data){ state.page = data; }, SEARCH_QUERY_SET(state, data){ state.searchQuery = data; }, FETCH(state, {rows , count, page}){ state.rows = rows; state.count = count || 0; state.page = page || 1; }, EDIT_ROW(state, data) { if (data.delete) { state.rows = _.without(state.rows, _.find(state.rows, (item) => item.val == data.row.val)); } else { state.rows[data.index] = data.row; } } }; const actions = { async init({commit}){ commit('LOADING', true); try { let res = await axios.get('config/statics'); console.log("res ==> " , res); commit('INJECT' , res.data); } catch (e) { } finally { commit('LOADING', false); } }, async removeProduct({commit , dispatch } , {id}){ commit('LOADING', true); try { let res = await axios.delete('product/'+id); dispatch('product/fetch'); } catch (e) { } finally { commit('LOADING', false); } }, async save({commit ,state}, data) { commit('LOADING', true); try { if(data.id){ const res = await axios.put(`product/${data.id}` , data); return res; }else{ const res = await axios.post('product/' , data); return res; } } catch (e) { } finally { commit('LOADING', false); } }, async fetch({commit ,state}, data) { commit('LOADING', true); try { let {data} = await axios.get('product' , {params:{page:state.page, text_search:state.searchQuery}}); commit('FETCH' , data); } catch (e) { } finally { commit('LOADING', false); } }, async activeDeactive({commit ,state}, payload) { try { let {data} = await axios.put(`product/variant/${payload.id}` , payload); } catch (e) { console.log("e ==> " , e); } finally { } } }; const getters = {}; export default { namespaced: true, state, mutations, actions, getters }