重构
This commit is contained in:
1 parent
dc23ea53ed
commit
8c7a9040d2
106 files changed
+5668
-9910
No files matched your search
@@ -0,0 +1,111 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import {
|
||||
createFeeding,
|
||||
createPee,
|
||||
createPoop,
|
||||
endFeeding,
|
||||
endSleep,
|
||||
getFamilyContext,
|
||||
getDailySummary,
|
||||
startFeeding,
|
||||
updateBaby,
|
||||
startSleep,
|
||||
updateCurrentBaby
|
||||
} from '../api/client'
|
||||
import type { BabyProfile, DailySummary, FamilyContext, FamilyMember, FeedingMethod } from '../types/domain'
|
||||
|
||||
export const useCareStore = defineStore('care', {
|
||||
state: () => ({
|
||||
context: null as FamilyContext | null,
|
||||
babies: [] as BabyProfile[],
|
||||
members: [] as FamilyMember[],
|
||||
selectedBabyId: localStorage.getItem('selectedBabyId') || '',
|
||||
baby: null as BabyProfile | null,
|
||||
summary: null as DailySummary | null,
|
||||
operatorName: localStorage.getItem('operatorName') || '宝妈',
|
||||
loading: false,
|
||||
error: ''
|
||||
}),
|
||||
actions: {
|
||||
async load() {
|
||||
this.loading = true
|
||||
this.error = ''
|
||||
try {
|
||||
this.context = await getFamilyContext()
|
||||
this.babies = this.context.babies
|
||||
this.members = this.context.members
|
||||
if (!this.selectedBabyId && this.babies[0]) {
|
||||
this.selectedBabyId = this.babies[0].babyId
|
||||
}
|
||||
const selected = this.babies.find(baby => baby.babyId === this.selectedBabyId) || this.babies[0]
|
||||
this.baby = selected || null
|
||||
if (this.baby) {
|
||||
localStorage.setItem('selectedBabyId', this.baby.babyId)
|
||||
this.summary = await getDailySummary(this.baby.babyId)
|
||||
}
|
||||
} catch (error) {
|
||||
this.error = error instanceof Error ? error.message : '加载失败'
|
||||
} finally {
|
||||
this.loading = false
|
||||
}
|
||||
},
|
||||
async refreshSummary() {
|
||||
if (!this.baby) return
|
||||
this.summary = await getDailySummary(this.baby.babyId)
|
||||
},
|
||||
async selectBaby(babyId: string) {
|
||||
this.selectedBabyId = babyId
|
||||
localStorage.setItem('selectedBabyId', babyId)
|
||||
this.baby = this.babies.find(baby => baby.babyId === babyId) || this.baby
|
||||
await this.refreshSummary()
|
||||
},
|
||||
setOperatorName(name: string) {
|
||||
this.operatorName = name || '宝妈'
|
||||
localStorage.setItem('operatorName', this.operatorName)
|
||||
},
|
||||
async saveBaby(payload: Pick<BabyProfile, 'name' | 'gender' | 'birthDate' | 'deliveryType'>) {
|
||||
this.baby = this.baby ? await updateBaby(this.baby.babyId, payload) : await updateCurrentBaby(payload)
|
||||
this.babies = this.babies.map(baby => baby.babyId === this.baby?.babyId ? this.baby : baby)
|
||||
await this.refreshSummary()
|
||||
},
|
||||
async logFeeding(method: FeedingMethod, amountMl?: number, breastSide?: 'left' | 'right') {
|
||||
if (!this.baby) return
|
||||
await createFeeding({
|
||||
babyId: this.baby.babyId,
|
||||
feedingMethod: method,
|
||||
amountMl,
|
||||
breastSide,
|
||||
operatorName: this.operatorName
|
||||
})
|
||||
await this.refreshSummary()
|
||||
},
|
||||
async toggleBreastfeedTimer(breastSide: 'left' | 'right') {
|
||||
if (!this.baby) return
|
||||
if (this.summary?.activeFeeding) {
|
||||
await endFeeding({ babyId: this.baby.babyId, breastSide, operatorName: this.operatorName })
|
||||
} else {
|
||||
await startFeeding({ babyId: this.baby.babyId, breastSide, operatorName: this.operatorName })
|
||||
}
|
||||
await this.refreshSummary()
|
||||
},
|
||||
async toggleSleep() {
|
||||
if (!this.baby) return
|
||||
if (this.summary?.activeSleep) {
|
||||
await endSleep({ babyId: this.baby.babyId, operatorName: this.operatorName })
|
||||
} else {
|
||||
await startSleep({ babyId: this.baby.babyId, operatorName: this.operatorName })
|
||||
}
|
||||
await this.refreshSummary()
|
||||
},
|
||||
async logPee() {
|
||||
if (!this.baby) return
|
||||
await createPee({ babyId: this.baby.babyId, operatorName: this.operatorName })
|
||||
await this.refreshSummary()
|
||||
},
|
||||
async logPoop() {
|
||||
if (!this.baby) return
|
||||
await createPoop({ babyId: this.baby.babyId, operatorName: this.operatorName })
|
||||
await this.refreshSummary()
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in new issue
Block a user