diff --git a/front/src/components/AnnouncementDetails.jsx b/front/src/components/AnnouncementDetails.jsx index 0a84453..7685e35 100644 --- a/front/src/components/AnnouncementDetails.jsx +++ b/front/src/components/AnnouncementDetails.jsx @@ -1,19 +1,18 @@ -import Modal from 'react-bootstrap/Modal' +import { Modal, Button } from 'react-bootstrap' +import { MapContainer, Marker, Popup, TileLayer } from 'react-leaflet' import { categoryNames } from '../assets/category' -import { MapContainer, Marker, Popup, TileLayer } from 'react-leaflet' -import { Button } from 'react-bootstrap' -import useBook from '../utils/useBook' +import { useBook } from '../hooks/api' function AnnouncementDetails({ close, announcement: { id, name, category, bestBy, description, lat, lng, address, metro } }) { - const handleBook = useBook(id) - + const {handleBook, status: bookStatus} = useBook(id) + return (
- + Подробнее @@ -24,10 +23,10 @@ function AnnouncementDetails({ close, announcement: { id, name, category, bestBy

{name}

{categoryNames.get(category)} - {/* dot */} + {/* dot */} Годен до {new Date(bestBy).toLocaleString('ru-RU')} - -

{description}

+ +

{description}

diff --git a/front/src/hooks/api/useBook.js b/front/src/hooks/api/useBook.js new file mode 100644 index 0000000..e7dc9e5 --- /dev/null +++ b/front/src/hooks/api/useBook.js @@ -0,0 +1,42 @@ +import { useState } from "react" +import { useNavigate } from "react-router-dom" + +import { getToken } from "../../utils/auth" +import { API_URL } from "../../config" + +function useBook(id) { + const navigate = useNavigate() + + const [status, setStatus] = useState('') + + const handleBook = () => { + const token = getToken() || "Test" + + if (token) { + setStatus("Загрузка") + + fetch(API_URL + '/book', { + method: 'POST', + body: JSON.stringify({ + id: id + }), + headers: { + 'Authorization': 'Bearer ' + token, + 'Content-Type': 'application/json' + } + }).then(res => res.json()).then(data => { + if (data.Success === true) { + setStatus('Забронировано') + } else { + setStatus("Ошибка бронирования") + } + }) + } else { + return navigate("/login") + } + } + + return { handleBook, status } +} + +export default useBook diff --git a/front/src/utils/auth.js b/front/src/utils/auth.js index ce9a497..88d8dcf 100644 --- a/front/src/utils/auth.js +++ b/front/src/utils/auth.js @@ -6,4 +6,8 @@ const getToken = () => { return token } -export { getToken } \ No newline at end of file +const setToken = (token) => { + localStorage.setItem("Token", token) +} + +export { getToken, setToken } \ No newline at end of file diff --git a/front/src/utils/useBook.js b/front/src/utils/useBook.js deleted file mode 100644 index 1bcddbb..0000000 --- a/front/src/utils/useBook.js +++ /dev/null @@ -1,18 +0,0 @@ -import { useNavigate } from "react-router-dom" -import { getToken } from "./auth" - -function useBook(id) { - const navigate = useNavigate() - - const handleBook = () => { - /* TODO */ - - if (!getToken()) { - return navigate("/login") - } - } - - return handleBook -} - -export default useBook