/** * External dependencies */ import { Button } from '@wordpress/components'; import { useRegistry } from '@wordpress/data'; import { useCallback, useState } from '@wordpress/element'; /** * Internal dependencies */ import { markAsSpamAction, markAsNotSpamAction, moveToTrashAction, restoreAction, deleteAction, markAsReadAction, markAsUnreadAction, } from '../../inbox/dataviews/actions'; /** * Types */ import type { FormResponse } from '../../../types'; type ResponseNavigationProps = { onActionComplete?: ( FormResponse ) => void; response: FormResponse; }; const ResponseActions = ( { onActionComplete, response, }: ResponseNavigationProps ): JSX.Element => { const [ isMarkingAsSpam, setIsMarkingAsSpam ] = useState( false ); const [ isMarkingAsNotSpam, setIsMarkingAsNotSpam ] = useState( false ); const [ isMovingToTrash, setIsMovingToTrash ] = useState( false ); const [ isRestoring, setIsRestoring ] = useState( false ); const [ isDeleting, setIsDeleting ] = useState( false ); const [ isTogglingReadStatus, setIsTogglingReadStatus ] = useState( false ); const registry = useRegistry(); const handleMarkAsSpam = useCallback( async () => { onActionComplete?.( response ); setIsMarkingAsSpam( true ); await markAsSpamAction.callback( [ response ], { registry } ); setIsMarkingAsSpam( false ); }, [ response, registry, onActionComplete ] ); const handleMarkAsNotSpam = useCallback( async () => { onActionComplete?.( response ); setIsMarkingAsNotSpam( true ); await markAsNotSpamAction.callback( [ response ], { registry } ); setIsMarkingAsNotSpam( false ); }, [ response, registry, onActionComplete ] ); const handleMoveToTrash = useCallback( async () => { onActionComplete?.( response ); setIsMovingToTrash( true ); await moveToTrashAction.callback( [ response ], { registry } ); setIsMovingToTrash( false ); }, [ response, registry, onActionComplete ] ); const handleRestore = useCallback( async () => { onActionComplete?.( response ); setIsRestoring( true ); await restoreAction.callback( [ response ], { registry } ); setIsRestoring( false ); }, [ response, registry, onActionComplete ] ); const handleDelete = useCallback( async () => { onActionComplete?.( response ); setIsDeleting( true ); await deleteAction.callback( [ response ], { registry } ); setIsDeleting( false ); }, [ response, registry, onActionComplete ] ); const handleMarkAsRead = useCallback( async () => { setIsTogglingReadStatus( true ); await markAsReadAction.callback( [ response ], { registry } ); setIsTogglingReadStatus( false ); onActionComplete?.( { ...response, is_unread: false } ); }, [ response, registry, onActionComplete ] ); const handleMarkAsUnread = useCallback( async () => { setIsTogglingReadStatus( true ); await markAsUnreadAction.callback( [ response ], { registry } ); setIsTogglingReadStatus( false ); onActionComplete?.( { ...response, is_unread: true } ); }, [ response, registry, onActionComplete ] ); const readUnreadButtons = ( <> { response.is_unread && ( ) } { ! response.is_unread && ( ) } ); switch ( response.status ) { case 'spam': return (
{ readUnreadButtons }
); case 'trash': return (
{ readUnreadButtons }
); default: // 'publish' (inbox) or any other status return (
{ readUnreadButtons }
); } }; export default ResponseActions;