Skip to content

Commit

Permalink
fix: Fix multiple browser history push when navigating and using sear…
Browse files Browse the repository at this point in the history
…ch bar
  • Loading branch information
mbacherycz committed Nov 8, 2024
1 parent 30ed67c commit eb7c0f8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
20 changes: 12 additions & 8 deletions packages/frontend/src/components/Header/SearchBar.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { MagnifyingGlassIcon, MultiplyIcon } from '@navikt/aksel-icons';
import cx from 'classnames';
import { useState } from 'react';
import { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useNavigate, useSearchParams } from 'react-router-dom';
import { useSearchParams } from 'react-router-dom';
import { useWindowSize } from '../../../utils/useWindowSize.tsx';
import { Backdrop } from '../Backdrop';
import { useSearchString } from '../Header';
Expand All @@ -15,7 +15,6 @@ export const SearchBar: React.FC = () => {
const [searchParams, setSearchParams] = useSearchParams();
const { searchString, setSearchString } = useSearchString();
const [searchValue, setSearchValue] = useState<string>(searchString);
const navigate = useNavigate();
const { isTabletOrSmaller } = useWindowSize();

const handleClose = () => {
Expand All @@ -42,13 +41,18 @@ export const SearchBar: React.FC = () => {
setSearchString(value);
setShowDropdownMenu(false);
newSearchParams.set('search', value);
setSearchParams(newSearchParams);
navigate({
pathname: '/',
search: `?${newSearchParams.toString()}`,
});
setSearchParams(newSearchParams, { replace: true });
};

useEffect(() => {
const searchBarParam = new URLSearchParams(searchParams);
if (searchBarParam.get('search')) {
return;
}
setSearchValue('');
searchBarParam.delete('search');
}, [location.search]);

return (
<div className={styles.searchbarContainer}>
<div className={cx(styles.inputContainer, { [styles.searchbarOpen]: showDropdownMenu })}>
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/src/pages/Inbox/Inbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export const Inbox = ({ viewType }: InboxProps) => {
useEffect(() => {
const newSearchParams = new URLSearchParams(searchParams);
newSearchParams.set('sortBy', selectedSortOrder);
setSearchParams(newSearchParams);
setSearchParams(newSearchParams, { replace: true });
}, [selectedSortOrder]);

const shouldShowSearchResults = !isFetchingSearchResults && showingSearchResults;
Expand Down
27 changes: 14 additions & 13 deletions packages/frontend/tests/stories/loginPartyContext.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,22 @@ test.describe('LoginPartyContext', () => {
await expect(page.getByRole('link', { name: 'Melding om bortkjøring av snø' })).not.toBeVisible();
});

test('Go-back button updates state and shows correct data and color theme', async ({ page }: { page: Page }) => {
await expect(page.getByRole('button', { name: 'Test Testesen' })).toBeVisible();
await expect(page.getByRole('link', { name: 'Skatten din for 2022' })).toBeVisible();
await expect(page.getByRole('link', { name: 'Innkalling til sesjon' })).not.toBeVisible();
test('Go-back button deletes search bar value', async ({ page }: { page: Page }) => {
await page.getByPlaceholder('Søk i innboks').click();
await expect(page.getByPlaceholder('Søk i innboks')).toBeVisible();
await page.getByPlaceholder('Søk i innboks').fill('skatten din');
await page.getByPlaceholder('Søk i innboks').press('Enter');

await page.getByRole('button', { name: 'Test Testesen' }).click();
await page.getByText('Alle virksomheter').click();
await expect(page.getByRole('link', { name: 'Skatten din for 2022' })).not.toBeVisible();
await expect(page.getByRole('link', { name: 'Innkalling til sesjon' })).toBeVisible();
await expect(page.getByTestId('pageLayout-background')).toHaveClass(/.*isCompany.*/);
let searchParams = new URL(page.url()).searchParams;
expect(searchParams.has('search')).toBe(true);
expect(searchParams.get('search')).toBe('skatten din');
await expect(page.getByRole('link', { name: 'Skatten din for 2022' })).toBeVisible();
await expect(page.getByRole('link', { name: 'Melding om bortkjøring av snø' })).not.toBeVisible();

await page.goBack();
await expect(page.getByRole('button', { name: 'Test Testesen' })).toBeVisible();
await expect(page.getByRole('link', { name: 'Skatten din for 2022' })).toBeVisible();
await expect(page.getByRole('link', { name: 'Innkalling til sesjon' })).not.toBeVisible();
await expect(page.getByTestId('pageLayout-background')).not.toHaveClass(/.*isCompany.*/);
searchParams = new URL(page.url()).searchParams;
expect(searchParams.has('search')).toBe(false);
await expect(page.getByPlaceholder('Søk i innboks')).toBeEmpty();
await expect(page.getByRole('link', { name: 'Melding om bortkjøring av snø' })).toBeVisible();
});
});

0 comments on commit eb7c0f8

Please sign in to comment.