frontend route handlers

This commit is contained in:
2025-05-29 12:08:38 +03:00
parent 403c7e4153
commit 6987560a37
3 changed files with 162 additions and 34 deletions

View File

@@ -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>