journal.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. const state = {
  2. journals: [],
  3. loadingJournals: false,
  4. selectedJournal: null
  5. }
  6. const getters = {
  7. journals(state) {
  8. return state.journals
  9. },
  10. bankJournals(state, getters) {
  11. return state.journals.filter(j => j.type === 'bank' && j.currencyId === getters.selectedCurrency.id)
  12. },
  13. loadingJournals(state) {
  14. return state.loadingJournals
  15. },
  16. selectedJournal(state) {
  17. return state.selectedJournal
  18. },
  19. hasJournalSelected(state) {
  20. return !!state.selectedJournal
  21. },
  22. hasCashJournalSelected(state) {
  23. return !!state.selectedJournal && state.selectedJournal.type === 'cash'
  24. },
  25. hasBankJournalSelected(state) {
  26. return !!state.selectedJournal && state.selectedJournal.type === 'bank'
  27. }
  28. }
  29. const mutations = {
  30. setJournals (state, payload) {
  31. state.journals = payload
  32. },
  33. setLoadingJournals (state, payload) {
  34. state.loadingJournals = !!payload
  35. },
  36. autoSelectJournal (state) {
  37. const { selectedCurrency } = this.getters
  38. state.selectedJournal = state.journals.find(j => j.type === 'cash' && j.currencyId === selectedCurrency.id)
  39. },
  40. setSelectedJournal (state, journalId) {
  41. if (!journalId) {
  42. state.selectedJournal = null
  43. return
  44. }
  45. state.selectedJournal = state.journals.find(item => item.id === journalId)
  46. }
  47. }
  48. const actions = {
  49. initJournals ({ commit }, payload) {
  50. commit('setJournals', payload)
  51. commit('autoSelectJournal')
  52. commit('setLoadingJournals')
  53. },
  54. autoSelectJournal({ commit }) {
  55. commit('autoSelectJournal')
  56. },
  57. selectJournal ({ commit }, journalId) {
  58. commit('setSelectedJournal', journalId)
  59. },
  60. resetJournal ({ getters, commit }) {
  61. if (getters.isWired) {
  62. commit('setLoadingJournals', true)
  63. commit('setJournals', [])
  64. }
  65. commit('setSelectedJournal', null)
  66. commit('autoSelectJournal')
  67. }
  68. }
  69. export default {
  70. state,
  71. getters,
  72. mutations,
  73. actions
  74. }