flaschengeist-frontend/src/store/modules/pricelist.js

270 lines
7.8 KiB
JavaScript

import url from '@/plugins/routes'
import axios from 'axios'
const timeout = 20000
const state = {
priceListLoading: false,
typesLoading: false,
priceList: [],
types: []
}
const mutations = {
setPriceList: (state, priceList) => {
state.priceList = priceList
},
setTypes: (state, types) => {
state.types = types
state.types.sort((a, b) => {
const low = b.name.toLowerCase()
const high = a.name.toLowerCase()
if (high < low) return -1
if (high > low) return 1
return 0
})
},
updatePriceList: (state, drink) => {
var a = state.priceList.find(b => {
return b.id === drink.id
})
if (a) {
a.name = drink.name
a.type = drink.type
a.price = drink.price
a.price_big = drink.price_big
a.price_club = drink.price_club
a.price_club_big = drink.price_club_big
a.premium = drink.premium
a.premium_club = drink.premium_club
a.price_extern_club = drink.price_extern_club
} else {
state.priceList.push({
id: drink.id,
name: drink.name,
type: drink.type,
price: drink.price,
price_big: drink.price_big,
price_club: drink.price_club,
price_club_big: drink.price_club_big,
premium: drink.premium,
premium_club: drink.premium_club,
price_extern_club: drink.price_extern_club
})
}
},
deleteDrinkPrice: (state, data) => {
var index = state.priceList.indexOf(
state.priceList.find(a => {
return a.id === data.id
})
)
state.priceList.splice(index, 1)
},
updateDrinkType: (state, type) => {
var a = state.types.find(b => {
return b.id === type.id
})
if (a) {
a.name = type.name
} else {
state.types.push({
...type
})
}
state.types.sort((a, b) => {
const low = b.name.toLowerCase()
const high = a.name.toLowerCase()
if (high < low) return -1
if (high > low) return 1
return 0
})
},
deleteDrinkType: (state, type) => {
var index = state.types.indexOf(
state.types.find(a => {
return a.id === type.id
})
)
state.types.splice(index, 1)
},
setPriceListLoading: (state, value) => {
state.priceListLoading = value
},
setTypesLoading: (state, value) => {
state.typesLoading = value
}
}
const actions = {
async getPriceList({ commit, dispatch }) {
try {
commit('setPriceListLoading', true)
const response = await axios.get(url.pricelist, { timeout })
commit('setPriceList', response.data)
commit('setPriceListLoading', false)
dispatch('getLifetime', null, { root: true })
} catch (e) {
if (e.message == 'Network Error') {
dispatch('connectionError/addError', null, { root: true })
}
commit('setPriceListLoading', false)
}
},
async getTypes({ commit, dispatch }) {
try {
commit('setTypesLoading', true)
const response = await axios.get(url.getTypes, { timeout })
commit('setTypes', response.data)
commit('setTypesLoading', false)
dispatch('getLifetime', null, { root: true })
} catch (e) {
if (e.message == 'Network Error') {
dispatch('connectionError/addError', null, { root: true })
}
commit('setTypesLoading', false)
}
},
async setDrink({ commit, rootState, dispatch }, data) {
try {
commit('setPriceListLoading', true)
const response = await axios.post(
url.gastro.setDrink,
{ ...data },
{ headers: { Token: rootState.login.user.accessToken }, timeout }
)
commit('updatePriceList', response.data)
commit('setPriceListLoading', false)
dispatch('getLifetime', null, { root: true })
} catch (e) {
if (e.message == 'Network Error') {
dispatch('connectionError/addError', null, { root: true })
}
commit('setPriceListLoading', false)
if (e.response)
if (e.response.status === 401) dispatch('logout', null, { root: true })
}
},
async updateDrink({ commit, rootState, dispatch }, data) {
try {
commit('setPriceListLoading', true)
const response = await axios.post(
url.gastro.updateDrink,
{ ...data },
{ headers: { Token: rootState.login.user.accessToken }, timeout }
)
commit('updatePriceList', response.data)
commit('setPriceListLoading', false)
dispatch('getLifetime', null, { root: true })
} catch (e) {
if (e.message == 'Network Error') {
dispatch('connectionError/addError', null, { root: true })
}
commit('setPriceListLoading', false)
if (e.response)
if (e.response.status === 401) dispatch('logout', null, { root: true })
}
},
async deleteDrink({ commit, rootState, dispatch }, data) {
try {
commit('setPriceListLoading', true)
await axios.post(
url.gastro.deleteDrink,
{ ...data },
{ headers: { Token: rootState.login.user.accessToken }, timeout }
)
commit('deleteDrinkPrice', data)
commit('setPriceListLoading', false)
dispatch('getLifetime', null, { root: true })
} catch (e) {
if (e.message == 'Network Error') {
dispatch('connectionError/addError', null, { root: true })
}
commit('setPriceListLoading', false)
if (e.response)
if (e.response.status === 401) dispatch('logout', null, { root: true })
}
},
async setDrinkType({ commit, rootState, dispatch }, data) {
try {
commit('setTypesLoading', true)
const response = await axios.post(
url.gastro.setType,
{ ...data },
{ headers: { Token: rootState.login.user.accessToken }, timeout }
)
commit('updateDrinkType', response.data)
commit('setTypesLoading', false)
dispatch('getLifetime', null, { root: true })
} catch (e) {
if (e.message == 'Network Error') {
dispatch('connectionError/addError', null, { root: true })
}
commit('setTypesLoading', false)
if (e.response)
if (e.response.status === 401) dispatch('logout', null, { root: true })
}
},
async updateDrinkType({ commit, rootState, dispatch }, data) {
try {
commit('setTypesLoading', true)
const response = await axios.post(
url.gastro.updateType,
{ ...data },
{ headers: { Token: rootState.login.user.accessToken }, timeout }
)
commit('updateDrinkType', response.data)
commit('setTypesLoading', false)
dispatch('getLifetime', null, { root: true })
} catch (e) {
if (e.message == 'Network Error') {
dispatch('connectionError/addError', null, { root: true })
}
commit('setTypesLoading', false)
if (e.response)
if (e.response.status === 401) dispatch('logout', null, { root: true })
}
},
async deleteDrinkType({ commit, rootState, dispatch }, data) {
try {
commit('setTypesLoading', true)
await axios.post(
url.gastro.deleteType,
{ ...data },
{ headers: { Token: rootState.login.user.accessToken }, timeout }
)
commit('deleteDrinkType', data)
commit('setTypesLoading', false)
dispatch('getLifetime', null, { root: true })
} catch (e) {
if (e.message == 'Network Error') {
dispatch('connectionError/addError', null, { root: true })
}
commit('setTypesLoading', false)
if (e.response)
if (e.response.status === 401) dispatch('logout', null, { root: true })
}
}
}
const getters = {
priceList: state => {
return state.priceList
},
types: state => {
return state.types
},
priceListLoading: state => {
return state.priceListLoading
},
typesLoading: state => {
return state.typesLoading
}
}
export default {
namespaced: true,
state,
mutations,
actions,
getters
}