Skip to content

Commit 7b4cd5a

Browse files
committed
feat(search): add sort order for saves
1 parent 56ffb92 commit 7b4cd5a

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
lines changed

clients/web/src/containers/search/header.js

+43-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import { breakpointLargeHandset } from 'common/constants'
55
import { useTranslation } from 'next-i18next'
66
import { Loader } from 'components/loader/loader'
77
import Link from 'next/link'
8+
import { savedItemsSetSortOrder } from 'containers/saves/saved-items/saved-items.state'
9+
import { savedItemsSetSortBy } from 'containers/saves/saved-items/saved-items.state'
810
import { SearchFilterMenu } from 'components/list-filter-menu/search-filter-menu'
11+
import { ListSort } from 'components/list-sort/list-sort'
12+
import { useDispatch } from 'react-redux'
913

1014
const searchStyles = css`
1115
margin-bottom: 1.5rem;
@@ -50,7 +54,8 @@ const searchStyles = css`
5054
}
5155
}
5256
.sourceBar {
53-
display: block;
57+
display: flex;
58+
justify-content: space-between;
5459
a {
5560
text-decoration: none;
5661
display: inline-block;
@@ -68,8 +73,22 @@ const searchStyles = css`
6873
}
6974
`
7075

71-
export const SearchPageHeader = ({ total, loading, query, searchType, filter }) => {
76+
export const SearchPageHeader = ({
77+
total,
78+
loading,
79+
query,
80+
searchType,
81+
filter,
82+
sortOrder,
83+
isPremium
84+
}) => {
7285
const { t } = useTranslation()
86+
const dispatch = useDispatch()
87+
88+
const handleNewest = () => dispatch(savedItemsSetSortOrder('DESC'))
89+
const handleOldest = () => dispatch(savedItemsSetSortOrder('ASC'))
90+
const handleRelevance = () => dispatch(savedItemsSetSortBy('RELEVANCE'))
91+
7392
return query ? (
7493
<header className={searchStyles}>
7594
<h1 className="pageTitle" data-testid="page-title">
@@ -87,16 +106,29 @@ export const SearchPageHeader = ({ total, loading, query, searchType, filter })
87106
)}
88107
</h1>
89108
<div className="sourceBar">
90-
<Link className={searchType === 'all' ? 'active' : ''} href={`/search?q=${query}&st=all`}>
91-
All of Pocket
92-
</Link>{' '}
93-
<Link
94-
className={searchType === 'saves' ? 'active' : ''}
95-
href={`/search?q=${query}&st=saves`}>
96-
Only My Saves
97-
</Link>
109+
<div>
110+
<Link className={searchType === 'all' ? 'active' : ''} href={`/search?q=${query}&st=all`}>
111+
All of Pocket
112+
</Link>{' '}
113+
<Link
114+
className={searchType === 'saves' ? 'active' : ''}
115+
href={`/search?q=${query}&st=saves`}>
116+
Only My Saves
117+
</Link>
118+
{searchType === 'saves' ? (
119+
<SearchFilterMenu subset="search" query={query} filter={filter} sortOrder={sortOrder} />
120+
) : null}
121+
</div>
98122
{searchType === 'saves' ? (
99-
<SearchFilterMenu subset="search" query={query} filter={filter} />
123+
<ListSort
124+
query={query}
125+
filter={filter}
126+
showRelevance={isPremium}
127+
sortOrder={sortOrder}
128+
handleNewest={handleNewest}
129+
handleOldest={handleOldest}
130+
handleRelevance={handleRelevance}
131+
/>
100132
) : null}
101133
</div>
102134
</header>

clients/web/src/containers/search/saves/index.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ export const SearchSaves = ({ query, locale, filter }) => {
99
const dispatch = useDispatch()
1010
const total = useSelector((state) => state.pageSavedInfo.totalCount)
1111
const loading = useSelector((state) => state.pageSavedInfo.loading)
12+
const isPremium = useSelector((state) => state.user.premium_status === '1')
13+
14+
// We only pull this in as a trigger for re-searching since some ancient organizational
15+
// need made it so sort order isn't passed in
16+
const sortOrder = useSelector((state) => state.pageSavedInfo.sortOrder)
1217

1318
// We want to clear search results when we hit this page
1419
useEffect(() => {
@@ -18,17 +23,19 @@ export const SearchSaves = ({ query, locale, filter }) => {
1823
// Get items based on location
1924
useEffect(() => {
2025
const itemFilterFunction = filterSelector('search', filter)
21-
dispatch(itemFilterFunction(query, 'DESC'))
22-
}, [dispatch, query, locale, filter])
26+
dispatch(itemFilterFunction(query, sortOrder))
27+
}, [dispatch, query, locale, filter, sortOrder])
2328

2429
return (
2530
<>
2631
<SearchPageHeader
2732
query={query}
2833
total={total}
2934
loading={loading}
35+
isPremium={isPremium}
3036
searchType="saves"
3137
filter={filter}
38+
sortOrder={sortOrder}
3239
/>
3340
<ListOfSavedItems query={query} locale={locale} />
3441
</>

clients/web/src/pages/search/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ export const getServerSideProps: GetServerSideProps<LocalizedProps> = async ({
2929
// Light validation
3030
const searchType = typeof st === 'string' && validSearchTypes.includes(st) ? st : false
3131
const filter = typeof ft === 'string' && validFilters.includes(ft) ? ft : false
32-
3332
return { props: { ...defaultProps, query: q, searchType, filter } }
3433
}
3534

0 commit comments

Comments
 (0)