| 1 |
/** |
| 2 |
* Internal depenencies. |
| 3 |
*/ |
| 4 |
import { useContext, useEffect, useRef, WPElement } from '@wordpress/element'; |
| 5 |
import { _n, sprintf } from '@wordpress/i18n'; |
| 6 |
|
| 7 |
/** |
| 8 |
* Internal dependencies. |
| 9 |
*/ |
| 10 |
import Context from '../../context'; |
| 11 |
import Pagination from '../results/pagination'; |
| 12 |
import Result from '../results/result'; |
| 13 |
import Sort from '../tools/sort'; |
| 14 |
|
| 15 |
/** |
| 16 |
* Search results component. |
| 17 |
* |
| 18 |
* @returns {WPElement} Component element. |
| 19 |
*/ |
| 20 |
export default () => { |
| 21 |
const { |
| 22 |
state: { |
| 23 |
args: { offset, per_page }, |
| 24 |
searchResults, |
| 25 |
searchedTerm, |
| 26 |
totalResults, |
| 27 |
}, |
| 28 |
dispatch, |
| 29 |
} = useContext(Context); |
| 30 |
|
| 31 |
const headingRef = useRef(); |
| 32 |
|
| 33 |
/** |
| 34 |
* Handle clicking next. |
| 35 |
*/ |
| 36 |
const onNext = () => { |
| 37 |
dispatch({ type: 'NEXT_PAGE' }); |
| 38 |
}; |
| 39 |
|
| 40 |
/** |
| 41 |
* Handle clicking previous. |
| 42 |
*/ |
| 43 |
const onPrevious = () => { |
| 44 |
dispatch({ type: 'PREVIOUS_PAGE' }); |
| 45 |
}; |
| 46 |
|
| 47 |
/** |
| 48 |
* Effects. |
| 49 |
*/ |
| 50 |
useEffect(() => { |
| 51 |
headingRef.current.scrollIntoView({ behavior: 'smooth' }); |
| 52 |
}, [offset]); |
| 53 |
|
| 54 |
return ( |
| 55 |
<div className="ep-search-results"> |
| 56 |
<header className="ep-search-results__header"> |
| 57 |
<h1 className="ep-search-results__title" ref={headingRef} role="status"> |
| 58 |
{searchedTerm |
| 59 |
? sprintf( |
| 60 |
/* translators: %1$d: results count. %2$s: Search term. */ |
| 61 |
_n( |
| 62 |
'%1$d result for ā%2$sā', |
| 63 |
'%1$d results for ā%2$sā', |
| 64 |
totalResults, |
| 65 |
'elasticpress', |
| 66 |
), |
| 67 |
totalResults, |
| 68 |
searchedTerm, |
| 69 |
) |
| 70 |
: sprintf( |
| 71 |
/* translators: %d: results count. */ |
| 72 |
_n('%d result', '%d results', totalResults, 'elasticpress'), |
| 73 |
totalResults, |
| 74 |
)} |
| 75 |
</h1> |
| 76 |
|
| 77 |
<Sort /> |
| 78 |
</header> |
| 79 |
|
| 80 |
{searchResults.map((hit) => ( |
| 81 |
<Result key={hit._id} hit={hit} /> |
| 82 |
))} |
| 83 |
|
| 84 |
<Pagination |
| 85 |
offset={offset} |
| 86 |
onNext={onNext} |
| 87 |
onPrevious={onPrevious} |
| 88 |
perPage={per_page} |
| 89 |
total={totalResults} |
| 90 |
/> |
| 91 |
</div> |
| 92 |
); |
| 93 |
}; |
| 94 |
|