41 lines
833 B
TypeScript
41 lines
833 B
TypeScript
export function useNewsletterSignup() {
|
|
const { t } = useI18n()
|
|
const isLoading = ref(false)
|
|
const isSuccess = ref(false)
|
|
const error = ref<string | null>(null)
|
|
|
|
const submitEmail = async (email: string) => {
|
|
isLoading.value = true
|
|
error.value = null
|
|
|
|
try {
|
|
await $fetch('/api/newsletter/subscribe', {
|
|
method: 'POST',
|
|
body: { email }
|
|
})
|
|
|
|
isSuccess.value = true
|
|
} catch (e: unknown) {
|
|
const fetchError = e as { statusMessage?: string }
|
|
error.value = fetchError.statusMessage || t('errors.generic')
|
|
throw e
|
|
} finally {
|
|
isLoading.value = false
|
|
}
|
|
}
|
|
|
|
const reset = () => {
|
|
isLoading.value = false
|
|
isSuccess.value = false
|
|
error.value = null
|
|
}
|
|
|
|
return {
|
|
isLoading,
|
|
isSuccess,
|
|
error,
|
|
submitEmail,
|
|
reset
|
|
}
|
|
}
|