vendors.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. // Reset function
  65. function reset() {
  66. vendors.value = []
  67. currentVendor.value = null
  68. loading.value = false
  69. error.value = null
  70. searchConditions.value = {
  71. name: '',
  72. category: '',
  73. page: 1,
  74. size: 10
  75. }
  76. pagination.value = {
  77. currentPage: 1,
  78. pageSize: 10,
  79. totalCount: 0,
  80. totalPages: 0
  81. }
  82. }
  83. // API Actions
  84. async function searchVendors(conditions = {}) {
  85. setLoading(true)
  86. clearError()
  87. try {
  88. const searchParams = { ...searchConditions.value, ...conditions }
  89. updateSearchConditions(searchParams)
  90. const response = await useAxios().get('/vendors', {
  91. params: searchParams
  92. })
  93. if (response.data) {
  94. setVendors(response.data.vendors || [])
  95. updatePagination({
  96. currentPage: response.data.currentPage || 1,
  97. totalCount: response.data.totalCount || 0,
  98. totalPages: Math.ceil((response.data.totalCount || 0) / searchParams.size)
  99. })
  100. }
  101. } catch (err) {
  102. setError(err.message || '벤더사 검색 중 오류가 발생했습니다.')
  103. setVendors([])
  104. } finally {
  105. setLoading(false)
  106. }
  107. }
  108. async function getVendorById(id) {
  109. setLoading(true)
  110. clearError()
  111. try {
  112. const response = await useAxios().get(`/vendors/${id}`)
  113. if (response.data) {
  114. setCurrentVendor(response.data)
  115. return response.data
  116. }
  117. } catch (err) {
  118. setError(err.message || '벤더사 정보를 불러오는 중 오류가 발생했습니다.')
  119. setCurrentVendor(null)
  120. } finally {
  121. setLoading(false)
  122. }
  123. }
  124. return {
  125. // State
  126. vendors,
  127. currentVendor,
  128. loading,
  129. error,
  130. searchConditions,
  131. pagination,
  132. // Getters
  133. getVendors,
  134. getCurrentVendor,
  135. getLoading,
  136. getError,
  137. getSearchConditions,
  138. getPagination,
  139. // Actions
  140. setLoading,
  141. setError,
  142. clearError,
  143. setVendors,
  144. setCurrentVendor,
  145. updateSearchConditions,
  146. updatePagination,
  147. resetSearch,
  148. searchVendors,
  149. getVendorById,
  150. reset
  151. }
  152. }, {
  153. persist: {
  154. storage: persistedState.sessionStorage,
  155. paths: ['searchConditions', 'pagination']
  156. }
  157. })