mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
fix(pending-post): preserve sparse retry routes
This commit is contained in:
@@ -16,6 +16,7 @@ const testState = vi.hoisted(() => ({
|
||||
lastPublishOptions: undefined as Record<string, any> | undefined,
|
||||
navigateMock: vi.fn(),
|
||||
publishCommentMock: vi.fn(async () => undefined),
|
||||
publishIndex: undefined as number | undefined,
|
||||
}));
|
||||
|
||||
vi.mock('react-router-dom', () => ({
|
||||
@@ -28,6 +29,7 @@ vi.mock('@bitsocial/bitsocial-react-hooks', () => ({
|
||||
testState.lastPublishOptions = options;
|
||||
return {
|
||||
abandonPublish: testState.abandonPublishMock,
|
||||
index: testState.publishIndex,
|
||||
publishComment: testState.publishCommentMock,
|
||||
};
|
||||
},
|
||||
@@ -79,10 +81,19 @@ const renderHook = (post = failedPost, deleteRedirectPath?: string) => {
|
||||
});
|
||||
};
|
||||
|
||||
const flushEffects = async (count = 4) => {
|
||||
for (let i = 0; i < count; i += 1) {
|
||||
await act(async () => {
|
||||
await Promise.resolve();
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
describe('useDeleteFailedPost', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
testState.lastPublishOptions = undefined;
|
||||
testState.publishIndex = undefined;
|
||||
vi.stubGlobal('alert', testState.alertMock);
|
||||
useChallengesStore.setState({ challenges: [] });
|
||||
|
||||
@@ -142,6 +153,20 @@ describe('useDeleteFailedPost', () => {
|
||||
expect(testState.publishCommentMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('redirects retry publishes to the new pending row', async () => {
|
||||
testState.publishCommentMock.mockImplementationOnce(async () => {
|
||||
testState.publishIndex = 12;
|
||||
});
|
||||
|
||||
await act(async () => {
|
||||
await latestValue.onRetryFailedPost();
|
||||
});
|
||||
renderHook();
|
||||
await flushEffects();
|
||||
|
||||
expect(testState.navigateMock).toHaveBeenCalledWith('/pending/12', { replace: true });
|
||||
});
|
||||
|
||||
it('redirects after deleting a failed post when a redirect path is provided', async () => {
|
||||
renderHook(failedPost, '/mu');
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useCallback, useMemo, useRef, useState } from 'react';
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { ChallengeVerification, Comment, PublishCommentOptions, deleteComment, usePublishComment } from '@bitsocial/bitsocial-react-hooks';
|
||||
import { alertChallengeVerificationFailed } from '../lib/utils/challenge-utils';
|
||||
@@ -71,6 +71,7 @@ export const getFailedPostRetryPublishOptions = (post?: FailedPost): PublishComm
|
||||
const useDeleteFailedPost = (post?: FailedPost, deleteRedirectPath?: string) => {
|
||||
const [isDeletingFailedPost, setIsDeletingFailedPost] = useState(false);
|
||||
const [isRetryingFailedPost, setIsRetryingFailedPost] = useState(false);
|
||||
const [isRetryRedirectPending, setIsRetryRedirectPending] = useState(false);
|
||||
const addChallenge = useChallengesStore((state) => state.addChallenge);
|
||||
const navigate = useNavigate();
|
||||
const abandonPublishRef = useRef<(() => Promise<void>) | undefined>();
|
||||
@@ -93,15 +94,25 @@ const useDeleteFailedPost = (post?: FailedPost, deleteRedirectPath?: string) =>
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
console.error('Failed to retry failed post:', error);
|
||||
setIsRetryRedirectPending(false);
|
||||
alert(`Failed to retry post: ${error.message}`);
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
[abandonCurrentPublish, addChallenge, retryPublishOptions],
|
||||
);
|
||||
const { abandonPublish, publishComment } = usePublishComment(publishOptionsWithCallbacks);
|
||||
const { abandonPublish, index: retryPostIndex, publishComment } = usePublishComment(publishOptionsWithCallbacks);
|
||||
abandonPublishRef.current = abandonPublish;
|
||||
|
||||
useEffect(() => {
|
||||
if (!isRetryRedirectPending || typeof retryPostIndex !== 'number') {
|
||||
return;
|
||||
}
|
||||
|
||||
setIsRetryRedirectPending(false);
|
||||
navigate(`/pending/${retryPostIndex}`, { replace: true });
|
||||
}, [isRetryRedirectPending, navigate, retryPostIndex]);
|
||||
|
||||
const onDeleteFailedPost = useCallback(() => {
|
||||
if (isDeletingFailedPost || isRetryingFailedPost || !canDeleteFailedPost) {
|
||||
return;
|
||||
@@ -140,12 +151,14 @@ const useDeleteFailedPost = (post?: FailedPost, deleteRedirectPath?: string) =>
|
||||
}
|
||||
|
||||
setIsRetryingFailedPost(true);
|
||||
setIsRetryRedirectPending(true);
|
||||
|
||||
try {
|
||||
await deleteComment(targetComment);
|
||||
await publishComment();
|
||||
} catch (error) {
|
||||
console.error('Failed to retry failed post:', error);
|
||||
setIsRetryRedirectPending(false);
|
||||
alert(`Failed to retry post: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
||||
} finally {
|
||||
setIsRetryingFailedPost(false);
|
||||
|
||||
Reference in New Issue
Block a user