|
1 |
| -import React from 'react'; |
| 1 | +import React, { useState } from 'react'; |
2 | 2 | import { classNames } from '../../../../utils/classNames';
|
3 |
| -import { Alert, Button, Heading } from '@navikt/ds-react'; |
| 3 | +import { Alert, Button, Heading, Pagination } from '@navikt/ds-react'; |
4 | 4 | import { SearchResultHit } from './search-hit/SearchResultHit';
|
5 | 5 | import { ContentLoader } from '../../../common/loader/ContentLoader';
|
6 | 6 | import { useSearchState } from '../../../../context/search-state/useSearchState';
|
7 | 7 |
|
8 | 8 | import style from './SearchResult.module.css';
|
9 | 9 |
|
10 | 10 | export const SearchResult = () => {
|
11 |
| - const { searchResult, setSearchResultIsOpen } = useSearchState(); |
| 11 | + const { searchResult, setSearchResultIsOpen, searchParams, runSearch } = useSearchState(); |
| 12 | + |
12 | 13 | const { hits, total, status, params } = searchResult;
|
| 14 | + const { size, from } = params; |
| 15 | + |
| 16 | + const numPages = Math.ceil(total / size); |
| 17 | + const currentPage = Math.floor(from / size) + 1; |
13 | 18 |
|
14 | 19 | return (
|
15 |
| - <div className={classNames(style.result)}> |
16 |
| - {status === 'loading' ? ( |
17 |
| - <ContentLoader size={'3xlarge'} text={'Laster søketreff...'} /> |
18 |
| - ) : status === 'error' ? ( |
19 |
| - <Alert variant={'error'}> |
20 |
| - { |
21 |
| - 'Feil: søket kunne ikke utføres. Prøv igjen, eller kontakt brukerstøtte dersom problemet vedvarer.' |
22 |
| - } |
23 |
| - </Alert> |
24 |
| - ) : status === 'success' ? ( |
25 |
| - <> |
26 |
| - <div className={style.header}> |
27 |
| - <Heading |
28 |
| - size={'small'} |
29 |
| - level={'2'} |
30 |
| - >{`Treff for "${params.query}" (${total})`}</Heading> |
31 |
| - <Button |
32 |
| - size={'xsmall'} |
33 |
| - variant={'tertiary'} |
34 |
| - onClick={() => setSearchResultIsOpen(false)} |
35 |
| - > |
36 |
| - {'Lukk'} |
37 |
| - </Button> |
38 |
| - </div> |
39 |
| - {hits.map((hit) => ( |
40 |
| - <SearchResultHit hit={hit} key={hit.versionKey} /> |
41 |
| - ))} |
42 |
| - ) |
43 |
| - </> |
44 |
| - ) : null} |
45 |
| - </div> |
| 20 | + <> |
| 21 | + <div className={classNames(style.result)}> |
| 22 | + {status === 'loading' ? ( |
| 23 | + <ContentLoader size={'3xlarge'} text={'Laster søketreff...'} /> |
| 24 | + ) : status === 'error' ? ( |
| 25 | + <Alert variant={'error'}> |
| 26 | + { |
| 27 | + 'Feil: søket kunne ikke utføres. Prøv igjen, eller kontakt brukerstøtte dersom problemet vedvarer.' |
| 28 | + } |
| 29 | + </Alert> |
| 30 | + ) : status === 'success' ? ( |
| 31 | + <> |
| 32 | + <div className={style.header}> |
| 33 | + <Heading |
| 34 | + size={'small'} |
| 35 | + level={'2'} |
| 36 | + >{`Treff for "${params.query}" (${total}):`}</Heading> |
| 37 | + <Button |
| 38 | + size={'xsmall'} |
| 39 | + variant={'tertiary'} |
| 40 | + onClick={() => setSearchResultIsOpen(false)} |
| 41 | + > |
| 42 | + {'Lukk'} |
| 43 | + </Button> |
| 44 | + </div> |
| 45 | + {hits.map((hit) => ( |
| 46 | + <SearchResultHit hit={hit} key={hit.versionKey} /> |
| 47 | + ))} |
| 48 | + </> |
| 49 | + ) : null} |
| 50 | + </div> |
| 51 | + {numPages > 1 && ( |
| 52 | + <Pagination |
| 53 | + page={currentPage} |
| 54 | + onPageChange={(page) => runSearch({ ...searchParams, from: size * (page - 1) })} |
| 55 | + count={numPages} |
| 56 | + size={'xsmall'} |
| 57 | + className={style.paginator} |
| 58 | + /> |
| 59 | + )} |
| 60 | + </> |
46 | 61 | );
|
47 | 62 | };
|
0 commit comments