frontend route handlers
This commit is contained in:
@@ -11,46 +11,96 @@ export default function UserRoutes() {
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [loading, setLoading] = useState(true)
|
||||
|
||||
useEffect(() => {
|
||||
const fetchRoutes = async () => {
|
||||
try {
|
||||
const response = await fetch('/api/account/routes', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
})
|
||||
const fetchRoutes = async () => {
|
||||
try {
|
||||
const response = await fetch('/api/account/routes', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json()
|
||||
throw new Error(error.message || 'Failed to fetch routes')
|
||||
}
|
||||
|
||||
const data = await response.json()
|
||||
setRoutes(data || [])
|
||||
} catch (error) {
|
||||
console.error('Error fetching routes:', error)
|
||||
setError(error instanceof Error ? error.message : 'Не удалось загрузить маршруты')
|
||||
} finally {
|
||||
setLoading(false)
|
||||
if (!response.ok) {
|
||||
const error = await response.json()
|
||||
throw new Error(error.message || 'Failed to fetch routes')
|
||||
}
|
||||
}
|
||||
|
||||
const data = await response.json()
|
||||
setRoutes(data || [])
|
||||
} catch (error) {
|
||||
console.error('Error fetching routes:', error)
|
||||
setError(error instanceof Error ? error.message : 'Не удалось загрузить маршруты')
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
fetchRoutes()
|
||||
}, [])
|
||||
|
||||
const handleHighlight = () => {
|
||||
showToast({
|
||||
type: 'success',
|
||||
message: 'Ваше объявление выделено на сутки!',
|
||||
})
|
||||
const handleHighlight = async (route: Route) => {
|
||||
try {
|
||||
const response = await fetch(`/api/account/highlight`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
route_id: route.id,
|
||||
}),
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json()
|
||||
throw new Error(error.message || 'Failed to highlight route')
|
||||
}
|
||||
|
||||
await fetchRoutes()
|
||||
|
||||
showToast({
|
||||
type: 'success',
|
||||
message: 'Ваше объявление выделено на сутки!',
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Error highlighting route:', error)
|
||||
showToast({
|
||||
type: 'error',
|
||||
message: 'Не удалось выделить объявление',
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const handleUpper = () => {
|
||||
showToast({
|
||||
type: 'success',
|
||||
message: 'Ваше объявление поднято в выдаче!',
|
||||
})
|
||||
const handleUpper = async (route: Route) => {
|
||||
try {
|
||||
const response = await fetch(`/api/account/upper`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
route_id: route.id,
|
||||
}),
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json()
|
||||
throw new Error(error.message || 'Failed to upper route')
|
||||
}
|
||||
|
||||
await fetchRoutes()
|
||||
|
||||
showToast({
|
||||
type: 'success',
|
||||
message: 'Ваше объявление поднято в выдаче!',
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Error upper route:', error)
|
||||
showToast({
|
||||
type: 'error',
|
||||
message: 'Не удалось поднять объявление',
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if (loading) {
|
||||
@@ -149,12 +199,12 @@ export default function UserRoutes() {
|
||||
<Button
|
||||
text="Поднять объявление"
|
||||
className="border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 shadow-sm hover:bg-gray-100 focus:outline-none"
|
||||
onClick={handleUpper}
|
||||
onClick={() => handleUpper(route)}
|
||||
/>
|
||||
<Button
|
||||
text="Выделить рамкой"
|
||||
className="border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 shadow-sm hover:bg-gray-100 focus:outline-none"
|
||||
onClick={handleHighlight}
|
||||
onClick={() => handleHighlight(route)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
39
frontend/app/api/account/highlight/route.ts
Normal file
39
frontend/app/api/account/highlight/route.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { NextRequest } from 'next/server'
|
||||
import { getServerSession } from 'next-auth'
|
||||
import { authOptions } from '@/app/api/auth/[...nextauth]/route'
|
||||
|
||||
export async function PATCH(req: NextRequest) {
|
||||
try {
|
||||
const session = await getServerSession(authOptions)
|
||||
|
||||
if (!session) {
|
||||
return new Response(JSON.stringify({ error: 'Unauthorized' }), {
|
||||
status: 401,
|
||||
})
|
||||
}
|
||||
|
||||
const { route_id } = await req.json()
|
||||
|
||||
const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/account/route_highlight/`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${session.accessToken}`,
|
||||
},
|
||||
body: JSON.stringify({ route_id }),
|
||||
})
|
||||
|
||||
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) {
|
||||
console.error('PATCH /api/account/highlight error:', error)
|
||||
return new Response(JSON.stringify({ error: 'Internal Server Error' }), {
|
||||
status: 500,
|
||||
})
|
||||
}
|
||||
}
|
||||
39
frontend/app/api/account/upper/route.ts
Normal file
39
frontend/app/api/account/upper/route.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { NextRequest } from 'next/server'
|
||||
import { getServerSession } from 'next-auth'
|
||||
import { authOptions } from '@/app/api/auth/[...nextauth]/route'
|
||||
|
||||
export async function PATCH(req: NextRequest) {
|
||||
try {
|
||||
const session = await getServerSession(authOptions)
|
||||
|
||||
if (!session) {
|
||||
return new Response(JSON.stringify({ error: 'Unauthorized' }), {
|
||||
status: 401,
|
||||
})
|
||||
}
|
||||
|
||||
const { route_id } = await req.json()
|
||||
|
||||
const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/account/route_up/`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${session.accessToken}`,
|
||||
},
|
||||
body: JSON.stringify({ route_id }),
|
||||
})
|
||||
|
||||
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) {
|
||||
console.error('PATCH /api/account/route_up error:', error)
|
||||
return new Response(JSON.stringify({ error: 'Internal Server Error' }), {
|
||||
status: 500,
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user