Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
66 lines (62 loc) · 1.61 KB
/
Copy pathindex.js
File metadata and controls
66 lines (62 loc) · 1.61 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
import React from 'react'
import map from '@wasmuth/map'
import pathOr from '@wasmuth/path-or'
import connect from '@app-elements/connect'
import Pagination from '@app-elements/pagination'
import qs from '@app-elements/router/qs'
const getActivePage = () => {
const search = typeof window !== 'undefined' ? window.location.search : ''
const args = qs.parse(search)
return args.page ? parseInt(args.page, 10) : 1
}
// We subscribe to `currentPath` to rerender on route change
const ListResource = connect({
name: 'ListResource',
withState: ({ currentPath }) => ({ currentPath }),
withRequest: {
endpoint: ({ limit, endpoint }) => {
const activePage = getActivePage()
return limit != null
? `${endpoint}?limit=${limit}${activePage > 1 ? `&offset=${limit * activePage}` : ''}`
: endpoint
}
}
})(({
endpoint,
limit,
list = true,
pagination = false,
result,
isLoading,
render: View
}) =>
isLoading
? <p>Loading...</p>
: (
<div key={endpoint}>
{list
? map(
(item, idx) => <View key={item.id || idx} {...item} />,
pathOr(result, 'results', result)
)
: <View {...result} />}
{pagination && limit != null
? (
<Pagination
activePage={getActivePage()}
request={result}
pageSize={limit}
/>
)
: null}
</div>
)
)
export default ListResource
export const Resource = ({ endpoint, ...props }) =>
<ListResource
key={`resource-${endpoint}`}
{...props}
list={false}
endpoint={endpoint}
/>