-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
144 lines (130 loc) · 4.04 KB
/
index.html
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
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<meta http-equiv='X-UA-Compatible' content='ie=edge'>
<title>Patient Lists API Demo</title>
</head>
<body>
<table id='patients' border=1>
<tr>
<th>Patient</th>
<th>Age</th>
<th>Gender</th>
<th>Location</th>
<th>Last Visit</th>
<th>Admin</th>
<th>Chief Complaint</th>
<th>PCP</th>
<th>Attending</th>
</tr>
</table>
<script type='module' src='./node_modules/moment/src/moment.js'></script>
<script>
const SERVER = 'http://hapi.fhir.org/baseR4';
function jsonify(response) {
return response.json();
}
function isActive(group) {
if ('active' in group.resource) {
return group.resource.active;
}
return true;
}
// Copies all entries from a Bundle by recursively fetching all 'next' links until done.
async function fetchAll(bundle, entries) {
entries.push(bundle.entry);
const next = bundle.link.find((x) => x.relation === 'next');
if (next) {
await fetch(next.url)
.then(jsonify)
.then(async (nextBundle) => {
await fetchAll(nextBundle, entries);
})
.catch(console.error);
}
}
async function getGroups(bundle) {
const entities = [];
await fetchAll(bundle, entities);
const groups = entities.flat().filter(isActive);
// Resolve all 'descriptive' groups to 'actual'
for (let i = 0, x = groups.length; i < x; i += 1) {
const group = groups[i];
if (!group.actual) {
console.log('TODO: NEED TO RESOLVE DESCRIPTIVE GROUP', group);
// TODO: update the group in-place with the resolved instance.
// groups[i] = group.resolve(); // TODO: something like this.
}
}
return groups;
}
async function getPatients(groups) {
// Start by finding the set of unique patient references among all found groups.
const patients = new Map();
for (let i = 0; i < groups.length; i += 1) {
const members = groups[i].resource.member || [];
for (let j = 0; j < members.length; j += 1) {
const id = members[j].entity.reference;
if (id) {
patients.set(id);
}
}
}
// Resolve all patient references to resources before returning.
patients.keys().map((reference) => {
patients.set(
reference,
fetch(`${SERVER}/${reference}`)
.then(jsonify)
.catch(console.error),
);
return true;
});
// Resolve all promises, leaving the resource objects in the rval.
const rval = [];
patients.values().map(async (patient) => {
rval.push(await patient);
return true;
});
return rval;
}
function getName(patient) {
if (!patient.name || !patient.name) {
return `UNKNOWN NAME (id=${patient.id})`;
}
return `${patient.name[0].family}, ${patient.name[0].given.join(' ')}`;
}
function getPatientDisplayData(patient) {
const rval = [];
rval.push(getName(patient));
// TODO: return an array of items to put into a table.
rval.push('');
rval.push(patient.gender);
return rval;
}
function render(patientList) {
const patients = document.getElementById('patients');
for (let i = 0; i < patientList.length; i += 1) {
const data = getPatientDisplayData(patientList[i]);
const tr = document.createElement('tr');
data.map((text) => {
const td = document.createElement('td');
td.innerHTML = text;
tr.appendChild(td);
return true;
});
patients.appendChild(tr);
}
}
// TODO: add any other contraints to the Group query here...
fetch(`${SERVER}/Group?type=person`)
.then(jsonify)
.then(getGroups)
.then(getPatients)
.then(render)
.catch(console.error);
</script>
</body>
</html>