Some checks failed
ci / ci (22, ubuntu-latest) (push) Has been cancelled
Nuxt 4 + Supabase + Flightics API. Incluye búsqueda de vuelos, inspiraciones, watchlist, tracking de precios y mapa interactivo. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
63 lines
1.6 KiB
TypeScript
63 lines
1.6 KiB
TypeScript
interface UserProfile {
|
|
home_airports: string[]
|
|
default_adults: number
|
|
default_children: number
|
|
default_infants: number
|
|
locale: string
|
|
show_origin_time: boolean
|
|
}
|
|
|
|
export function useUserPreferences() {
|
|
const user = useSupabaseUser()
|
|
const profile = useState<UserProfile | null>('user-profile', () => null)
|
|
const loading = useState<boolean>('user-profile-loading', () => false)
|
|
|
|
let fetchPromise: Promise<void> | null = null
|
|
|
|
async function fetchProfile() {
|
|
if (!user.value) return
|
|
if (fetchPromise) return fetchPromise
|
|
loading.value = true
|
|
fetchPromise = (async () => {
|
|
try {
|
|
const data = await $fetch<UserProfile>('/api/profile')
|
|
profile.value = data
|
|
} catch {
|
|
profile.value = null
|
|
} finally {
|
|
loading.value = false
|
|
fetchPromise = null
|
|
}
|
|
})()
|
|
return fetchPromise
|
|
}
|
|
|
|
async function updateProfile(updates: Partial<UserProfile>) {
|
|
if (!user.value) return
|
|
try {
|
|
const data = await $fetch<UserProfile>('/api/profile', {
|
|
method: 'PATCH',
|
|
body: updates
|
|
})
|
|
profile.value = data
|
|
} catch {
|
|
// silent
|
|
}
|
|
}
|
|
|
|
const homeAirports = computed(() => profile.value?.home_airports ?? [])
|
|
|
|
const defaultPassengers = computed(() => ({
|
|
adult: profile.value?.default_adults ?? 1,
|
|
child: profile.value?.default_children ?? 0,
|
|
infant: profile.value?.default_infants ?? 0
|
|
}))
|
|
|
|
watch(user, (u) => {
|
|
if (u) fetchProfile()
|
|
else profile.value = null
|
|
}, { immediate: true })
|
|
|
|
return { profile, loading, homeAirports, defaultPassengers, fetchProfile, updateProfile }
|
|
}
|