-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathResults.js
76 lines (71 loc) · 1.95 KB
/
Results.js
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
import React, { useRef } from 'react'
import PropTypes from 'prop-types'
import ResultsItem from './ResultsItem'
const Results = ({ items, status }) => {
const containerEl = useRef(null)
const isLoading = status === 'idle' || status === 'pending'
const isRejected = status === 'rejected'
let message
if (isLoading) {
message = (
<section className="callout warning text-center">
<p className="h3">Loading new results...</p>
</section>
)
} else if (isRejected) {
message = (
<section className="callout alert text-center">
<p className="h4">
There was an error retrieving results. Please try again.
</p>
</section>
)
}
return (
<>
{message}
{items.length > 0 && (
<>
<section className="callout primary" ref={containerEl}>
{items.map((item) => (
<ResultsItem
key={item.id}
id={item.id}
title={item.title}
url={item.url}
rating={item.rating}
previewUrl={item.previewUrl}
/>
))}
</section>
<footer className="text-center">
<button
type="button"
className="button"
onClick={() => {
containerEl.current.scrollIntoView(true)
}}
>
To top
</button>
</footer>
</>
)}
</>
)
}
Results.propTypes = {
error: PropTypes.instanceOf(Error),
items: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.string.isRequired,
title: PropTypes.string.isRequired,
url: PropTypes.string.isRequired,
rating: PropTypes.oneOf(['G', 'PG', 'PG-13', 'R']).isRequired,
previewUrl: PropTypes.string.isRequired,
}),
).isRequired,
status: PropTypes.oneOf(['idle', 'pending', 'resolved', 'rejected'])
.isRequired,
}
export default Results