-
Notifications
You must be signed in to change notification settings - Fork 0
/
CamperList.js
106 lines (96 loc) · 2.5 KB
/
CamperList.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
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
import React from 'react'
import './CamperList.css'
import { FixedSizeList as List } from 'react-window'
import NewCamperForm from './NewCamperForm'
class ItemRenderer extends React.PureComponent {
render() {
let camper = this.props.data[this.props.index]
return (
<div
key={camper.id}
style={this.props.style}
className={this.props.index % 2 ? 'ListItemOdd' : 'ListItemEven'}
>
{camper.name}
{camper.sac.sort().map(summer => (
<span className="badge-summer" key={`${camper.id}-${summer}`}>
{summer}
</span>
))}
{/*<span className={`rank-badge ${camper.rank.toLowerCase()}`}>
{camper.rank}
</span>*/}
</div>
)
}
}
class CamperList extends React.Component {
state = {
camperData: [],
nameSearchValue: '',
}
_fetchCampersRequest = null
handleChange = e => {
let searchValue = e.target.value
this.setState(
{ nameSearchValue: searchValue },
this.fetchCampers(searchValue)
)
}
fetchCampers = searchValue => {
if (this._fetchCampersRequest === null) {
let query = '/campers?sac=True'
if (searchValue) query += `&search=${searchValue}`
this._fetchCampersRequest = this.fetchCamperData(query).then(res => {
this._fetchCampersRequest = null
this.setState({ camperData: res.campers })
})
}
}
fetchCamperData = async req => {
try {
const response = await fetch(req)
return await response.json()
} catch (err) {
console.error(err)
return []
}
}
componentDidMount() {
this.fetchCampers()
}
componentWillUnmount() {
if (this._fetchCampersRequest) {
this._fetchCampersRequest.cancel()
}
}
render() {
return (
<div className="container">
<div className="search-bar">
<input
type="text"
placeholder="Search"
value={this.state.nameSearchValue}
onChange={this.handleChange}
/>
</div>
<List
className="list"
height={250}
itemCount={this.state.camperData.length}
itemSize={35}
width={600}
>
{props => <ItemRenderer {...props} data={this.state.camperData} />}
</List>
<div className="new-camper-form">
<NewCamperForm
updateList={() => this.fetchCampers(this.state.nameSearchValue)}
/>
</div>
</div>
)
}
}
export default CamperList