Files
smt/src/stores/profile.js
T
2026-03-18 19:24:51 +08:00

67 lines
1.7 KiB
JavaScript

import { defineStore } from 'pinia'
import { storage, PROFILE_KEY } from '@/utils/storage'
import { getProfile, updateProfile } from '@/api/profile'
import { useUserStore } from '@/stores/user'
export const useProfileStore = defineStore('profile', {
state: () => ({
exists: false,
isCompleted: false,
profile: storage.get(PROFILE_KEY),
awakeMinutes: 960,
baselineIntervalMinutes: 60
}),
getters: {
needOnboarding: (state) => !state.exists || !state.isCompleted
},
actions: {
async fetchProfile() {
try {
const res = await getProfile()
const userStore = useUserStore()
this.exists = res.data.exists
this.awakeMinutes = res.data.awake_minutes || 960
this.baselineIntervalMinutes = res.data.baseline_interval_minutes || 60
if (res.data.profile) {
this.profile = res.data.profile
storage.set(PROFILE_KEY, res.data.profile)
if (res.data.profile.mode) {
userStore.setMode(res.data.profile.mode)
}
this.isCompleted = res.data.is_completed ||
!!res.data.profile.onboarding_completed_at ||
res.data.profile.baseline_cigs_per_day > 0
} else {
this.isCompleted = res.data.is_completed
}
return res.data
} catch (e) {
console.error('fetchProfile error:', e)
throw e
}
},
async saveProfile(data) {
try {
const res = await updateProfile(data)
const userStore = useUserStore()
this.exists = res.data.exists
this.isCompleted = res.data.is_completed
this.profile = res.data.profile
storage.set(PROFILE_KEY, res.data.profile)
if (res.data.profile?.mode) {
userStore.setMode(res.data.profile.mode)
}
return res.data
} catch (e) {
console.error('saveProfile error:', e)
throw e
}
}
}
})