Skip to content

Commit

Permalink
Merge pull request stakwork#16 from MirzaHanan/bounty-page-go-back-home
Browse files Browse the repository at this point in the history
Fix(Bounty-Page): Enhanced Navigation for Direct Access to Bounty Pages
  • Loading branch information
elraphty authored Jan 25, 2024
2 parents ddcd600 + e395c7d commit 7eb977f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/pages/tickets/TicketModalPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const focusedDesktopModalStyles = widgetConfigs.wanted.modalStyle;

type Props = {
setConnectPerson: (p: any) => void;
visible?: boolean;
};

export const TicketModalPage = observer(({ setConnectPerson }: Props) => {
Expand All @@ -28,7 +29,7 @@ export const TicketModalPage = observer(({ setConnectPerson }: Props) => {
const [activeListIndex, setActiveListIndex] = useState<number>(0);
const [publicFocusIndex, setPublicFocusIndex] = useState(0);
const [removeNextAndPrev, setRemoveNextAndPrev] = useState(false);
const { bountyId } = useParams<{ uuid: string; bountyId: string }>();
const { uuid, bountyId } = useParams<{ uuid: string; bountyId: string }>();
const [activeBounty, setActiveBounty] = useState<PersonBounty[]>([]);
const [visible, setVisible] = useState(false);
const [isDeleted, setisDeleted] = useState(false);
Expand Down Expand Up @@ -73,10 +74,21 @@ export const TicketModalPage = observer(({ setConnectPerson }: Props) => {
getBounty();
}, [getBounty, removeNextAndPrev]);

const goBack = async () => {
const isDirectAccess = useCallback(
() => !document.referrer && location.pathname.includes('/bounty/'),
[location.pathname]
);

const goBack = () => {
setVisible(false);
setisDeleted(false);
history.goBack();

if (isDirectAccess()) {
const homePageUrl = uuid ? `/org/bounties/${uuid}` : '/bounties';
history.push(homePageUrl);
} else {
history.goBack();
}
};

const directionHandler = (person: any, body: any) => {
Expand Down
58 changes: 58 additions & 0 deletions src/pages/tickets/__tests__/TicketModalPage.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import React from 'react';
import { render, fireEvent, screen, waitFor } from '@testing-library/react';
import { useIsMobile } from 'hooks';
import { useStores } from 'store';
import { TicketModalPage } from '../TicketModalPage.tsx';

jest.mock('hooks', () => ({
useIsMobile: jest.fn()
}));

jest.mock('store', () => ({
useStores: jest.fn()
}));

const mockPush = jest.fn();
const mockGoBack = jest.fn();

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useHistory: () => ({
push: mockPush,
goBack: mockGoBack
}),
useLocation: () => ({
pathname: '/bounty/1239',
search: '',
state: {}
}),
useParams: () => ({
uuid: 'ck95pe04nncjnaefo08g',
bountyId: '1239'
})
}));

describe('TicketModalPage Component', () => {
it('should redirect to the appropriate page on close based on the route', async () => {
(useIsMobile as jest.Mock).mockReturnValue(false);

(useStores as jest.Mock).mockReturnValue({
main: {
getBountyById: jest.fn(),
getBountyIndexById: jest.fn()
}
});

render(<TicketModalPage setConnectPerson={jest.fn()} visible={true} />);

// eslint-disable-next-line @typescript-eslint/no-empty-function
await waitFor(() => {});

const closeButton = screen.queryByTestId('close-btn');
if (closeButton) {
fireEvent.click(closeButton);

expect(mockPush).toHaveBeenCalledWith('/bounties');
}
});
});

0 comments on commit 7eb977f

Please sign in to comment.