customer.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. const state = {
  2. customers: [],
  3. filteredCustomers: [],
  4. loadingCustomers: false,
  5. showingCustomerForm: false,
  6. selectedCustomer: null
  7. }
  8. const getters = {
  9. customers(state) {
  10. return state.customers
  11. },
  12. visibleCustomers(state) {
  13. return state.filteredCustomers.length === 0 ? state.customers : state.filteredCustomers
  14. },
  15. loadingCustomers(state) {
  16. return state.loadingCustomers
  17. },
  18. showingCustomerForm(state) {
  19. return state.showingCustomerForm
  20. },
  21. selectedCustomer(state) {
  22. return state.selectedCustomer
  23. },
  24. selectedCustomerName(state) {
  25. return state.selectedCustomer && state.selectedCustomer.name
  26. },
  27. customerCredit(state) {
  28. return state.selectedCustomer && state.selectedCustomer.creditLimit >= state.selectedCustomer.credit ? state.selectedCustomer.creditLimit - state.credit : 0
  29. }
  30. }
  31. const mutations = {
  32. setCustomers(state, payload) {
  33. state.customers = payload
  34. },
  35. setFilteredCustomers(state, payload) {
  36. state.filteredCustomers = [...payload]
  37. },
  38. setLoadingCustomers(state, payload) {
  39. state.loadingCustomers = !!payload
  40. },
  41. setShowCustomerForm(state, payload) {
  42. state.showingCustomerForm = !!payload
  43. },
  44. addCustomer(state, payload) {
  45. state.customers = [payload, ...state.customers]
  46. },
  47. setSelectedCustomer(state, payload) {
  48. state.selectedCustomer = payload
  49. }
  50. }
  51. const actions = {
  52. initCustomers({ commit }, payload) {
  53. commit('setCustomers', payload)
  54. commit('setLoadingCustomers')
  55. },
  56. filterCustomers({ commit }, payload) {
  57. commit('setFilteredCustomers', payload)
  58. },
  59. showCustomerForm({ commit }) {
  60. commit('setShowCustomerForm', true)
  61. },
  62. hideCustomerForm({ commit }) {
  63. commit('setShowCustomerForm', false)
  64. },
  65. submitCustomer({ commit, dispatch }, payload) {
  66. commit('setLoadingCustomers', true)
  67. dispatch('createCustomer', payload)
  68. dispatch('hideCustomerForm')
  69. },
  70. receiveCustomer({ commit }, payload) {
  71. commit('addCustomer', payload)
  72. commit('setLoadingCustomers', false)
  73. },
  74. selectCustomer({ commit }, payload) {
  75. commit('setSelectedCustomer', payload)
  76. },
  77. resetCustomer({ commit }) {
  78. commit('setLoadingCustomers', true)
  79. commit('setCustomers', [])
  80. commit('setFilteredCustomers', [])
  81. commit('setSelectedCustomer', null)
  82. }
  83. }
  84. export default {
  85. state,
  86. getters,
  87. mutations,
  88. actions
  89. }