-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnobuild.js
156 lines (145 loc) · 4.31 KB
/
nobuild.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
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
155
156
import { h, Component, render } from 'https://unpkg.com/preact?module';
import htm from 'https://unpkg.com/htm?module';
// Initialize htm with Preact
const html = htm.bind(h);
/**
* functional component example
*/
const MyJumbo = () => {
return html`
<div class='jumbotron myextra'>
<div class="text-center mytitle" id="title">Preact w/htm Example </div>
<p class="info">Data retrieved from \
<a href="https://jsonplaceholder.typicode.com/" target="blank">jsonplaceholder.typicode.com</a>, a great api for testing.
<br/>
<i>Build using <a href="https://preactjs.com/" target="blank">preact</a> \
and <a href="https://github.com/developit/htm" target="blank">htm</i></a>.
</p>
</div>
`;
};
class App extends Component {
constructor(props) {
super(props);
this.state = { code: null };// track status
}
/**
* Get a Post by its ID
*/
getById() {
const id = parseInt(document.getElementById('findby').value, 10);
if (!id) {
alert('no post id in query');
return;
}
let spin = document.getElementById('getspan');
spin.hidden = false;
fetch(`https://jsonplaceholder.typicode.com/posts/${id}`)
.then(response => {
this.setState({code: response.status})
return response.json();
})
.then(data => {
this.resetTableBody();
this.createTableRow(data);
})
.catch(error => console.error('fetch Error:', error))
.finally(() => spin.hidden = true);
}
/**
* Fetch all Posts
*/
fetchAll() {
let spin = document.getElementById('fetchspan');
spin.hidden = false;
fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => {
this.setState({code: response.status})
return response.json();
})
.then(data => {
this.resetTableBody();
data.forEach(item => this.createTableRow(item));
})
.catch(error => console.error('fetch Error:', error))
.finally(() => spin.hidden = true);
}
/**
* Remove and replace the <tbody>
*/
resetTableBody() {
const table = document.getElementById('tableforall');
let body = document.getElementById('tb');
body.remove();
body = document.createElement('tbody');
body.setAttribute('id', 'tb');
table.append(body);
}
/**
* Create a table row
*
* @param {object} item the data
*/
createTableRow(item) {
const keys = Object.keys(item);
if (!keys.length) {
alert(`Nothing found: http status ${this.state.code}`);
return;
}
const tb = document.getElementById('tb');
const row = document.createElement("TR");
//first time create the headings
const tableHeader = document.getElementById('thd');
const addHeaders = !tableHeader.hasChildNodes();
const hrow = addHeaders ? document.createElement("TR") : null;
keys.forEach(key => {
if (hrow) {
const hcell = document.createElement('TH');
hcell.innerHTML = key.toUpperCase();
hrow.appendChild(hcell);
tableHeader.appendChild(hrow);
}
const cell = document.createElement('TD');
cell.innerHTML = item[key];
row.appendChild(cell);
});
tb.appendChild(row)
}
/**
* Render our page
*/
render() {
return html`
<div class="container">
<${MyJumbo} />
<hr />
<div class='row'>
<div class="col-sm-6">
<button type="button" class="btn btn-primary" onClick=${()=> this.fetchAll()}>
<span id="fetchspan" hidden="true" class="spinner-border spinner-border-sm text-success"></span>
Fetch all Posts
</button>
</div>
<div class="col-sm-6">
<button type="button" class="btn btn-primary" onClick=${()=> this.getById()}>
<span id="getspan" hidden="true" class="spinner-border spinner-border-sm text-danger"></span>
Find Post by id
</button>
<label>
<input class="form-control input-ext" type="text" id="findby" />
</label>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<table id="tableforall" class="table table-dark">
<thead id='thd' />
<tbody id='tb' />
</table>
</div>
</div>
</div>
`;
}
}
render(html`<${App} />`, document.body);