54 lines
1.3 KiB
JavaScript
54 lines
1.3 KiB
JavaScript
import { defineStore } from 'pinia'
|
|
import { storage, PROFILE_KEY } from '@/utils/storage'
|
|
import { getProfile, updateProfile } from '@/api/profile'
|
|
|
|
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()
|
|
this.exists = res.data.exists
|
|
this.isCompleted = res.data.is_completed
|
|
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)
|
|
}
|
|
|
|
return res.data
|
|
} catch (e) {
|
|
console.error('fetchProfile error:', e)
|
|
throw e
|
|
}
|
|
},
|
|
|
|
async saveProfile(data) {
|
|
try {
|
|
const res = await updateProfile(data)
|
|
this.exists = res.data.exists
|
|
this.isCompleted = res.data.is_completed
|
|
this.profile = res.data.profile
|
|
storage.set(PROFILE_KEY, res.data.profile)
|
|
return res.data
|
|
} catch (e) {
|
|
console.error('saveProfile error:', e)
|
|
throw e
|
|
}
|
|
}
|
|
}
|
|
})
|