From cf81e3d817ac84bfc64e592e15af60fb20236a10 Mon Sep 17 00:00:00 2001 From: dm1sh Date: Sat, 29 Jul 2023 18:43:44 +0300 Subject: [PATCH] Fixed ann removal --- front/src/api/removeAnnouncement/index.ts | 6 +++- front/src/components/AnnouncementDetails.tsx | 13 ++++++-- front/src/hooks/api/useRemoveAnnouncement.ts | 20 +++++++----- front/src/hooks/api/useUser.ts | 2 +- front/src/hooks/useFetch.ts | 33 +++++++++++++------- front/src/pages/HomePage.tsx | 6 ++-- 6 files changed, 53 insertions(+), 27 deletions(-) diff --git a/front/src/api/removeAnnouncement/index.ts b/front/src/api/removeAnnouncement/index.ts index fa88c16..dd52f7a 100644 --- a/front/src/api/removeAnnouncement/index.ts +++ b/front/src/api/removeAnnouncement/index.ts @@ -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 } diff --git a/front/src/components/AnnouncementDetails.tsx b/front/src/components/AnnouncementDetails.tsx index 4a20086..164f755 100644 --- a/front/src/components/AnnouncementDetails.tsx +++ b/front/src/components/AnnouncementDetails.tsx @@ -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() diff --git a/front/src/hooks/api/useRemoveAnnouncement.ts b/front/src/hooks/api/useRemoveAnnouncement.ts index ca8271f..0f47e09 100644 --- a/front/src/hooks/api/useRemoveAnnouncement.ts +++ b/front/src/hooks/api/useRemoveAnnouncement.ts @@ -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 } } diff --git a/front/src/hooks/api/useUser.ts b/front/src/hooks/api/useUser.ts index ca22c1a..93bff07 100644 --- a/front/src/hooks/api/useUser.ts +++ b/front/src/hooks/api/useUser.ts @@ -16,7 +16,7 @@ const useUser = (): UseFetchReturn => ( data: initialUser, loading: false, error: null, - setData: () => {0} + refetch: () => { return } } ) diff --git a/front/src/hooks/useFetch.ts b/front/src/hooks/useFetch.ts index e20a86e..d000e74 100644 --- a/front/src/hooks/useFetch.ts +++ b/front/src/hooks/useFetch.ts @@ -1,27 +1,26 @@ import { useEffect, useState } from 'react' -import { SetState } from '../utils/types' import useSend from './useSend' -type UseFetchShared = { +type UseFetchShared = { loading: boolean, abort?: () => void, - setData: SetState + refetch: () => void, } type UseFetchSucced = { error: null, data: T, -} & UseFetchShared +} & UseFetchShared -type UseFetchErrored = { +type UseFetchErrored = { error: string, data: undefined -} & UseFetchShared +} & UseFetchShared -type UseFetchReturn = UseFetchSucced | UseFetchErrored +type UseFetchReturn = UseFetchSucced | UseFetchErrored -const gotError = (res: UseFetchReturn): res is UseFetchErrored => ( +const gotError = (res: UseFetchReturn): res is UseFetchErrored => ( typeof res.error === 'string' ) @@ -40,15 +39,25 @@ function useFetch>( ): UseFetchReturn { 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>( }) : ({ data: undefined, error }) ), loading, - setData + refetch } } diff --git a/front/src/pages/HomePage.tsx b/front/src/pages/HomePage.tsx index 14afa98..48162a9 100644 --- a/front/src/pages/HomePage.tsx +++ b/front/src/pages/HomePage.tsx @@ -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 }) => + seeMore: ({ close }: { close: () => void }) => }) }) } @@ -30,7 +30,7 @@ function fallbackGenerateStories(announcements: UseFetchReturn) 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('Здесь пока пусто')