Added announcement removal for published by user
This commit is contained in:
parent
9688f56c43
commit
d93b2e131c
12
front/src/api/removeAnnouncement/index.ts
Normal file
12
front/src/api/removeAnnouncement/index.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { API_URL } from '../../config'
|
||||||
|
import { RemoveAnnouncement, RemoveAnnouncementResponse } from './types'
|
||||||
|
|
||||||
|
const composeRemoveAnnouncementURL = () => (
|
||||||
|
API_URL + '/announcement?'
|
||||||
|
)
|
||||||
|
|
||||||
|
const processRemoveAnnouncement = (data: RemoveAnnouncementResponse): RemoveAnnouncement => {
|
||||||
|
return data.Answer
|
||||||
|
}
|
||||||
|
|
||||||
|
export { composeRemoveAnnouncementURL, processRemoveAnnouncement }
|
17
front/src/api/removeAnnouncement/types.ts
Normal file
17
front/src/api/removeAnnouncement/types.ts
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import { isObject } from '../../utils/types'
|
||||||
|
|
||||||
|
type RemoveAnnouncementResponse = {
|
||||||
|
Answer: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
const isRemoveAnnouncementResponse = (obj: unknown): obj is RemoveAnnouncementResponse => (
|
||||||
|
isObject(obj, {
|
||||||
|
'Answer': 'boolean'
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
type RemoveAnnouncement = boolean
|
||||||
|
|
||||||
|
export type { RemoveAnnouncementResponse, RemoveAnnouncement }
|
||||||
|
|
||||||
|
export { isRemoveAnnouncementResponse }
|
@ -3,10 +3,11 @@ import { MapContainer, Marker, Popup, TileLayer } from 'react-leaflet'
|
|||||||
|
|
||||||
import LineDot from './LineDot'
|
import LineDot from './LineDot'
|
||||||
import { categoryNames } from '../assets/category'
|
import { categoryNames } from '../assets/category'
|
||||||
import { useBook } from '../hooks/api'
|
import { useBook, useRemoveAnnouncement } from '../hooks/api'
|
||||||
import { Announcement } from '../api/announcement/types'
|
import { Announcement } from '../api/announcement/types'
|
||||||
import { iconItem } from '../utils/markerIcons'
|
import { iconItem } from '../utils/markerIcons'
|
||||||
import { CSSProperties } from 'react'
|
import { CSSProperties } from 'react'
|
||||||
|
import { useId } from '../hooks'
|
||||||
|
|
||||||
type AnnouncementDetailsProps = {
|
type AnnouncementDetailsProps = {
|
||||||
close: () => void,
|
close: () => void,
|
||||||
@ -19,12 +20,19 @@ const styles = {
|
|||||||
alignItems: 'center',
|
alignItems: 'center',
|
||||||
justifyContent: 'center',
|
justifyContent: 'center',
|
||||||
} as CSSProperties,
|
} as CSSProperties,
|
||||||
|
map: {
|
||||||
|
width: '100%',
|
||||||
|
minHeight: 300,
|
||||||
|
} as CSSProperties,
|
||||||
}
|
}
|
||||||
|
|
||||||
function AnnouncementDetails({ close, announcement: {
|
function AnnouncementDetails({ close, announcement: {
|
||||||
id, name, category, bestBy, description, lat, lng, address, metro
|
id, name, category, bestBy, description, lat, lng, address, metro, bookedBy, userId
|
||||||
} }: AnnouncementDetailsProps) {
|
} }: AnnouncementDetailsProps) {
|
||||||
const { handleBook, bookButton } = useBook()
|
const { handleBook, bookButton } = useBook()
|
||||||
|
const { handleRemove, removeButton } = useRemoveAnnouncement(close)
|
||||||
|
|
||||||
|
const myId = useId()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
@ -47,7 +55,7 @@ function AnnouncementDetails({ close, announcement: {
|
|||||||
|
|
||||||
<p className='mb-3'>{description}</p>
|
<p className='mb-3'>{description}</p>
|
||||||
|
|
||||||
<MapContainer style={{ width: '100%', minHeight: 300 }} center={[lat, lng]} zoom={16} >
|
<MapContainer style={styles.map} center={[lat, lng]} zoom={16} >
|
||||||
<TileLayer
|
<TileLayer
|
||||||
attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
|
attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
|
||||||
url='https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'
|
url='https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'
|
||||||
@ -64,7 +72,12 @@ function AnnouncementDetails({ close, announcement: {
|
|||||||
</Modal.Body>
|
</Modal.Body>
|
||||||
|
|
||||||
<Modal.Footer>
|
<Modal.Footer>
|
||||||
<Button variant='success' onClick={() => void handleBook(id)} {...bookButton} />
|
<p>Забронировали {bookedBy} чел.</p>
|
||||||
|
{(myId === userId) ? (
|
||||||
|
<Button variant='success' onClick={() => void handleRemove(id)} {...removeButton} />
|
||||||
|
) : (
|
||||||
|
<Button variant='success' onClick={() => void handleBook(id)} {...bookButton} />
|
||||||
|
)}
|
||||||
</Modal.Footer>
|
</Modal.Footer>
|
||||||
</Modal.Dialog>
|
</Modal.Dialog>
|
||||||
</div>
|
</div>
|
||||||
|
@ -5,3 +5,4 @@ export { default as useTrashboxes } from './useTrashboxes'
|
|||||||
export { default as useAddAnnouncement } from './useAddAnnouncement'
|
export { default as useAddAnnouncement } from './useAddAnnouncement'
|
||||||
export { default as useOsmAddresses } from './useOsmAddress'
|
export { default as useOsmAddresses } from './useOsmAddress'
|
||||||
export { default as useUser } from './useUser'
|
export { default as useUser } from './useUser'
|
||||||
|
export { default as useRemoveAnnouncement } from './useRemoveAnnouncement'
|
||||||
|
31
front/src/hooks/api/useRemoveAnnouncement.ts
Normal file
31
front/src/hooks/api/useRemoveAnnouncement.ts
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
import { useCallback } from 'react'
|
||||||
|
import { useSendWithButton } from '..'
|
||||||
|
import { composeRemoveAnnouncementURL, processRemoveAnnouncement } from '../../api/removeAnnouncement'
|
||||||
|
import { isRemoveAnnouncementResponse } from '../../api/removeAnnouncement/types'
|
||||||
|
|
||||||
|
const useRemoveAnnouncement = (close: () => void) => {
|
||||||
|
const { doSend, button } = useSendWithButton(
|
||||||
|
'Удалить',
|
||||||
|
'Удалено',
|
||||||
|
true,
|
||||||
|
composeRemoveAnnouncementURL(),
|
||||||
|
'DELETE',
|
||||||
|
true,
|
||||||
|
isRemoveAnnouncementResponse,
|
||||||
|
processRemoveAnnouncement
|
||||||
|
)
|
||||||
|
|
||||||
|
const doSendWithClose = useCallback(async (id: number) => {
|
||||||
|
await doSend({
|
||||||
|
body: JSON.stringify({
|
||||||
|
id
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
close()
|
||||||
|
}, [doSend, close])
|
||||||
|
|
||||||
|
return { handleRemove: doSendWithClose, removeButton: button }
|
||||||
|
}
|
||||||
|
|
||||||
|
export default useRemoveAnnouncement
|
Loading…
x
Reference in New Issue
Block a user