generated from scale-tone/react-ts-basic
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathSearchResults.tsx
138 lines (110 loc) · 4.26 KB
/
SearchResults.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import React from 'react';
import { observer } from 'mobx-react';
import styled from 'styled-components';
import { Avatar, Card, Chip, CardHeader, CardContent, Grid, Link, LinearProgress, Typography } from '@material-ui/core';
import FolderIcon from '@material-ui/icons/Folder';
import { SearchResultsState } from '../states/SearchResultsState';
// List of search results
@observer
export class SearchResults extends React.Component<{ state: SearchResultsState, inProgress: boolean }> {
componentDidMount() {
// Doing a simple infinite scroll
document.addEventListener('scroll', (evt) => {
const scrollingElement = (evt.target as Document).scrollingElement;
if (!scrollingElement) {
return;
}
const scrollPos = scrollingElement.scrollHeight - window.innerHeight - scrollingElement.scrollTop;
const scrollPosThreshold = 100;
if (scrollPos < scrollPosThreshold) {
this.props.state.loadMoreResults();
}
});
}
render(): JSX.Element {
const state = this.props.state;
var cards = state.searchResults.map(item => {
return (
<Grid key={item.key} item sm={12} md={6}>
<Card raised>
<CardHeader
avatar={
<Link onClick={() => state.showDetails(item)}>
<Avatar><FolderIcon /></Avatar>
</Link>
}
title={<Link variant="h6" onClick={() => state.showDetails(item)}>{item.name}</Link>}
/>
<CardContent>
{item.otherFields.map(val => { return (
<Typography variant="body2" key={val}>
{val}
</Typography>
)})}
</CardContent>
<TagButtonsDiv>
{item.keywords.map(kw => { return (
<TagChip
key={kw}
label={kw}
size="small"
onClick={() => state.facetsState.filterBy(item.keywordsFieldName, kw)}
disabled={this.props.inProgress}
/>
); })}
</TagButtonsDiv>
</Card>
</Grid>
);
});
return (<>
<div>
<CountersTypography variant="caption" >
{state.searchResults.length} of {state.totalResults} results shown
</CountersTypography>
{!!state.inProgress && (<TopLinearProgress />)}
</div>
<ResultsGrid container spacing={3}>
{!!state.errorMessage && (
<ErrorChip color="secondary" label={state.errorMessage} onDelete={() => state.HideError()}/>
)}
{cards}
</ResultsGrid>
{(!!state.inProgress && !!state.searchResults.length) && (<LinearProgress />)}
</>);
}
}
const ResultsGrid: typeof Grid = styled(Grid)({
paddingRight: 30,
paddingBottom: 20,
// required for Edge :(((
marginLeft: '0px !important',
})
const TagButtonsDiv = styled.div({
marginRight: '15px !important',
marginLeft: '5px !important',
marginTop: '8px !important',
marginBottom: '10px !important',
display: 'flex',
flexWrap: 'wrap'
})
const CountersTypography: typeof Typography = styled(Typography)({
float: 'right',
width: 'auto',
margin: '10px !important'
})
const TopLinearProgress: typeof LinearProgress = styled(LinearProgress)({
top: 20
})
const TagChip: typeof Chip = styled(Chip)({
marginLeft: '10px !important',
marginBottom: '10px !important'
})
const ErrorChip: typeof Chip = styled(Chip)({
paddingTop: 10,
paddingBottom: 10,
paddingLeft: 20,
paddingRight: 20,
marginLeft: 50,
marginRight: 50,
})