Skip to content

Commit

Permalink
Fixed the bookmarked offers page (#2652)
Browse files Browse the repository at this point in the history
* fixed the bookmarked offers page

* fixed tests
  • Loading branch information
PavloDolia authored Nov 12, 2024
1 parent e2ed728 commit dd0388e
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const styles = {
justifyContent: 'flex-start',
alignItems: 'center',
gap: '10px',
scrollMarginTop: '16px',
[theme.breakpoints.up('md')]: {
flexDirection: 'row',
justifyContent: 'space-between',
Expand Down
133 changes: 69 additions & 64 deletions src/containers/bookmarked-offers/BookmarksToolbar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FormEvent, useState } from 'react'
import { FormEvent, forwardRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { Box, IconButton } from '@mui/material'
import ClearIcon from '@mui/icons-material/Clear'
Expand Down Expand Up @@ -26,75 +26,80 @@ interface BookmarksToolbarProps {
offersView: CardsView
}

const BookmarksToolbar = ({
filters,
updateFilters,
additionalParams,
offersView,
handleOffersView
}: BookmarksToolbarProps) => {
const [title, setTitle] = useState(filters.title)
const { t } = useTranslation()
const { isLaptopAndAbove } = useBreakpoints()
const BookmarksToolbar = forwardRef<HTMLDivElement, BookmarksToolbarProps>(
(
{ filters, updateFilters, additionalParams, offersView, handleOffersView },
ref
) => {
const [title, setTitle] = useState(filters.title)
const { t } = useTranslation()
const { isLaptopAndAbove } = useBreakpoints()

const handleInputSubmit = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault()
updateFilters({ ...additionalParams, title })
}
const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setTitle(event.target.value)
}

const handleSortBy = (value: string) => {
updateFilters({ ...additionalParams, sort: value })
}
const handleInputSubmit = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault()
updateFilters({ ...additionalParams, title })
}

const sortOptions = sortTranslationKeys.map(({ title, value }) => ({
title: t(title),
value
}))
const handleSortBy = (value: string) => {
updateFilters({ ...additionalParams, sort: value })
}

const inputProps = {
endAdornment: filters.title ? (
<IconButton
data-testid='clear-button'
onClick={() => {
setTitle('')
updateFilters({ ...additionalParams, title: '' })
}}
sx={{ p: 0 }}
>
<ClearIcon color='secondary' />
</IconButton>
) : (
<SearchIcon color='primary' />
)
}
const sortOptions = sortTranslationKeys.map(({ title, value }) => ({
title: t(title),
value
}))

const inputProps = {
endAdornment: filters.title ? (
<IconButton
data-testid='clear-button'
onClick={() => {
setTitle('')
updateFilters({ ...additionalParams, title: '' })
}}
sx={{ p: 0 }}
>
<ClearIcon color='secondary' />
</IconButton>
) : (
<SearchIcon color='primary' />
)
}

return (
<Box sx={styles.container}>
<Box sx={styles.searchBox}>
<form onSubmit={handleInputSubmit}>
<FilterInput
InputProps={inputProps}
label={t('bookmarkedOffers.search')}
onChange={setTitle}
size={SizeEnum.Medium}
sx={styles.filterInput}
value={title}
return (
<Box ref={ref} sx={styles.container}>
<Box sx={styles.searchBox}>
<form onSubmit={handleInputSubmit}>
<FilterInput
InputProps={inputProps}
label={t('bookmarkedOffers.search')}
onChange={handleInputChange}
size={SizeEnum.Medium}
sx={styles.filterInput}
value={title}
/>
</form>
</Box>
<Box sx={styles.selectContainer}>
<AppSelect
fields={sortOptions}
selectTitle={t('filters.sortBy.sortByTitle')}
setValue={handleSortBy}
value={filters.sort}
/>
</form>
{isLaptopAndAbove && (
<ViewSwitcher onChange={handleOffersView} value={offersView} />
)}
</Box>
</Box>
<Box sx={styles.selectContainer}>
<AppSelect
fields={sortOptions}
selectTitle={t('filters.sortBy.sortByTitle')}
setValue={handleSortBy}
value={filters.sort}
/>
{isLaptopAndAbove && (
<ViewSwitcher onChange={handleOffersView} value={offersView} />
)}
</Box>
</Box>
)
}
)
}
)

BookmarksToolbar.displayName = 'BookmarksToolbar'

export default BookmarksToolbar
5 changes: 4 additions & 1 deletion src/pages/bookmarked-offers/BookmarkedOffers.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChangeEvent, useCallback, useEffect, useState } from 'react'
import { ChangeEvent, useCallback, useEffect, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { Divider } from '@mui/material'
import Typography from '@mui/material/Typography'
Expand Down Expand Up @@ -45,6 +45,7 @@ const BookmarkedOffers = () => {
const { bookmarkedOffers } = useAppSelector((state) => state.editProfile)
const { isMobile } = useBreakpoints()
const dispatch = useAppDispatch()
const toolbarRef = useRef<HTMLDivElement>(null)
const { t } = useTranslation()

const { items, count: offersCount } = offers
Expand Down Expand Up @@ -100,6 +101,7 @@ const BookmarkedOffers = () => {

const handlePageChange = (_: ChangeEvent<unknown>, page: number) => {
filterQueryActions.updateFiltersInQuery({ page })
toolbarRef.current?.scrollIntoView({ block: 'start', behavior: 'smooth' })
}

useEffect(() => {
Expand All @@ -116,6 +118,7 @@ const BookmarkedOffers = () => {
filters={filters}
handleOffersView={setCardsView}
offersView={cardsView}
ref={toolbarRef}
updateFilters={filterQueryActions.updateFiltersInQuery}
/>
<Divider sx={styles.divider} />
Expand Down
6 changes: 5 additions & 1 deletion tests/unit/pages/bookmarked-offers/BookmarkedOffers.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ import * as sortValues from '~/containers/find-offer/offer-filter-block/OfferFil
const mockNavigate = vi.fn()
let mockSearchParams = new URLSearchParams()
const mockSetSearchParams = vi.fn()
const mockScrollIntoView = vi.fn()
const mockT = (str) => str

window.HTMLElement.prototype.scrollIntoView = mockScrollIntoView

vi.mock('react-router-dom', async () => {
const actual = await vi.importActual('react-router-dom')
return {
Expand Down Expand Up @@ -77,7 +80,7 @@ describe('BookmarkedOffers page with offers', () => {
expect(offer2Title).toBeInTheDocument()
})

it('should change the page number', async () => {
it('should change the page number and scroll to the top', async () => {
const pageNumber = 2
const goToPageBtn = await screen.findByText(`${pageNumber}`, {
selector: 'button'
Expand All @@ -88,6 +91,7 @@ describe('BookmarkedOffers page with offers', () => {
expect(mockSetSearchParams).toHaveBeenCalledWith(
new URLSearchParams({ page: pageNumber })
)
expect(mockScrollIntoView).toHaveBeenCalled()
})

it('should add title to URL search params', async () => {
Expand Down

0 comments on commit dd0388e

Please sign in to comment.