forked from typicode/json-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
75 lines (68 loc) · 1.5 KB
/
Copy pathindex.js
File metadata and controls
75 lines (68 loc) · 1.5 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
import 'promise-polyfill/src/polyfill'
import 'whatwg-fetch'
import { h, render } from 'preact'
import 'milligram/dist/milligram.css'
import './style.css'
function ResourceItem({ name, length }) {
return (
<li>
<a href={name}>/{name}</a> <sup>{length ? `${length}x` : 'object'}</sup>
</li>
)
}
function ResourceList({ db }) {
return (
<ul>
{Object.keys(db).map(name => (
<ResourceItem
name={name}
length={Array.isArray(db[name]) && db[name].length}
/>
))}
</ul>
)
}
function NoResources() {
return <p>No resources found</p>
}
function ResourcesBlock({ db }) {
return (
<div>
<h4>Resources</h4>
{Object.keys(db).length ? <ResourceList db={db} /> : <NoResources />}
</div>
)
}
window
.fetch('db')
.then(response => response.json())
.then(db =>
render(<ResourcesBlock db={db} />, document.getElementById('resources'))
)
function CustomRoutesBlock({ customRoutes }) {
const rules = Object.keys(customRoutes)
if (rules.length) {
return (
<div>
<h4>Custom Routes</h4>
<table>
{rules.map(rule => (
<tr>
<td>{rule}</td>
<td>⇢ {customRoutes[rule]}</td>
</tr>
))}
</table>
</div>
)
}
}
window
.fetch('__rules')
.then(response => response.json())
.then(customRoutes => {
render(
<CustomRoutesBlock customRoutes={customRoutes} />,
document.getElementById('custom-routes')
)
})