This commit is contained in:
oneao committed 2025-11-19 17:30:44 +08:00
1 parent 7419021927
commit 49285f1df2
16 files changed
+1184 -283

No files matched your search

@@ -1,44 +1,59 @@
import type { MoneyCategoryItem } from '@/types/money/category'
import AsyncStorage from '@react-native-async-storage/async-storage'
import { create } from 'zustand'
import { createJSONStorage, persist } from 'zustand/middleware'
import { queryMoneyCategoryApi } from '@/request/api/money/category'
interface MoneyCategoryStore {
categories: MoneyCategoryItem[]
// 通过 getter 动态计算
expense: MoneyCategoryItem[]
income: MoneyCategoryItem[]
setCategories: (categories: MoneyCategoryItem[]) => void
addCategory: (category: MoneyCategoryItem) => void
removeCategory: (id: number) => void
init: () => Promise<void>
}
export const useMoneyCategoryStore = create<MoneyCategoryStore>()(set => ({
categories: [],
expense: [],
income: [],
export const useMoneyCategoryStore = create<MoneyCategoryStore>()(
persist(
(set, get) => ({
categories: [],
setCategories: categories =>
set({
categories,
expense: categories.filter(c => c.type === 0),
income: categories.filter(c => c.type === 1),
}),
// getter 样式属性,通过 get() 动态计算
get expense() {
return get().categories.filter(c => c.type === 0)
},
get income() {
return get().categories.filter(c => c.type === 1)
},
addCategory: category =>
set((state) => {
const newCategories = [...state.categories, category]
return {
categories: newCategories,
expense: newCategories.filter(c => c.type === 0),
income: newCategories.filter(c => c.type === 1),
}
}),
setCategories: categories => set({ categories }),
removeCategory: id =>
set((state) => {
const newCategories = state.categories.filter(c => c.id !== id)
return {
categories: newCategories,
expense: newCategories.filter(c => c.type === 0),
income: newCategories.filter(c => c.type === 1),
}
addCategory: category =>
set(state => ({ categories: [...state.categories, category] })),
removeCategory: id =>
set(state => ({ categories: state.categories.filter(c => c.id !== id) })),
init: async () => {
try {
const res = await queryMoneyCategoryApi()
if (res.isSuccess) {
const categories = res.data || []
set({ categories })
}
}
catch (error) {
console.error('Failed to fetch categories:', error)
}
},
}),
}))
{
name: 'money-category-storage',
storage: createJSONStorage(() => AsyncStorage),
},
),
)