Separated useSendWithButton hook from useAddAnnouncement

This commit is contained in:
Dmitriy Shishkov 2023-07-27 16:55:07 +03:00
parent 8220b43e9b
commit 2a229c96ba
Signed by: dm1sh
GPG Key ID: 027994B0AA357688
3 changed files with 34 additions and 21 deletions

View File

@ -1,31 +1,18 @@
import { useCallback } from 'react' import { useSendWithButton } from '..'
import { useSend } from '..'
import { composePutAnnouncementURL, processPutAnnouncement } from '../../api/putAnnouncement' import { composePutAnnouncementURL, processPutAnnouncement } from '../../api/putAnnouncement'
import { isPutAnnouncementResponse } from '../../api/putAnnouncement/types' import { isPutAnnouncementResponse } from '../../api/putAnnouncement/types'
import useSendButtonCaption from '../useSendButtonCaption'
const useAddAnnouncement = () => { const useAddAnnouncement = () => (
const { doSend, loading, error } = useSend( useSendWithButton(
'Опубликовать',
'Опубликовано',
true,
composePutAnnouncementURL(), composePutAnnouncementURL(),
'PUT', 'PUT',
true, true,
isPutAnnouncementResponse, 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 export default useAddAnnouncement

View File

@ -3,3 +3,4 @@ export { default as useSend } from './useSend'
export { default as useFetch } from './useFetch' export { default as useFetch } from './useFetch'
export { default as useStoryIndex } from './useStoryIndex' export { default as useStoryIndex } from './useStoryIndex'
export { default as useFilters } from './useFilters' export { default as useFilters } from './useFilters'
export { default as useSendWithButton } from './useSendWithButton'

View File

@ -0,0 +1,25 @@
import { useCallback } from 'react'
import { useSend } from '.'
import useSendButtonCaption from './useSendButtonCaption'
function useSendWithButton<R, T extends NonNullable<unknown>>(
initial: string,
result: string,
singular?: boolean,
...useSendArgs: Parameters<typeof useSend<R, T>>
) {
const { doSend, loading, error } = useSend(...useSendArgs)
const { update, ...button } = useSendButtonCaption(initial, loading, error, result, singular)
const doSendWithButton = useCallback(async (params: Parameters<typeof doSend>[1]) => {
const data = await doSend({}, params)
update(data)
}, [doSend, update])
return { doSend: doSendWithButton, button }
}
export default useSendWithButton