From 6987560a378ae7d416fa08d6c7e473201cd507e5 Mon Sep 17 00:00:00 2001 From: Timofey Date: Thu, 29 May 2025 12:08:38 +0300 Subject: [PATCH] frontend route handlers --- frontend/app/(urls)/account/routes/page.tsx | 118 ++++++++++++++------ frontend/app/api/account/highlight/route.ts | 39 +++++++ frontend/app/api/account/upper/route.ts | 39 +++++++ 3 files changed, 162 insertions(+), 34 deletions(-) create mode 100644 frontend/app/api/account/highlight/route.ts create mode 100644 frontend/app/api/account/upper/route.ts diff --git a/frontend/app/(urls)/account/routes/page.tsx b/frontend/app/(urls)/account/routes/page.tsx index 8010445..9065c70 100644 --- a/frontend/app/(urls)/account/routes/page.tsx +++ b/frontend/app/(urls)/account/routes/page.tsx @@ -11,46 +11,96 @@ export default function UserRoutes() { const [error, setError] = useState(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() {