Fixed ann removal
This commit is contained in:
parent
e214ea53e7
commit
cf81e3d817
@ -5,7 +5,11 @@ const composeRemoveAnnouncementURL = () => (
|
||||
API_URL + '/announcement?'
|
||||
)
|
||||
|
||||
const processRemoveAnnouncement = (data: RemoveAnnouncementResponse): RemoveAnnouncement => {
|
||||
function processRemoveAnnouncement(data: RemoveAnnouncementResponse): RemoveAnnouncement {
|
||||
if (!data.Answer) {
|
||||
throw new Error('Не удалось закрыть объявление')
|
||||
}
|
||||
|
||||
return data.Answer
|
||||
}
|
||||
|
||||
|
@ -11,7 +11,8 @@ import { useId } from '../hooks'
|
||||
|
||||
type AnnouncementDetailsProps = {
|
||||
close: () => void,
|
||||
announcement: Announcement
|
||||
refresh: () => void,
|
||||
announcement: Announcement,
|
||||
}
|
||||
|
||||
const styles = {
|
||||
@ -26,11 +27,17 @@ const styles = {
|
||||
} as CSSProperties,
|
||||
}
|
||||
|
||||
function AnnouncementDetails({ close, announcement: {
|
||||
function AnnouncementDetails({ close, refresh, announcement: {
|
||||
id, name, category, bestBy, description, lat, lng, address, metro, bookedBy, userId
|
||||
} }: AnnouncementDetailsProps) {
|
||||
const { handleBook, bookButton } = useBook()
|
||||
const { handleRemove, removeButton } = useRemoveAnnouncement(close)
|
||||
|
||||
const removeRefresh = () => {
|
||||
close()
|
||||
refresh()
|
||||
}
|
||||
|
||||
const { handleRemove, removeButton } = useRemoveAnnouncement(removeRefresh)
|
||||
|
||||
const myId = useId()
|
||||
|
||||
|
@ -1,12 +1,13 @@
|
||||
import { useCallback } from 'react'
|
||||
|
||||
import { useSendWithButton } from '..'
|
||||
import { composeRemoveAnnouncementURL, processRemoveAnnouncement } from '../../api/removeAnnouncement'
|
||||
import { isRemoveAnnouncementResponse } from '../../api/removeAnnouncement/types'
|
||||
|
||||
const useRemoveAnnouncement = (close: () => void) => {
|
||||
const useRemoveAnnouncement = (resolve: () => void) => {
|
||||
const { doSend, button } = useSendWithButton(
|
||||
'Удалить',
|
||||
'Удалено',
|
||||
'Закрыть',
|
||||
'Закрыто',
|
||||
true,
|
||||
composeRemoveAnnouncementURL(),
|
||||
'DELETE',
|
||||
@ -16,14 +17,19 @@ const useRemoveAnnouncement = (close: () => void) => {
|
||||
)
|
||||
|
||||
const doSendWithClose = useCallback(async (id: number) => {
|
||||
await doSend({}, {
|
||||
const res = await doSend({}, {
|
||||
body: JSON.stringify({
|
||||
id
|
||||
})
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
})
|
||||
|
||||
close()
|
||||
}, [doSend, close])
|
||||
if (res) {
|
||||
resolve()
|
||||
}
|
||||
}, [doSend, resolve])
|
||||
|
||||
return { handleRemove: doSendWithClose, removeButton: button }
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ const useUser = (): UseFetchReturn<User> => (
|
||||
data: initialUser,
|
||||
loading: false,
|
||||
error: null,
|
||||
setData: () => {0}
|
||||
refetch: () => { return }
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -1,27 +1,26 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
|
||||
import { SetState } from '../utils/types'
|
||||
import useSend from './useSend'
|
||||
|
||||
type UseFetchShared<T> = {
|
||||
type UseFetchShared = {
|
||||
loading: boolean,
|
||||
abort?: () => void,
|
||||
setData: SetState<T | undefined>
|
||||
refetch: () => void,
|
||||
}
|
||||
|
||||
type UseFetchSucced<T> = {
|
||||
error: null,
|
||||
data: T,
|
||||
} & UseFetchShared<T>
|
||||
} & UseFetchShared
|
||||
|
||||
type UseFetchErrored<T> = {
|
||||
type UseFetchErrored = {
|
||||
error: string,
|
||||
data: undefined
|
||||
} & UseFetchShared<T>
|
||||
} & UseFetchShared
|
||||
|
||||
type UseFetchReturn<T> = UseFetchSucced<T> | UseFetchErrored<T>
|
||||
type UseFetchReturn<T> = UseFetchSucced<T> | UseFetchErrored
|
||||
|
||||
const gotError = <T>(res: UseFetchReturn<T>): res is UseFetchErrored<T> => (
|
||||
const gotError = <T>(res: UseFetchReturn<T>): res is UseFetchErrored => (
|
||||
typeof res.error === 'string'
|
||||
)
|
||||
|
||||
@ -40,15 +39,25 @@ function useFetch<R, T extends NonNullable<unknown>>(
|
||||
): UseFetchReturn<T> {
|
||||
const [data, setData] = useState(initialData)
|
||||
|
||||
const { doSend, loading, error } = useSend(url, method, needAuth, guardResponse, processResponse, true, params)
|
||||
const { doSend, loading, error } = useSend(
|
||||
url,
|
||||
method,
|
||||
needAuth,
|
||||
guardResponse,
|
||||
processResponse,
|
||||
true,
|
||||
params
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
function refetch() {
|
||||
doSend().then(
|
||||
data => { if (data !== undefined) setData(data) }
|
||||
).catch( // must never occur
|
||||
err => import.meta.env.DEV && console.error('Failed to do fetch request', err)
|
||||
)
|
||||
}, [doSend])
|
||||
}
|
||||
|
||||
useEffect(refetch, [doSend])
|
||||
|
||||
return {
|
||||
...(
|
||||
@ -57,7 +66,7 @@ function useFetch<R, T extends NonNullable<unknown>>(
|
||||
}) : ({ data: undefined, error })
|
||||
),
|
||||
loading,
|
||||
setData
|
||||
refetch
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -12,13 +12,13 @@ import { useStoryIndex } from '../hooks'
|
||||
|
||||
import puffSpinner from '../assets/puff.svg'
|
||||
|
||||
function generateStories(announcements: Announcement[]): Story[] {
|
||||
function generateStories(announcements: Announcement[], refresh: () => void): Story[] {
|
||||
return announcements.map(announcement => {
|
||||
return ({
|
||||
id: announcement.id,
|
||||
url: announcement.src || categoryGraphics[announcement.category],
|
||||
type: announcement.src?.endsWith('mp4') ? 'video' : undefined,
|
||||
seeMore: ({ close }: { close: () => void }) => <AnnouncementDetails close={close} announcement={announcement} />
|
||||
seeMore: ({ close }: { close: () => void }) => <AnnouncementDetails close={close} refresh={refresh} announcement={announcement} />
|
||||
})
|
||||
})
|
||||
}
|
||||
@ -30,7 +30,7 @@ function fallbackGenerateStories(announcements: UseFetchReturn<Announcement[]>)
|
||||
if (gotError(announcements))
|
||||
return fallbackStory(announcements.error, true)
|
||||
|
||||
const stories = generateStories(announcements.data)
|
||||
const stories = generateStories(announcements.data, announcements.refetch)
|
||||
|
||||
if (stories.length === 0)
|
||||
return fallbackStory('Здесь пока пусто')
|
||||
|
Loading…
x
Reference in New Issue
Block a user