|
| 1 | +import React, { useEffect } from 'react' |
| 2 | +import { List, AutoSizer } from 'react-virtualized' |
| 3 | +import { makeStyles } from '@material-ui/core/styles' |
| 4 | +import Card from '@material-ui/core/Card' |
| 5 | +import CardActionArea from '@material-ui/core/CardActionArea' |
| 6 | +import CardContent from '@material-ui/core/CardContent' |
| 7 | +import CardMedia from '@material-ui/core/CardMedia' |
| 8 | +import Typography from '@material-ui/core/Typography' |
| 9 | +import intl from 'react-intl-universal' |
| 10 | +import { Link } from 'react-router-dom' |
| 11 | +import CircularProgress from '@material-ui/core/CircularProgress' |
| 12 | +import purple from '@material-ui/core/colors/purple' |
| 13 | + |
| 14 | +const useStyles = makeStyles(theme => ({ |
| 15 | + root: props => { |
| 16 | + return { |
| 17 | + marginTop: theme.spacing(1), |
| 18 | + maxWidth: 350, |
| 19 | + height: window.innerHeight - props.layoutConfig.topBar.reducedHeight - props.layoutConfig.tabHeight - 139, |
| 20 | + [theme.breakpoints.up(600)]: { |
| 21 | + height: window.innerHeight - props.layoutConfig.topBar.reducedHeight - props.layoutConfig.tabHeight - 256 |
| 22 | + }, |
| 23 | + [theme.breakpoints.up(props.layoutConfig.hundredPercentHeightBreakPoint)]: { |
| 24 | + height: window.innerHeight - props.layoutConfig.topBar.reducedHeight - props.layoutConfig.tabHeight - 178 |
| 25 | + }, |
| 26 | + [theme.breakpoints.up(1100)]: { |
| 27 | + height: window.innerHeight - props.layoutConfig.topBar.reducedHeight - props.layoutConfig.tabHeight - 196 |
| 28 | + }, |
| 29 | + [theme.breakpoints.up(props.layoutConfig.reducedHeightBreakpoint)]: { |
| 30 | + height: window.innerHeight - props.layoutConfig.topBar.reducedHeight - props.layoutConfig.tabHeight - 265 |
| 31 | + }, |
| 32 | + fontFamily: 'Roboto' |
| 33 | + } |
| 34 | + }, |
| 35 | + list: { |
| 36 | + [theme.breakpoints.up('md')]: { |
| 37 | + paddingRight: 4 |
| 38 | + } |
| 39 | + }, |
| 40 | + link: { |
| 41 | + textDecoration: 'none' |
| 42 | + }, |
| 43 | + progressContainer: { |
| 44 | + width: '100%', |
| 45 | + height: 600, |
| 46 | + [theme.breakpoints.up('md')]: { |
| 47 | + height: 'calc(100% - 80px)' |
| 48 | + }, |
| 49 | + display: 'flex', |
| 50 | + alignItems: 'center', |
| 51 | + justifyContent: 'center' |
| 52 | + } |
| 53 | +})) |
| 54 | + |
| 55 | +const ReactVirtualizedList = props => { |
| 56 | + const classes = useStyles(props) |
| 57 | + const { results } = props.perspectiveState |
| 58 | + |
| 59 | + useEffect(() => { |
| 60 | + props.fetchResults({ |
| 61 | + resultClass: props.resultClass, |
| 62 | + facetClass: props.facetClass |
| 63 | + }) |
| 64 | + }, []) |
| 65 | + |
| 66 | + useEffect(() => { |
| 67 | + const { facetUpdateID } = props |
| 68 | + if (facetUpdateID > 0) { |
| 69 | + props.fetchResults({ |
| 70 | + resultClass: props.resultClass, |
| 71 | + facetClass: props.facetClass |
| 72 | + }) |
| 73 | + } |
| 74 | + }, [props.facetUpdateID]) |
| 75 | + |
| 76 | + const rowRenderer = ({ |
| 77 | + key, // Unique key within array of rows |
| 78 | + index, // Index of row within collection |
| 79 | + isScrolling, // The List is currently being scrolled |
| 80 | + isVisible, // This row is visible within the List (eg it is not an overscanned row) |
| 81 | + style // Style object to be applied to row (to position it) |
| 82 | + }) => { |
| 83 | + const data = props.perspectiveState.results[index] |
| 84 | + let image = null |
| 85 | + if (data.imageURL) { |
| 86 | + const { imageURL } = data |
| 87 | + image = imageURL.includes(', ') ? imageURL.split(', ')[0] : imageURL |
| 88 | + } |
| 89 | + return ( |
| 90 | + <div className={classes.rowRoot} key={key} style={style}> |
| 91 | + <Link className={classes.link} to={data.dataProviderUrl}> |
| 92 | + <Card> |
| 93 | + <CardActionArea> |
| 94 | + {image && |
| 95 | + <CardMedia |
| 96 | + component='img' |
| 97 | + alt='Kuva löydöstä' |
| 98 | + height='140' |
| 99 | + image={image} |
| 100 | + title='Kuva löydöstä' |
| 101 | + />} |
| 102 | + <CardContent> |
| 103 | + <Typography gutterBottom variant='h5' component='h2'> |
| 104 | + {data.findName} |
| 105 | + </Typography> |
| 106 | + <Typography variant='body2' color='textSecondary' component='p'> |
| 107 | + <strong>{intl.get('perspectives.finds.properties.objectType.label')}: </strong> |
| 108 | + {data.objectType} |
| 109 | + </Typography> |
| 110 | + <Typography variant='body2' color='textSecondary' component='p'> |
| 111 | + <strong>{intl.get('perspectives.finds.properties.material.label')}: </strong> |
| 112 | + {data.material} |
| 113 | + </Typography> |
| 114 | + <Typography variant='body2' color='textSecondary' component='p'> |
| 115 | + <strong>{intl.get('perspectives.finds.properties.period.label')}: </strong> |
| 116 | + {data.period} |
| 117 | + </Typography> |
| 118 | + <Typography variant='body2' color='textSecondary' component='p'> |
| 119 | + <strong>{intl.get('perspectives.finds.properties.municipality.label')}: </strong> |
| 120 | + {data.municipality} |
| 121 | + </Typography> |
| 122 | + </CardContent> |
| 123 | + </CardActionArea> |
| 124 | + </Card> |
| 125 | + </Link> |
| 126 | + |
| 127 | + </div> |
| 128 | + ) |
| 129 | + } |
| 130 | + |
| 131 | + const getRowHeight = ({ index }) => { |
| 132 | + const data = props.perspectiveState.results[index] |
| 133 | + let height = 300 |
| 134 | + if (!data.imageURL) { |
| 135 | + height -= 140 |
| 136 | + } |
| 137 | + if (data.findName.length > 26) { |
| 138 | + height += 32 |
| 139 | + } |
| 140 | + if (data.findName.length > 40) { |
| 141 | + height += 54 |
| 142 | + } |
| 143 | + if (data.period) { |
| 144 | + const limit = window.innerWidth < 328 ? 25 : 34 |
| 145 | + if (data.period.length > limit) { |
| 146 | + height += 20 |
| 147 | + } |
| 148 | + } |
| 149 | + return height |
| 150 | + } |
| 151 | + |
| 152 | + const validResults = () => { |
| 153 | + const { results, resultClass } = props.perspectiveState |
| 154 | + if (resultClass !== props.resultClass) { return false } |
| 155 | + if (results == null) { return false } |
| 156 | + if (results.length < 1) { return false } |
| 157 | + return true |
| 158 | + } |
| 159 | + |
| 160 | + // if (props.perspectiveState.results) { |
| 161 | + // props.perspectiveState.results.map(r => { |
| 162 | + // if (r.period && r.period.length > 33) { |
| 163 | + // console.log(r) |
| 164 | + // } |
| 165 | + // }) |
| 166 | + // } |
| 167 | + |
| 168 | + return ( |
| 169 | + <div className={classes.root}> |
| 170 | + {(!validResults() || props.perspectiveState.results.fetching) |
| 171 | + ? ( |
| 172 | + <div className={classes.progressContainer}> |
| 173 | + <CircularProgress style={{ color: purple[500] }} thickness={5} /> |
| 174 | + </div> |
| 175 | + ) |
| 176 | + : ( |
| 177 | + <AutoSizer> |
| 178 | + {({ height, width }) => ( |
| 179 | + <List |
| 180 | + className={classes.list} |
| 181 | + height={height} |
| 182 | + width={width} |
| 183 | + rowCount={results.length} |
| 184 | + rowHeight={getRowHeight} |
| 185 | + rowRenderer={rowRenderer} |
| 186 | + /> |
| 187 | + )} |
| 188 | + </AutoSizer> |
| 189 | + )} |
| 190 | + </div> |
| 191 | + ) |
| 192 | +} |
| 193 | + |
| 194 | +export default ReactVirtualizedList |
0 commit comments