47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
import { Lead } from '@/app/types'
|
|
|
|
export const sendLead = async (data: Lead) => {
|
|
const API_URL = process.env.NEXT_PUBLIC_API_URL
|
|
|
|
const headers: Record<string, string> = {
|
|
'Content-Type': 'application/json',
|
|
Accept: 'application/json',
|
|
}
|
|
|
|
try {
|
|
const body = JSON.stringify({
|
|
name: data.name,
|
|
phone_number: data.phone_number,
|
|
email: data.email,
|
|
moving_price: data.moving_price,
|
|
moving_date: data.moving_date,
|
|
comment: data.comment,
|
|
})
|
|
|
|
const response = await fetch(`${API_URL}/account/send_lead/`, {
|
|
method: 'POST',
|
|
headers,
|
|
body,
|
|
})
|
|
|
|
if (!response.ok) {
|
|
let errorMessage = `Failed to send lead data: ${response.status} ${response.statusText}`
|
|
try {
|
|
const errorData = await response.text()
|
|
|
|
if (errorData) {
|
|
errorMessage += ` - ${errorData}`
|
|
}
|
|
} catch (e) {
|
|
console.error('Error parsing error response:', e)
|
|
}
|
|
throw new Error(errorMessage)
|
|
}
|
|
const text = await response.text()
|
|
return text ? JSON.parse(text) : null
|
|
} catch (error) {
|
|
console.error('Error sending lead data:', error)
|
|
throw error
|
|
}
|
|
}
|