sender page

This commit is contained in:
2025-05-22 13:28:20 +03:00
parent 5ba9121f33
commit 4182708db7
8 changed files with 409 additions and 72 deletions

View File

@@ -1,12 +1,12 @@
'use client'
import React from 'react'
import MultiSelect from '@/components/ui/Selector'
import TextInput from '@/components/ui/TextInput'
import PhoneInput from '@/components/ui/PhoneInput'
import Button from '@/components/ui/Button'
import TextAreaInput from '@/components/ui/TextAreaInput'
import CheckboxInput from '@/components/ui/CheckboxInput'
import LocationSelect from '@/components/ui/LocationSelect'
import SingleSelect from '@/components/ui/SingleSelect'
import { useForm } from '@/app/hooks/useForm'
import useUserStore from '@/app/store/userStore'
import showToast from '@/components/ui/Toast'
@@ -29,10 +29,10 @@ const formatDateToHTML = (date: Date) => {
const validationRules = {
transport: { required: true },
country_from: { required: true, minLength: 2 },
city_from: { required: true, minLength: 2 },
country_to: { required: true, minLength: 2 },
city_to: { required: true, minLength: 2 },
country_from_id: { required: true },
city_from: { required: true },
country_to_id: { required: true },
city_to: { required: true },
cargo_type: { required: true },
departure: {
required: true,
@@ -61,6 +61,8 @@ const SenderPage = () => {
city_from: '',
country_to: '',
city_to: '',
country_from_id: '',
country_to_id: '',
cargo_type: '',
departure: '',
arrival: '',
@@ -86,15 +88,26 @@ const SenderPage = () => {
validationRules,
async values => {
try {
// await addNewSpecialist(values, selectedImage || undefined)
showToast({
type: 'success',
message: 'Маршрут успешно создан!',
const response = await fetch('/api/account/sender', {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(values),
})
} catch {
if (!response.ok) {
const error = await response.json()
throw new Error(error.error || 'Ошибка при обновлении данных')
}
const result = await response.json()
setUser(result.user)
showToast({ type: 'success', message: 'Данные успешно обновлены!' })
} catch (error) {
showToast({
type: 'error',
message: 'Упс, что то пошло не так...',
message: error instanceof Error ? error.message : 'Ой, что то пошло не так..',
})
}
}
@@ -115,45 +128,27 @@ const SenderPage = () => {
{/* тип груза и транспорта */}
<div className="grid grid-cols-1 gap-6 sm:grid-cols-2">
<div>
<label htmlFor="cargo_type" className="block text-sm font-medium text-gray-700">
Тип груза
</label>
<MultiSelect
value={values.cargo_type ? [parseInt(values.cargo_type)] : []}
handleChange={e => {
handleChange({
target: {
id: 'cargo_type',
value: e.target.value[0]?.toString() || '',
},
})
}}
<SingleSelect
name="cargo_type"
value={values.cargo_type}
handleChange={handleChange}
label="Тип груза"
placeholder="Выберите тип груза"
options={cargoOptions}
className="mt-1"
placeholder="Выберите тип груза"
noOptionsMessage="Нет доступных типов груза"
/>
</div>
<div>
<label htmlFor="transport" className="block text-sm font-medium text-gray-700">
Способ перевозки
</label>
<MultiSelect
value={values.transport ? [parseInt(values.transport)] : []}
handleChange={e => {
handleChange({
target: {
id: 'transport',
value: e.target.value[0]?.toString() || '',
},
})
}}
<SingleSelect
name="transport"
value={values.transport}
handleChange={handleChange}
label="Способ перевозки"
placeholder="Выберите способ перевозки"
options={transportOptions}
className="mt-1"
placeholder="Выберите способ перевозки"
noOptionsMessage="Нет доступных способов перевозки"
/>
</div>
@@ -165,41 +160,80 @@ const SenderPage = () => {
<div className="grid grid-cols-1 gap-6 sm:grid-cols-2">
<div className="space-y-6">
<TextInput
<LocationSelect
name="country_from"
value={values.country_from}
handleChange={handleChange}
handleChange={e => {
const selectedOption = e.target.selectedOption
handleChange({
target: {
id: 'country_from',
value: e.target.value,
},
})
handleChange({
target: {
id: 'country_from_id',
value: selectedOption?.id?.toString() || '',
},
})
handleChange({
target: {
id: 'city_from',
value: '',
},
})
}}
label="Страна отправления"
placeholder="Введите страну отправления"
style="register"
placeholder="Выберите страну отправления"
/>
<TextInput
name="country_to"
value={values.country_to}
<LocationSelect
name="city_from"
value={values.city_from}
handleChange={handleChange}
label="Страна назначения"
placeholder="Введите страну назначения"
style="register"
label="Город отправления"
placeholder="Выберите город отправления"
countryId={values.country_from_id}
isCity
/>
</div>
<div className="space-y-6">
<TextInput
<LocationSelect
name="country_to"
value={values.country_to}
handleChange={e => {
const selectedOption = e.target.selectedOption
handleChange({
target: {
id: 'country_to',
value: e.target.value,
},
})
handleChange({
target: {
id: 'country_to_id',
value: selectedOption?.id?.toString() || '',
},
})
handleChange({
target: {
id: 'city_to',
value: '',
},
})
}}
label="Страна назначения"
placeholder="Выберите страну назначения"
/>
<LocationSelect
name="city_to"
value={values.city_to}
handleChange={handleChange}
label="Город назначения"
placeholder="Введите город назначения"
style="register"
/>
<TextInput
name="country_to"
value={values.country_to}
handleChange={handleChange}
label="Страна назначения"
placeholder="Введите страну назначения"
style="register"
placeholder="Выберите город назначения"
countryId={values.country_to_id}
isCity
/>
</div>
</div>

View File

@@ -27,13 +27,14 @@ export async function POST(req: NextRequest) {
email_notification,
} = data
const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/account/create_sender/`, {
const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/account/create_route/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${session.accessToken}`,
},
body: JSON.stringify({
owner_type: 'sender',
transport,
country_from,
city_from,

View File

@@ -192,6 +192,8 @@ export interface SenderPageProps extends Record<string, FormValue> {
city_from: string
country_to: string
city_to: string
country_from_id: string
country_to_id: string
cargo_type: string
departure: string
arrival: string