-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclient.js
More file actions
64 lines (60 loc) · 1.76 KB
/
client.js
File metadata and controls
64 lines (60 loc) · 1.76 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
'use strict';
const buildAPI = (methods) => {
const api = {};
for (const method of methods) {
api[method] = (...args) =>
new Promise((resolve, reject) => {
const url = `/api/${method}`;
console.log(url, args);
fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(args),
}).then((res) => {
const { status } = res;
if (status !== 200) {
reject(new Error(`Status Code: ${status}`));
return;
}
resolve(res.json());
});
});
}
return api;
};
const api = buildAPI(['entity', 'read', 'update']);
const createForm = async (entity, id) => {
const schema = await api.entity(entity);
const instance = await api.read(id);
const form = document.createElement('div');
const inputs = {};
for (const field in schema) {
const definition = schema[field];
const input = document.createElement('input');
input.setAttribute('id', field);
if (definition.control === 'password') {
input.setAttribute('type', 'password');
}
input.value = instance[field];
inputs[field] = input;
const label = document.createElement('label');
label.innerHTML = field;
label.setAttribute('for', field);
const br = document.createElement('br');
form.appendChild(label);
form.appendChild(input);
form.appendChild(br);
}
const button = document.createElement('button');
button.innerHTML = 'Save';
button.onclick = () => {
const instance = {};
for (const field in schema) {
instance[field] = inputs[field].value;
}
api.update(id, instance);
};
form.appendChild(button);
document.body.appendChild(form);
};
createForm('sensor', 2000);