backend logic

This commit is contained in:
2025-05-24 14:55:27 +03:00
parent e4fcf0716d
commit b755eda4b5
7 changed files with 160 additions and 31 deletions

View File

@@ -236,3 +236,12 @@ export interface SearchPageProps {
params: Promise<{ category?: string }>
searchParams?: Promise<{ [key: string]: string | string[] | undefined }>
}
export interface Lead {
name: string
phone_number: string
email: string
moving_price: string
moving_date: string
comment: string
}

View File

@@ -25,7 +25,7 @@ interface RouteFormProps {
description: string
}
const formatDateToHTML = (date: Date) => {
export const formatDateToHTML = (date: Date) => {
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')

View File

@@ -7,13 +7,14 @@ import TextInput from '../ui/TextInput'
import TextAreaInput from '../ui/TextAreaInput'
import PhoneInput from '../ui/PhoneInput'
import useUserStore from '@/app/store/userStore'
import { sendLead } from '@/lib/main/sendLead'
const validationRules = {
name: { required: true },
phone_number: { required: true },
email: { required: true },
price: { required: false },
deliveryTime: { required: false },
moving_price: { required: true },
moving_date: { required: true },
comment: { required: false },
}
@@ -26,21 +27,24 @@ interface LeadPopupProps {
const LeadPopup = ({ id, isOpen, onClose, onSuccess }: LeadPopupProps) => {
const { user } = useUserStore()
const today = new Date().toISOString().split('T')[0]
const initialValues = {
name: user?.name || '',
phone_number: user?.phone_number || '',
email: user?.email || '',
moving_price: '',
moving_date: '',
comment: '',
id: id,
}
const { values, handleChange, handleSubmit, setValues } = useForm(
{
name: user?.name || '',
phone_number: user?.phone_number || '',
email: user?.email || '',
price: '',
deliveryTime: '',
comment: '',
id: id,
},
initialValues,
validationRules,
async values => {
try {
// await sendLead(values)
await sendLead(values)
showToast({
type: 'success',
message: 'Сообщение отправлено!',
@@ -98,22 +102,28 @@ const LeadPopup = ({ id, isOpen, onClose, onSuccess }: LeadPopupProps) => {
/>
<TextInput
name="price"
value={values.price}
name="moving_price"
value={values.moving_price}
handleChange={handleChange}
label="Предлагаемая цена"
placeholder="Укажите стоимость перевозки"
style="register"
/>
<TextInput
name="deliveryTime"
value={values.deliveryTime}
handleChange={handleChange}
label="Срок доставки"
placeholder="Укажите предполагаемый срок доставки"
style="register"
/>
<div>
<label htmlFor="moving_date" className="block text-sm font-medium text-gray-700">
Срок доставки
</label>
<input
type="date"
name="moving_date"
id="moving_date"
value={values.moving_date}
onChange={handleChange}
min={today}
className="mt-1 block w-full rounded-xl border border-gray-300 px-3 py-2 focus:border-blue-500 focus:ring-1 focus:ring-blue-500 focus:outline-none"
/>
</div>
<TextAreaInput
name="comment"
value={values.comment}

View File

@@ -0,0 +1,46 @@
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
}
}