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