diff --git a/front/src/hooks/api/useAddAnnouncement.ts b/front/src/hooks/api/useAddAnnouncement.ts index 5c60488..ab46a30 100644 --- a/front/src/hooks/api/useAddAnnouncement.ts +++ b/front/src/hooks/api/useAddAnnouncement.ts @@ -1,31 +1,18 @@ -import { useCallback } from 'react' - -import { useSend } from '..' +import { useSendWithButton } from '..' import { composePutAnnouncementURL, processPutAnnouncement } from '../../api/putAnnouncement' import { isPutAnnouncementResponse } from '../../api/putAnnouncement/types' -import useSendButtonCaption from '../useSendButtonCaption' -const useAddAnnouncement = () => { - const { doSend, loading, error } = useSend( +const useAddAnnouncement = () => ( + useSendWithButton( + 'Опубликовать', + 'Опубликовано', + true, composePutAnnouncementURL(), 'PUT', true, isPutAnnouncementResponse, - processPutAnnouncement, + processPutAnnouncement ) - - const { update, ...button } = useSendButtonCaption('Опубликовать', loading, error, 'Опубликовано') - - const doSendWithButton = useCallback(async (formData: FormData) => { - const data = await doSend({}, { - body: formData - }) - update(data) - - return data - }, [doSend, update]) - - return { doSend: doSendWithButton, button } -} +) export default useAddAnnouncement diff --git a/front/src/hooks/index.ts b/front/src/hooks/index.ts index dd1b5ff..5a757f7 100644 --- a/front/src/hooks/index.ts +++ b/front/src/hooks/index.ts @@ -3,3 +3,4 @@ export { default as useSend } from './useSend' export { default as useFetch } from './useFetch' export { default as useStoryIndex } from './useStoryIndex' export { default as useFilters } from './useFilters' +export { default as useSendWithButton } from './useSendWithButton' diff --git a/front/src/hooks/useSendWithButton.ts b/front/src/hooks/useSendWithButton.ts new file mode 100644 index 0000000..5115422 --- /dev/null +++ b/front/src/hooks/useSendWithButton.ts @@ -0,0 +1,25 @@ +import { useCallback } from 'react' +import { useSend } from '.' +import useSendButtonCaption from './useSendButtonCaption' + +function useSendWithButton>( + initial: string, + result: string, + singular?: boolean, + ...useSendArgs: Parameters> +) { + const { doSend, loading, error } = useSend(...useSendArgs) + + const { update, ...button } = useSendButtonCaption(initial, loading, error, result, singular) + + const doSendWithButton = useCallback(async (params: Parameters[1]) => { + const data = await doSend({}, params) + + update(data) + + }, [doSend, update]) + + return { doSend: doSendWithButton, button } +} + +export default useSendWithButton