Files
TestEShopDist/src/use/useCommonData.ts
T
2022-12-02 18:59:29 +08:00

35 lines
1007 B
TypeScript

import { useDispatch } from 'react-redux'
import { useCallback, useState } from 'react'
import { CLEAR_SHOPCOUNT, SET_SHOPCOUNT } from '@/constants/common'
import { DataParam } from '@/reducers/commonData'
import { GetShoppingCartApi } from '@/api/shopCart'
import { useSelector } from '@/reducers/hooks'
export default () => {
const commonData = useSelector(state => state.commonData)
const dispatch = useDispatch()
const setShopCount = (shopCount: number) => {
dispatch({ type: SET_SHOPCOUNT, data: { shopCount } })
}
const removeShopCount = () => {
dispatch({ type: CLEAR_SHOPCOUNT })
}
const { fetchData: fetchDataShopCount } = GetShoppingCartApi()
const getShopCount = async() => {
// 获取购物车数据数量
const { data } = await fetchDataShopCount()
const color_list = data.color_list || []
setShopCount(color_list.length)
}
return {
setShopCount,
removeShopCount,
getShopCount,
commonData,
}
}