2020-07-03 00:02:49 +02:00
|
|
|
import React from 'react';
|
2020-07-02 12:13:51 +02:00
|
|
|
|
|
|
|
import { makeStyles } from '@material-ui/core/styles';
|
|
|
|
import Paper from '@material-ui/core/Paper';
|
|
|
|
import Typography from '@material-ui/core/Typography';
|
|
|
|
import TextField from '@material-ui/core/TextField';
|
|
|
|
import Container from '@material-ui/core/Container';
|
2020-07-02 23:55:56 +02:00
|
|
|
import Pagination from '@material-ui/lab/Pagination';
|
2020-07-02 12:13:51 +02:00
|
|
|
|
|
|
|
import { useSearch } from './utils/useSearch';
|
2020-07-02 23:55:56 +02:00
|
|
|
import {
|
|
|
|
useLocationSearch,
|
|
|
|
useLocationSearchState
|
|
|
|
} from './utils/routing';
|
2020-07-02 12:13:51 +02:00
|
|
|
import { SearchHit } from './SearchHit';
|
|
|
|
|
|
|
|
|
|
|
|
const classes = makeStyles((theme) => ({
|
|
|
|
root: {
|
|
|
|
display: 'flex',
|
|
|
|
flexWrap: 'wrap',
|
|
|
|
},
|
|
|
|
textField: {
|
|
|
|
marginLeft: theme.spacing(1),
|
|
|
|
marginRight: theme.spacing(1),
|
|
|
|
width: '25ch',
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
|
|
|
export const SearchPage = () => {
|
2020-07-02 23:55:56 +02:00
|
|
|
const pageSize = 20;
|
|
|
|
const [query, setQuery] = useLocationSearchState('query', '');
|
|
|
|
const [pageNumber, setPageNumber] = useLocationSearchState('page', 1, parseInt);
|
2020-07-03 00:02:49 +02:00
|
|
|
const [, setLocationSearch] = useLocationSearch();
|
2020-07-02 23:55:56 +02:00
|
|
|
|
2020-07-02 12:13:51 +02:00
|
|
|
|
2020-07-02 23:55:56 +02:00
|
|
|
const [results, numberOfHits, error, resultsAreLoading] = useSearch(query, pageSize, pageNumber);
|
|
|
|
const totalPages = Math.ceil(numberOfHits/pageSize);
|
2020-07-02 12:13:51 +02:00
|
|
|
|
|
|
|
let resultsDisplay="No rule found...";
|
|
|
|
if (resultsAreLoading) {
|
|
|
|
resultsDisplay = "Searching";
|
|
|
|
}
|
|
|
|
else if (results.length > 0) {
|
|
|
|
resultsDisplay = results.map(result => <SearchHit key={result.id} data={result}/>)
|
|
|
|
}
|
2020-07-02 23:55:56 +02:00
|
|
|
|
|
|
|
function handleQueryUpdate(event) {
|
|
|
|
if (pageNumber > 1) {
|
|
|
|
setLocationSearch({query: event.target.value, page: 1});
|
|
|
|
} else {
|
|
|
|
setQuery(event.target.value, {push: false});
|
|
|
|
}
|
|
|
|
}
|
2020-07-02 12:13:51 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<Paper className={classes.languagesBar}>
|
|
|
|
<Container maxWidth="md">
|
|
|
|
<Typography variant="h4" className={classes.searchBar}>Search Rule Specifications</Typography>
|
|
|
|
<TextField
|
|
|
|
id="title-query"
|
2020-07-06 17:41:13 +02:00
|
|
|
label="Rule Title and Description"
|
2020-07-02 12:13:51 +02:00
|
|
|
style={{ margin: 8 }}
|
2020-07-06 17:41:13 +02:00
|
|
|
placeholder="Search in rule titles and descriptions"
|
2020-07-02 12:13:51 +02:00
|
|
|
fullWidth
|
|
|
|
margin="normal"
|
|
|
|
InputLabelProps={{
|
|
|
|
shrink: true,
|
|
|
|
}}
|
|
|
|
variant="outlined"
|
2020-07-02 23:55:56 +02:00
|
|
|
value={query}
|
|
|
|
onChange={handleQueryUpdate}
|
|
|
|
error={error}
|
|
|
|
helperText={error}
|
2020-07-02 12:13:51 +02:00
|
|
|
/>
|
|
|
|
</Container>
|
|
|
|
</Paper>
|
2020-07-02 23:55:56 +02:00
|
|
|
<Typography variant="h5" className={classes.searchBar}>Number of rules found: {numberOfHits}</Typography>
|
2020-07-02 12:13:51 +02:00
|
|
|
<ul>
|
|
|
|
{resultsDisplay}
|
|
|
|
</ul>
|
2020-07-02 23:55:56 +02:00
|
|
|
<Pagination count={totalPages} page={pageNumber} siblingCount={2}
|
|
|
|
onChange={(event, value) => setPageNumber(value)}
|
|
|
|
/>
|
|
|
|
<Paper/>
|
2020-07-02 12:13:51 +02:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|