| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- export const useVendorsStore = defineStore('vendorsStore', () => {
- // State
- const vendors = ref([])
- const currentVendor = ref(null)
- const loading = ref(false)
- const error = ref(null)
-
- // Search & Filter State
- const searchConditions = ref({
- name: '',
- category: '',
- page: 1,
- size: 10
- })
-
- // Pagination State
- const pagination = ref({
- currentPage: 1,
- pageSize: 10,
- totalCount: 0,
- totalPages: 0
- })
- // Getters (직접 반환)
- const getVendors = computed(() => vendors.value)
- const getCurrentVendor = computed(() => currentVendor.value)
- const getLoading = computed(() => loading.value)
- const getError = computed(() => error.value)
- const getSearchConditions = computed(() => searchConditions.value)
- const getPagination = computed(() => pagination.value)
- // Actions
- function setLoading(state) {
- loading.value = state
- }
- function setError(errorMessage) {
- error.value = errorMessage
- }
- function clearError() {
- error.value = null
- }
- function setVendors(vendorList) {
- vendors.value = vendorList
- }
- function setCurrentVendor(vendor) {
- currentVendor.value = vendor
- }
- function updateSearchConditions(conditions) {
- searchConditions.value = { ...searchConditions.value, ...conditions }
- }
- function updatePagination(paginationData) {
- pagination.value = { ...pagination.value, ...paginationData }
- }
- function resetSearch() {
- searchConditions.value = {
- name: '',
- category: '',
- page: 1,
- size: 10
- }
- pagination.value = {
- currentPage: 1,
- pageSize: 10,
- totalCount: 0,
- totalPages: 0
- }
- }
- // Reset function
- function reset() {
- vendors.value = []
- currentVendor.value = null
- loading.value = false
- error.value = null
- searchConditions.value = {
- name: '',
- category: '',
- page: 1,
- size: 10
- }
- pagination.value = {
- currentPage: 1,
- pageSize: 10,
- totalCount: 0,
- totalPages: 0
- }
- }
- // API Actions
- async function searchVendors(conditions = {}) {
- setLoading(true)
- clearError()
-
- try {
- const searchParams = { ...searchConditions.value, ...conditions }
- updateSearchConditions(searchParams)
-
- const response = await useAxios().get('/vendors', {
- params: searchParams
- })
-
- if (response.data) {
- setVendors(response.data.vendors || [])
- updatePagination({
- currentPage: response.data.currentPage || 1,
- totalCount: response.data.totalCount || 0,
- totalPages: Math.ceil((response.data.totalCount || 0) / searchParams.size)
- })
- }
- } catch (err) {
- setError(err.message || '벤더사 검색 중 오류가 발생했습니다.')
- setVendors([])
- } finally {
- setLoading(false)
- }
- }
- async function getVendorById(id) {
- setLoading(true)
- clearError()
-
- try {
- const response = await useAxios().get(`/vendors/${id}`)
-
- if (response.data) {
- setCurrentVendor(response.data)
- return response.data
- }
- } catch (err) {
- setError(err.message || '벤더사 정보를 불러오는 중 오류가 발생했습니다.')
- setCurrentVendor(null)
- } finally {
- setLoading(false)
- }
- }
- return {
- // State
- vendors,
- currentVendor,
- loading,
- error,
- searchConditions,
- pagination,
-
- // Getters
- getVendors,
- getCurrentVendor,
- getLoading,
- getError,
- getSearchConditions,
- getPagination,
-
- // Actions
- setLoading,
- setError,
- clearError,
- setVendors,
- setCurrentVendor,
- updateSearchConditions,
- updatePagination,
- resetSearch,
- searchVendors,
- getVendorById,
- reset
- }
- }, {
- persist: {
- storage: persistedState.sessionStorage,
- paths: ['searchConditions', 'pagination']
- }
- })
|