init
This commit is contained in:
45
frontend/app/composables/useContact.ts
Normal file
45
frontend/app/composables/useContact.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
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 };
|
||||
}
|
||||
Reference in New Issue
Block a user