route handler + backend api

This commit is contained in:
2025-05-22 11:45:25 +03:00
parent 5ad12c34cd
commit 5ba9121f33
7 changed files with 220 additions and 36 deletions

View File

@@ -8,6 +8,7 @@ import Button from '@/components/ui/Button'
import TextAreaInput from '@/components/ui/TextAreaInput'
import CheckboxInput from '@/components/ui/CheckboxInput'
import { useForm } from '@/app/hooks/useForm'
import useUserStore from '@/app/store/userStore'
import showToast from '@/components/ui/Toast'
import { SenderPageProps, SelectOption } from '@/app/types'
import {
@@ -41,7 +42,7 @@ const validationRules = {
required: true,
pattern: /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}$/,
},
contact_number: {
phone_number: {
required: true,
minLength: 11,
pattern: /^\+?[0-9]{11,}$/,
@@ -51,6 +52,7 @@ const validationRules = {
}
const SenderPage = () => {
const { user, setUser } = useUserStore()
const today = formatDateToHTML(new Date())
const initialValues: SenderPageProps = {
@@ -62,7 +64,7 @@ const SenderPage = () => {
cargo_type: '',
departure: '',
arrival: '',
contact_number: '',
phone_number: user?.phone_number || '',
comment: '',
email_notification: false,
}
@@ -240,7 +242,7 @@ const SenderPage = () => {
<h2 className="mb-2 text-xl font-medium text-gray-900">Контактная информация</h2>
<div className="space-y-6">
<PhoneInput
value={values.contact_number}
value={values.phone_number}
handleChange={handleChange}
label="Контактный телефон"
operatorsInfo={false}

View File

@@ -0,0 +1,63 @@
import { NextRequest } from 'next/server'
import { getServerSession } from 'next-auth'
import { authOptions } from '@/app/api/auth/[...nextauth]/route'
export async function POST(req: NextRequest) {
try {
const session = await getServerSession(authOptions)
if (!session) {
return new Response(JSON.stringify({ error: 'Unauthorized' }), {
status: 401,
})
}
const data = await req.json()
const {
transport,
country_from,
city_from,
country_to,
city_to,
cargo_type,
departure,
arrival,
phone_number,
comment,
email_notification,
} = data
const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/account/create_sender/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${session.accessToken}`,
},
body: JSON.stringify({
transport,
country_from,
city_from,
country_to,
city_to,
cargo_type,
departure,
arrival,
phone_number,
comment,
email_notification,
}),
})
if (!response.ok) {
const error = await response.json()
return new Response(JSON.stringify(error), { status: response.status })
}
const result = await response.json()
return new Response(JSON.stringify(result), { status: 200 })
} catch (error) {
return new Response(JSON.stringify({ error: 'Internal Server Error' }), {
status: 500,
})
}
}

View File

@@ -195,7 +195,7 @@ export interface SenderPageProps extends Record<string, FormValue> {
cargo_type: string
departure: string
arrival: string
contact_number: string
phone_number: string
comment: string
email_notification: boolean
}