forked from polka_billy/porridger
Updated details page, implemented booking
This commit is contained in:
42
front/src/hooks/api/useBook.js
Normal file
42
front/src/hooks/api/useBook.js
Normal file
@ -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
|
Reference in New Issue
Block a user