-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
197 lines (161 loc) · 7.3 KB
/
index.html
File metadata and controls
197 lines (161 loc) · 7.3 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Blood Pressure Logger</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.status {
padding: 0.1rem 0.8rem;
border: 3px solid;
border-radius: 6px;
max-width: max-content;
box-shadow: 3px 3px 5px lightgray;
font-weight: bold;
}
.status.error {
color: firebrick;
background: tomato;
border-color: firebrick;
}
.status.info {
color: darkblue;
background: lightblue;
border-color: darkblue;
}
.status.success {
color: darkgreen;
background: springgreen;
border-color: darkgreen;
}
.status.warning {
color: #7d4e15;
background: orange;
border-color: #7d4e15;
}
</style>
</head>
<body>
<div id="app">
<h1>Blood Pressure Logger</h1>
<p>Log your blood pressure. No sign-up required!</p>
<p>
Navigation:
<ul>
<li><a href="./index.html">Add new data</a> (you are here)</li>
<li><a href="./show.html">Show previously entered data</a></li>
</ul>
</p>
<!-- Blood Pressure Logger -->
<form id="log-form" action="{{INVOKE_URL}}" method="POST">
<div class="form-group">
<label for="systolic">Systolic</label>
<input type="number" id="systolic" name="systolic" min="0" required>
</div>
<div class="form-group">
<label for="diastolic">Diastolic</label>
<input type="number" id="diastolic" name="diastolic" min="0" required>
</div>
<input type="submit" value="Save Data">
</form>
<div style="margin: 2rem 0; display: none;" id="status-message-container" class="status">
<p id="status-message"></p>
<p id="status-action-container"></p>
</div>
<div>
<p>
Sources:
<ul>
<li><a href="https://www.mayoclinic.org/diseases-conditions/high-blood-pressure/in-depth/blood-pressure/art-20050982">Mayo Clinic</a></li>
</ul>
</p>
</div>
</div>
<script>
((window, document) => {
'use strict';
document.getElementById('log-form').addEventListener('submit', handleForm);
function setStatus(message, type, actionText, action) {
let statusMessageContainer = document.getElementById('status-message-container');
let statusMessage = document.getElementById('status-message');
let statusActionContainer = document.getElementById('status-action-container');
statusMessage.innerText = message;
statusActionContainer.innerHTML = '';
if (actionText && action) {
let statusAction = document.createElement('button');
statusAction.innerText = actionText;
statusAction.addEventListener('click', action);
statusActionContainer.appendChild(statusAction);
}
// Possible status message types
const types = [ 'info', 'success', 'error', 'warning' ];
if (! types.includes(type)) {
type = types[0];
}
types.forEach(t => statusMessageContainer.classList.remove(t));
statusMessageContainer.classList.add(type);
// Only show the status message container if the message is not blank.
statusMessageContainer.style.display = message ? 'block' : 'none';
}
function logBloodPressure(systolic, diastolic, action, method) {
return new Promise((resolve, reject) => {
fetch(action, {
method: method,
body: JSON.stringify({
systolic: systolic,
diastolic: diastolic,
})
}).then((response) => {
if (! response.ok) {
reject(response.status);
} else {
resolve(response);
}
}).catch((error) => reject(error));
});
}
function findRange(value, maxValueInEachRange) {
for (let i = 0; i < maxValueInEachRange.length; i++) {
if (value < maxValueInEachRange[i]) {
return i;
}
}
}
function handleForm(event) {
event.preventDefault();
let action = event.target.action;
let method = event.target.method;
let systolic = event.target[0].value;
let diastolic = event.target[1].value;
setStatus('Loading...');
logBloodPressure(systolic, diastolic, action, method)
.then((response) => {
showLoggedMessage(systolic, diastolic);
}).catch((error) => {
setStatus('There was an error saving the data. Error: ' + error, 'error', 'Try again', () => {
// Resubmit the form
document.getElementById('log-form').submit();
});
});
}
function showLoggedMessage(systolic, diastolic) {
// Show a message to the user that their blood pressure has been saved.
// If their blood pressure is outside the normal range, give them a warning.
// Data from: https://www.mayoclinic.org/diseases-conditions/high-blood-pressure/in-depth/blood-pressure/art-20050982
const breakpointSystolic = [ 120, 130, 140, Infinity ]; // User must be less than to be in the category
const breakpointDiastolic = [ 80, 80, 90, Infinity ]; // User must be less than to be in the category
const showWarning = [ false, true, true, true ];
const breakpointNames = [ 'normal blood pressure', 'elevated blood pressure', 'stage 1 high blood pressure (hypertension)', 'stage 2 high blood pressure (hypertension)' ];
let systolicRange = findRange(systolic, breakpointSystolic);
let diastolicRange = findRange(diastolic, breakpointDiastolic);
let userRange = Math.max(systolicRange, diastolicRange);
if (showWarning[userRange]) {
setStatus(`Your blood pressure has been logged. Our records indicate you may be at risk for ${breakpointNames[userRange]}. Consult with your doctor for more information.`, 'warning');
} else {
setStatus(`Your blood pressure has been logged. Our records indicate you have ${breakpointNames[userRange]}. Consider maintaining or adopting a healthy lifestyle.`, 'success');
}
}
})(window, document);
</script>
</body>
</html>