46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
export interface ContactFormData {
|
|
name: string;
|
|
phone: string;
|
|
email: string;
|
|
comment?: string;
|
|
}
|
|
|
|
export function useContact() {
|
|
const config = useRuntimeConfig();
|
|
const loading = ref(false);
|
|
const error = ref<string | null>(null);
|
|
const success = ref(false);
|
|
const aiReply = ref<string | null>(null);
|
|
|
|
async function submit(data: ContactFormData) {
|
|
loading.value = true;
|
|
error.value = null;
|
|
try {
|
|
const res = await $fetch<{ success: boolean; message: string; ai: { sentiment: string; category: string; autoReply: string } | null }>(
|
|
`${config.public.apiBase}/contact`,
|
|
{
|
|
method: 'POST',
|
|
body: data,
|
|
},
|
|
);
|
|
success.value = true;
|
|
aiReply.value = res.ai?.autoReply ?? null;
|
|
} catch (e: any) {
|
|
if (e.status === 429)
|
|
error.value = 'Слишком много запросов. Попробуйте через минуту.';
|
|
else
|
|
error.value = e.data?.message ?? 'Что-то пошло не так';
|
|
} finally {
|
|
loading.value = false;
|
|
}
|
|
}
|
|
|
|
function reset() {
|
|
success.value = false;
|
|
error.value = null;
|
|
aiReply.value = null;
|
|
}
|
|
|
|
return { loading, error, success, aiReply, submit, reset };
|
|
}
|