Separated useSendWithButton hook from useAddAnnouncement
This commit is contained in:
parent
8220b43e9b
commit
2a229c96ba
@ -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
|
||||||
|
@ -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'
|
||||||
|
25
front/src/hooks/useSendWithButton.ts
Normal file
25
front/src/hooks/useSendWithButton.ts
Normal 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
|
Loading…
x
Reference in New Issue
Block a user