12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- const state = {
- customers: [],
- filteredCustomers: [],
- loadingCustomers: false,
- showingCustomerForm: false,
- selectedCustomer: null
- }
- const getters = {
- customers(state) {
- return state.customers
- },
- visibleCustomers(state) {
- return state.filteredCustomers.length === 0 ? state.customers : state.filteredCustomers
- },
- loadingCustomers(state) {
- return state.loadingCustomers
- },
- showingCustomerForm(state) {
- return state.showingCustomerForm
- },
- selectedCustomer(state) {
- return state.selectedCustomer
- },
- selectedCustomerName(state) {
- return state.selectedCustomer && state.selectedCustomer.name
- },
- customerCredit(state) {
- return state.selectedCustomer && state.selectedCustomer.creditLimit >= state.selectedCustomer.credit ? state.selectedCustomer.creditLimit - state.credit : 0
- }
- }
- const mutations = {
- setCustomers(state, payload) {
- state.customers = payload
- },
- setFilteredCustomers(state, payload) {
- state.filteredCustomers = [...payload]
- },
- setLoadingCustomers(state, payload) {
- state.loadingCustomers = !!payload
- },
- setShowCustomerForm(state, payload) {
- state.showingCustomerForm = !!payload
- },
- addCustomer(state, payload) {
- state.customers = [payload, ...state.customers]
- },
- setSelectedCustomer(state, payload) {
- state.selectedCustomer = payload
- }
- }
- const actions = {
- initCustomers({ commit }, payload) {
- commit('setCustomers', payload)
- commit('setLoadingCustomers')
- },
- filterCustomers({ commit }, payload) {
- commit('setFilteredCustomers', payload)
- },
- showCustomerForm({ commit }) {
- commit('setShowCustomerForm', true)
- },
- hideCustomerForm({ commit }) {
- commit('setShowCustomerForm', false)
- },
- submitCustomer({ commit, dispatch }, payload) {
- commit('setLoadingCustomers', true)
- dispatch('createCustomer', payload)
- dispatch('hideCustomerForm')
- },
- receiveCustomer({ commit }, payload) {
- commit('addCustomer', payload)
- commit('setLoadingCustomers', false)
- },
- selectCustomer({ commit }, payload) {
- commit('setSelectedCustomer', payload)
- },
- resetCustomer({ commit }) {
- commit('setLoadingCustomers', true)
- commit('setCustomers', [])
- commit('setFilteredCustomers', [])
- commit('setSelectedCustomer', null)
- }
- }
- export default {
- state,
- getters,
- mutations,
- actions
- }
|