vendors.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. export const useVendorsStore = defineStore('vendorsStore', () => {
  2. // State
  3. const vendors = ref([])
  4. const currentVendor = ref(null)
  5. const loading = ref(false)
  6. const error = ref(null)
  7. // Search & Filter State
  8. const searchConditions = ref({
  9. name: '',
  10. category: '',
  11. page: 1,
  12. size: 10
  13. })
  14. // Pagination State
  15. const pagination = ref({
  16. currentPage: 1,
  17. pageSize: 10,
  18. totalCount: 0,
  19. totalPages: 0
  20. })
  21. // Getters (직접 반환)
  22. const getVendors = computed(() => vendors.value)
  23. const getCurrentVendor = computed(() => currentVendor.value)
  24. const getLoading = computed(() => loading.value)
  25. const getError = computed(() => error.value)
  26. const getSearchConditions = computed(() => searchConditions.value)
  27. const getPagination = computed(() => pagination.value)
  28. // Actions
  29. function setLoading(state) {
  30. loading.value = state
  31. }
  32. function setError(errorMessage) {
  33. error.value = errorMessage
  34. }
  35. function clearError() {
  36. error.value = null
  37. }
  38. function setVendors(vendorList) {
  39. vendors.value = vendorList
  40. }
  41. function setCurrentVendor(vendor) {
  42. currentVendor.value = vendor
  43. }
  44. function updateSearchConditions(conditions) {
  45. searchConditions.value = { ...searchConditions.value, ...conditions }
  46. }
  47. function updatePagination(paginationData) {
  48. pagination.value = { ...pagination.value, ...paginationData }
  49. }
  50. function resetSearch() {
  51. searchConditions.value = {
  52. name: '',
  53. category: '',
  54. page: 1,
  55. size: 10
  56. }
  57. pagination.value = {
  58. currentPage: 1,
  59. pageSize: 10,
  60. totalCount: 0,
  61. totalPages: 0
  62. }
  63. }
  64. // API Actions
  65. async function searchVendors(conditions = {}) {
  66. setLoading(true)
  67. clearError()
  68. try {
  69. const searchParams = { ...searchConditions.value, ...conditions }
  70. updateSearchConditions(searchParams)
  71. const response = await useAxios().get('/vendors', {
  72. params: searchParams
  73. })
  74. if (response.data) {
  75. setVendors(response.data.vendors || [])
  76. updatePagination({
  77. currentPage: response.data.currentPage || 1,
  78. totalCount: response.data.totalCount || 0,
  79. totalPages: Math.ceil((response.data.totalCount || 0) / searchParams.size)
  80. })
  81. }
  82. } catch (err) {
  83. setError(err.message || '벤더사 검색 중 오류가 발생했습니다.')
  84. setVendors([])
  85. } finally {
  86. setLoading(false)
  87. }
  88. }
  89. async function getVendorById(id) {
  90. setLoading(true)
  91. clearError()
  92. try {
  93. const response = await useAxios().get(`/vendors/${id}`)
  94. if (response.data) {
  95. setCurrentVendor(response.data)
  96. return response.data
  97. }
  98. } catch (err) {
  99. setError(err.message || '벤더사 정보를 불러오는 중 오류가 발생했습니다.')
  100. setCurrentVendor(null)
  101. } finally {
  102. setLoading(false)
  103. }
  104. }
  105. return {
  106. // State
  107. vendors,
  108. currentVendor,
  109. loading,
  110. error,
  111. searchConditions,
  112. pagination,
  113. // Getters
  114. getVendors,
  115. getCurrentVendor,
  116. getLoading,
  117. getError,
  118. getSearchConditions,
  119. getPagination,
  120. // Actions
  121. setLoading,
  122. setError,
  123. clearError,
  124. setVendors,
  125. setCurrentVendor,
  126. updateSearchConditions,
  127. updatePagination,
  128. resetSearch,
  129. searchVendors,
  130. getVendorById
  131. }
  132. }, {
  133. persist: {
  134. storage: persistedState.sessionStorage,
  135. paths: ['searchConditions', 'pagination']
  136. }
  137. })