列表
This commit is contained in:
@@ -16,15 +16,16 @@ export interface TennisMatch {
|
||||
}
|
||||
|
||||
// Store 状态接口
|
||||
interface TennisState {
|
||||
interface ListState {
|
||||
matches: TennisMatch[]
|
||||
loading: boolean
|
||||
error: string | null
|
||||
lastRefreshTime: string | null
|
||||
isShowFilterPopup: boolean
|
||||
}
|
||||
|
||||
// Store Actions 接口
|
||||
interface TennisActions {
|
||||
interface ListActions {
|
||||
fetchMatches: (params?: {
|
||||
page?: number
|
||||
pageSize?: number
|
||||
@@ -33,36 +34,39 @@ interface TennisActions {
|
||||
}) => Promise<void>
|
||||
refreshMatches: () => Promise<void>
|
||||
clearError: () => void
|
||||
updateState: (payload: Record<string, any>) => void
|
||||
}
|
||||
|
||||
// 完整的 Store 类型
|
||||
type TennisStore = TennisState & TennisActions
|
||||
type TennisStore = ListState & ListActions
|
||||
|
||||
// 创建 store
|
||||
export const useTennisStore = create<TennisStore>()((set, get) => ({
|
||||
export const useListStore = create<TennisStore>()((set, get) => ({
|
||||
// 初始状态
|
||||
matches: [],
|
||||
loading: false,
|
||||
error: null,
|
||||
lastRefreshTime: null,
|
||||
// 是否展示综合筛选弹窗
|
||||
isShowFilterPopup: false,
|
||||
|
||||
// 获取比赛数据
|
||||
fetchMatches: async (params) => {
|
||||
set({ loading: true, error: null })
|
||||
|
||||
|
||||
try {
|
||||
const matches = await getTennisMatches(params)
|
||||
set({
|
||||
matches,
|
||||
loading: false,
|
||||
lastRefreshTime: new Date().toISOString()
|
||||
set({
|
||||
matches,
|
||||
loading: false,
|
||||
lastRefreshTime: new Date().toISOString()
|
||||
})
|
||||
console.log('Store: 成功获取网球比赛数据:', matches.length, '条')
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : '未知错误'
|
||||
set({
|
||||
error: errorMessage,
|
||||
loading: false
|
||||
set({
|
||||
error: errorMessage,
|
||||
loading: false
|
||||
})
|
||||
console.error('Store: 获取网球比赛数据失败:', errorMessage)
|
||||
}
|
||||
@@ -71,20 +75,20 @@ export const useTennisStore = create<TennisStore>()((set, get) => ({
|
||||
// 刷新比赛数据
|
||||
refreshMatches: async () => {
|
||||
set({ loading: true, error: null })
|
||||
|
||||
|
||||
try {
|
||||
const matches = await getTennisMatches()
|
||||
set({
|
||||
matches,
|
||||
loading: false,
|
||||
lastRefreshTime: new Date().toISOString()
|
||||
set({
|
||||
matches,
|
||||
loading: false,
|
||||
lastRefreshTime: new Date().toISOString()
|
||||
})
|
||||
console.log('Store: 成功刷新网球比赛数据:', matches.length, '条')
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : '未知错误'
|
||||
set({
|
||||
error: errorMessage,
|
||||
loading: false
|
||||
set({
|
||||
error: errorMessage,
|
||||
loading: false
|
||||
})
|
||||
console.error('Store: 刷新网球比赛数据失败:', errorMessage)
|
||||
}
|
||||
@@ -93,16 +97,14 @@ export const useTennisStore = create<TennisStore>()((set, get) => ({
|
||||
// 清除错误信息
|
||||
clearError: () => {
|
||||
set({ error: null })
|
||||
},
|
||||
// 更新store数据
|
||||
updateState: (payload: Record<string, any>) => {
|
||||
set({
|
||||
...(payload || {})
|
||||
})
|
||||
}
|
||||
}))
|
||||
|
||||
// 导出便捷的 hooks
|
||||
export const useTennisMatches = () => useTennisStore((state) => state.matches)
|
||||
export const useTennisLoading = () => useTennisStore((state) => state.loading)
|
||||
export const useTennisError = () => useTennisStore((state) => state.error)
|
||||
export const useTennisLastRefresh = () => useTennisStore((state) => state.lastRefreshTime)
|
||||
export const useTennisActions = () => useTennisStore((state) => ({
|
||||
fetchMatches: state.fetchMatches,
|
||||
refreshMatches: state.refreshMatches,
|
||||
clearError: state.clearError
|
||||
}))
|
||||
export const useListState = () => useListStore((state) => state)
|
||||
|
||||
Reference in New Issue
Block a user