import {events} from '@/bus' import axios from "axios" import Vue from "vue" const defaultState = { invoiceSorting: { sort: localStorage.getItem('sorting') ? JSON.parse(localStorage.getItem('sorting')).sort : 'DESC', field: localStorage.getItem('sorting') ? JSON.parse(localStorage.getItem('sorting')).field : 'created_at', }, } const actions = { getInvoices: ({commit, getters}, type) => { commit('LOADING_STATE', {loading: true, data: []}) commit('STORE_CURRENT_FOLDER', { name: 'Invoices', id: undefined, location: type, }) let order = getters.invoiceSorting.sorting.sort === 'DESC' ? '-' : '' axios .get(`/api/v1/invoicing/invoices?filter[invoice_type]=${type}&sort=${order}${getters.invoiceSorting.sorting.field}`) .then(response => { commit('LOADING_STATE', {loading: false, data: response.data}) }) .catch(() => { Vue.prototype.$isSomethingWrong() }) .finally(() => { events.$emit('scrollTop') }) }, getClients: ({commit, getters}) => { commit('LOADING_STATE', {loading: true, data: []}) commit('STORE_CURRENT_FOLDER', { name: 'Clients', id: undefined, location: 'clients', }) let order = getters.invoiceSorting.sorting.sort === 'DESC' ? '-' : '' axios .get(`/api/v1/invoicing/clients?sort=${order}${getters.invoiceSorting.sorting.field}`) .then(response => { commit('LOADING_STATE', {loading: false, data: response.data}) }) .catch(() => { Vue.prototype.$isSomethingWrong() }) .finally(() => { events.$emit('scrollTop') }) }, getSearchResultForInvoices: ({commit, getters}, query) => { commit('LOADING_STATE', {loading: true, data: []}) commit('CHANGE_SEARCHING_STATE', true) axios .get('/api/v1/invoicing/invoices/search', { params: { query: query, type: getters.currentFolder.location, } }) .then(response => { commit('LOADING_STATE', {loading: false, data: response.data}) }) .catch(() => Vue.prototype.$isSomethingWrong()) }, getSearchResultForClients: ({commit, getters}, query) => { commit('LOADING_STATE', {loading: true, data: []}) commit('CHANGE_SEARCHING_STATE', true) axios .get('/api/v1/invoicing/clients/search', { params: {query: query} }) .then(response => { commit('LOADING_STATE', {loading: false, data: response.data}) }) .catch(() => Vue.prototype.$isSomethingWrong()) }, } const mutations = { UPDATE_INVOICE_SORTING(state) { state.invoiceSorting.field = JSON.parse(localStorage.getItem('sorting-invoices')).field state.invoiceSorting.sort = JSON.parse(localStorage.getItem('sorting-invoices')).sort }, } const getters = { invoiceSorting: (state) => { return {sorting: state.invoiceSorting, URI: '?sort=' + state.invoiceSorting.field + '&direction=' + state.invoiceSorting.sort} }, } export default { state: defaultState, getters, actions, mutations }