-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
154 lines (130 loc) · 3.77 KB
/
Copy pathindex.js
File metadata and controls
154 lines (130 loc) · 3.77 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import check from 'check-arg-types'
import equal from '@app-elements/equal'
import React from 'react'
import makeRequest from './makeRequest'
const toType = check.prototype.toType
let storeRef // Will get populated if WithRequest receives `store` via context
const _existing = {}
const OK_TIME = 30000
// cache result of request by endpoint, either in store or local cache object
const cache = (endpoint, result) => {
storeRef.setState({
requests: {
...storeRef.getState().requests || {},
[endpoint]: { result, timestamp: Date.now() }
}
})
}
// get timestamp of an endpoint from store or local cache object
const getTimestamp = endpoint => {
const reqs = storeRef.getState().requests || {}
return reqs[endpoint] && reqs[endpoint].timestamp
}
// check if last saved timestamp for endpoint is not expired
const validCache = endpoint => {
const ts = getTimestamp(endpoint)
if (!ts) return false
const diff = Date.now() - ts
return diff < OK_TIME
}
// clear result for an endpoint from store or local cache object
export const clearCache = endpoint => {
storeRef.setState({
requests: {
...storeRef.getState().requests || {},
[endpoint]: null
}
})
}
const withRequest = ({
name,
endpoint,
parse
}) => PassedComponent => {
class WithRequest extends React.Component {
constructor (props, { store }) {
super(props)
storeRef = store
this.state = { ...(this.state || {}), isLoading: true, result: null, error: null }
}
_performRequest (endpoint) {
const token = this.context.store.getState().token
const headers = {}
if (token) {
headers.Authorization = `Token ${token}`
}
const { xhr, promise } = makeRequest({ endpoint, headers })
_existing[endpoint] = xhr
promise
.then(result => {
cache(endpoint, result)
delete _existing[endpoint]
if (parse) {
this.setState({ result: parse(result, this.state.result), isLoading: false })
} else {
this.setState({ result, isLoading: false })
}
})
.catch(error => {
this.setState({ error, isLoading: false })
})
}
_loadResult (endpoint) {
if (!endpoint) {
return
}
if (_existing[endpoint] && !this.state.error) {
_existing[endpoint].abort()
delete _existing[endpoint]
}
if (validCache(endpoint)) {
this.setState({
result: storeRef.getState().requests[endpoint].result,
isLoading: false
})
} else {
this._performRequest(endpoint)
}
}
componentDidMount () {
this._loadResult(
toType(endpoint) === 'function'
? endpoint(this.props)
: endpoint
)
}
shouldComponentUpdate (nextProps, nextState) {
const nextEnpoint = toType(endpoint) === 'function'
? endpoint(nextProps)
: endpoint
const currEnpoint = toType(endpoint) === 'function'
? endpoint(this.props)
: endpoint
if (currEnpoint !== nextEnpoint) {
return true
}
return !equal(nextState, this.state) || !equal(this.props, nextProps)
}
componentDidUpdate () {
const currEnpoint = toType(endpoint) === 'function'
? endpoint(this.props)
: endpoint
if (!_existing[currEnpoint]) {
this.setState({ isLoading: true })
this._loadResult(currEnpoint)
}
}
render () {
return (
<PassedComponent {...this.props} {...this.state} />
)
}
}
const passedComponentName = PassedComponent.displayName ||
PassedComponent.name ||
name ||
'PassedComponent'
WithRequest.displayName = `withRequest(${passedComponentName})`
return WithRequest
}
export default withRequest