qcast-front/src/hooks/main/useMainContentsController.js

67 lines
1.7 KiB
JavaScript

'use client'
import { useContext, useEffect } from 'react'
import { useAxios } from '../useAxios'
import { SessionContext } from '@/app/SessionProvider'
import { QcastContext } from '@/app/QcastProvider'
export const useMainContentsController = () => {
const { session } = useContext(SessionContext)
const { promiseGet } = useAxios()
const { setQcastState, setIsGlobalLoading } = useContext(QcastContext)
useEffect(() => {
setIsGlobalLoading(true)
}, [])
/**
* 최근 물건 목록 조회
*/
const fetchObjectList = async () => {
try {
const apiUrl = `/api/main-page/object/${session?.storeId}/list`
await promiseGet({
url: apiUrl,
}).then((res) => {
if (res.status === 200) {
setQcastState({
saleStoreId: res.data.saleStoreId,
saleStoreName: res.data.saleStoreName,
objectList: res.data.objectList,
businessCharger: res.data.businessCharger,
businessChargerMail: res.data.businessChargerMail,
})
setIsGlobalLoading(false)
} else {
setQcastState({
saleStoreId: '',
saleStoreName: '',
objectList: [],
businessCharger: null,
businessChargerMail: null,
})
setIsGlobalLoading(false)
}
})
} catch (error) {
setIsGlobalLoading(false)
console.error('MAIN API fetching error:', error)
}
}
const initObjectList = () => {
setQcastState({
saleStoreId: '',
saleStoreName: '',
objectList: [],
businessCharger: null,
businessChargerMail: null,
})
}
return {
fetchObjectList,
initObjectList,
}
}