Moved useBook to useSend API
This commit is contained in:
parent
2a229c96ba
commit
3bb6809454
12
front/src/api/book/index.ts
Normal file
12
front/src/api/book/index.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { API_URL } from '../../config'
|
||||||
|
import { Book, BookResponse } from './types'
|
||||||
|
|
||||||
|
const composeBookURL = () => (
|
||||||
|
API_URL + '/book?'
|
||||||
|
)
|
||||||
|
|
||||||
|
const processBook = (data: BookResponse): Book => {
|
||||||
|
return data.Success
|
||||||
|
}
|
||||||
|
|
||||||
|
export { composeBookURL, processBook }
|
17
front/src/api/book/types.ts
Normal file
17
front/src/api/book/types.ts
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import { isObject } from '../../utils/types'
|
||||||
|
|
||||||
|
type BookResponse = {
|
||||||
|
Success: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
const isBookResponse = (obj: unknown): obj is BookResponse => (
|
||||||
|
isObject(obj, {
|
||||||
|
'Success': 'boolean'
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
type Book = boolean
|
||||||
|
|
||||||
|
export type { BookResponse, Book }
|
||||||
|
|
||||||
|
export { isBookResponse }
|
@ -21,8 +21,10 @@ const styles = {
|
|||||||
} as CSSProperties,
|
} as CSSProperties,
|
||||||
}
|
}
|
||||||
|
|
||||||
function AnnouncementDetails({ close, announcement: { id, name, category, bestBy, description, lat, lng, address, metro } }: AnnouncementDetailsProps) {
|
function AnnouncementDetails({ close, announcement: {
|
||||||
const { handleBook, status: bookStatus } = useBook(id)
|
id, name, category, bestBy, description, lat, lng, address, metro
|
||||||
|
} }: AnnouncementDetailsProps) {
|
||||||
|
const { handleBook, bookButton } = useBook()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
@ -62,9 +64,7 @@ function AnnouncementDetails({ close, announcement: { id, name, category, bestBy
|
|||||||
</Modal.Body>
|
</Modal.Body>
|
||||||
|
|
||||||
<Modal.Footer>
|
<Modal.Footer>
|
||||||
<Button variant='success' onClick={() => void handleBook()}>
|
<Button variant='success' onClick={() => void handleBook(id)} {...bookButton} />
|
||||||
{bookStatus || 'Забронировать'}
|
|
||||||
</Button>
|
|
||||||
</Modal.Footer>
|
</Modal.Footer>
|
||||||
</Modal.Dialog>
|
</Modal.Dialog>
|
||||||
</div>
|
</div>
|
||||||
|
@ -1,10 +1,7 @@
|
|||||||
import { useState } from 'react'
|
import { useCallback } from 'react'
|
||||||
import { useNavigate } from 'react-router-dom'
|
|
||||||
|
|
||||||
import { getToken } from '../../utils/auth'
|
|
||||||
import { API_URL } from '../../config'
|
|
||||||
import { isObject } from '../../utils/types'
|
import { isObject } from '../../utils/types'
|
||||||
import { handleHTTPErrors } from '../../utils'
|
import { useSendWithButton } from '..'
|
||||||
|
import { composeBookURL, processBook } from '../../api/book'
|
||||||
|
|
||||||
type BookResponse = {
|
type BookResponse = {
|
||||||
Success: boolean
|
Success: boolean
|
||||||
@ -16,59 +13,29 @@ const isBookResponse = (obj: unknown): obj is BookResponse => (
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
type BookStatus = '' | 'Загрузка...' | 'Забронировано' | 'Ошибка бронирования'
|
function useBook() {
|
||||||
|
const { doSend, button } = useSendWithButton('Забронировать',
|
||||||
|
'Забронировано',
|
||||||
|
true,
|
||||||
|
composeBookURL(),
|
||||||
|
'POST',
|
||||||
|
true,
|
||||||
|
isBookResponse,
|
||||||
|
processBook
|
||||||
|
)
|
||||||
|
|
||||||
function useBook(id: number) {
|
const handleBook = useCallback((id: number) => {
|
||||||
const navigate = useNavigate()
|
void doSend({
|
||||||
|
body: JSON.stringify({
|
||||||
const [status, setStatus] = useState<BookStatus>('')
|
id
|
||||||
|
}),
|
||||||
const handleBook = async () => {
|
headers: {
|
||||||
const token = getToken()
|
'Content-Type': 'application/json'
|
||||||
|
|
||||||
if (token) {
|
|
||||||
setStatus('Загрузка...')
|
|
||||||
|
|
||||||
try {
|
|
||||||
|
|
||||||
const res = await fetch(API_URL + '/book', {
|
|
||||||
method: 'POST',
|
|
||||||
body: JSON.stringify({
|
|
||||||
id: id
|
|
||||||
}),
|
|
||||||
headers: {
|
|
||||||
'Authorization': 'Bearer ' + token,
|
|
||||||
'Content-Type': 'application/json'
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
handleHTTPErrors(res)
|
|
||||||
|
|
||||||
const data: unknown = await res.json()
|
|
||||||
|
|
||||||
if (!isBookResponse(data)) {
|
|
||||||
throw new Error('Malformed server response')
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data.Success === true) {
|
|
||||||
setStatus('Забронировано')
|
|
||||||
} else {
|
|
||||||
throw new Error('Server refused to book')
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (err) {
|
})
|
||||||
setStatus('Ошибка бронирования')
|
}, [doSend])
|
||||||
|
|
||||||
if (import.meta.env.DEV) {
|
return { handleBook, bookButton: button }
|
||||||
console.log(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return navigate('/login')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return { handleBook, status }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default useBook
|
export default useBook
|
||||||
|
Loading…
x
Reference in New Issue
Block a user