u
This commit is contained in:
1 parent
d9c49b34db
commit
d33cef825a
36 files changed
+753
-1077
No files matched your search
@@ -26,18 +26,20 @@ export const useAppStore = create<AppState>((set, get) => ({
|
||||
const moneyCategoryStore = useMoneyCategoryStore.getState()
|
||||
const spaceStore = useSpaceStore.getState()
|
||||
|
||||
// 如果没登录,跳转到登录页,但不退出初始化
|
||||
if (!userStore.isLoggedIn()) {
|
||||
navStore.navigateToAuth()
|
||||
set({ ready: true })
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
// 分类初始化
|
||||
// 初始化分类
|
||||
await moneyCategoryStore.init()
|
||||
|
||||
// 获取空间信息
|
||||
const res = await getUserSpaceApi()
|
||||
console.log(res.data)
|
||||
|
||||
spaceStore.setUserSpace(res.data)
|
||||
}
|
||||
catch (err) {
|
||||
@@ -47,6 +49,7 @@ export const useAppStore = create<AppState>((set, get) => ({
|
||||
// 初始化完成
|
||||
set({ ready: true })
|
||||
},
|
||||
|
||||
reset: () => {
|
||||
set({ ready: false })
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import type { MoneyRecord, MoneyRecordGroup } from '@/types/money/record'
|
||||
import { create } from 'zustand'
|
||||
import { deleteMoneyRecordApi } from '@/request/api/money/record'
|
||||
import { MoneyCategoryEnum } from '@/types/money/category'
|
||||
import { add, sub } from '@/utils/math'
|
||||
import { isInMonth } from '@/utils/time'
|
||||
@@ -18,10 +17,6 @@ interface MoneyRecordStore {
|
||||
setRecords: (records: MoneyRecord[], month: string) => void
|
||||
setCurrentMonth: (month: string) => void
|
||||
|
||||
addMoneyRecord: (data: MoneyRecord) => void
|
||||
updateMoneyRecord: (updated: MoneyRecord) => void
|
||||
deleteMoneyRecord: (id: number) => void
|
||||
|
||||
getMoneyRecordById: (id: number) => MoneyRecord | null
|
||||
}
|
||||
|
||||
@@ -86,33 +81,6 @@ export const useMoneyRecordStore = create<MoneyRecordStore>((set, get) => ({
|
||||
|
||||
setCurrentMonth: month => set({ currentMonth: month }),
|
||||
|
||||
/* ---------------- actions ---------------- */
|
||||
addMoneyRecord: (data) => {
|
||||
if (!isInMonth(data.record_time, get().currentMonth))
|
||||
return
|
||||
const flatRecords = [...get().records.flatMap(g => g.records), data]
|
||||
const newGroups = recomputeGroups(flatRecords)
|
||||
set({ records: newGroups })
|
||||
},
|
||||
|
||||
updateMoneyRecord: (updated) => {
|
||||
const flatRecords = get().records.flatMap(g =>
|
||||
g.records.map(r => r.id === updated.id ? updated : r),
|
||||
)
|
||||
// 如果更新后的记录不在当前月,则直接删除
|
||||
const filteredRecords = flatRecords.filter(r => isInMonth(r.record_time, get().currentMonth))
|
||||
const newGroups = recomputeGroups(filteredRecords)
|
||||
set({ records: newGroups })
|
||||
},
|
||||
|
||||
deleteMoneyRecord: (id) => {
|
||||
const flatRecords = get().records.flatMap(g => g.records).filter(r => r.id !== id)
|
||||
const newGroups = recomputeGroups(flatRecords)
|
||||
set({ records: newGroups })
|
||||
|
||||
deleteMoneyRecordApi({ id }).catch(console.error)
|
||||
},
|
||||
|
||||
getMoneyRecordById: (id) => {
|
||||
for (const group of get().records) {
|
||||
const record = group.records.find(r => r.id === id)
|
||||
|
||||
@@ -6,7 +6,7 @@ interface SpaceState {
|
||||
clear: () => void
|
||||
}
|
||||
|
||||
export const useSpaceStore = create<SpaceState>(set => ({
|
||||
export const useSpaceStore = create<SpaceState>((set, get) => ({
|
||||
userSpace: null,
|
||||
setUserSpace: space => set({ userSpace: space }),
|
||||
clear: () => {
|
||||
|
||||
Reference in new issue
Block a user