-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
193 lines (167 loc) · 7.98 KB
/
script.js
File metadata and controls
193 lines (167 loc) · 7.98 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
document.addEventListener("DOMContentLoaded", () => {
const form = document.getElementById("trackerForm");
const logList = document.getElementById("logList");
const container = document.querySelector(".container");
const message = document.getElementById("message");
const infoBtn = document.getElementById("infoBtn");
const explainer = document.getElementById("explainer");
const installPwaBtn = document.getElementById("installPwaBtn");
let deferredPrompt;
// Toggle explainer information display
infoBtn.addEventListener("click", () => {
if (explainer.style.display === "none") {
explainer.style.display = "block";
infoBtn.textContent = "Hide Info";
} else {
explainer.style.display = "none";
infoBtn.textContent = "How does this work?";
}
});
// Handle the beforeinstallprompt event
window.addEventListener("beforeinstallprompt", (e) => {
e.preventDefault();
deferredPrompt = e;
installPwaBtn.style.display = "block"; // Show the install button
installPwaBtn.style.margin = "10px 0"; // Ensure same margin as other button
installPwaBtn.style.width = "100%"; // Ensure same width as other button
installPwaBtn.style.maxWidth = "300px"; // Ensure same max-width as other button
});
// Handle the PWA install button click event
installPwaBtn.addEventListener("click", () => {
installPwaBtn.style.display = "none"; // Hide the install button
deferredPrompt.prompt();
deferredPrompt.userChoice.then((choiceResult) => {
if (choiceResult.outcome === "accepted") {
console.log("User accepted the A2HS prompt");
} else {
console.log("User dismissed the A2HS prompt");
}
deferredPrompt = null;
});
});
// Handle the appinstalled event
window.addEventListener("appinstalled", () => {
console.log("PWA was installed");
installPwaBtn.style.display = "none"; // Ensure the install button is hidden after installation
});
// Check if the app is already installed
if (window.matchMedia('(display-mode: standalone)').matches || window.navigator.standalone === true) {
installPwaBtn.style.display = "none"; // Hide the install button if already installed
}
// Handle form submission
form.addEventListener("submit", (event) => {
event.preventDefault();
const date = formatDate(document.getElementById("date").value);
// Get the selected mucus value
const mucusRadio = document.querySelector('input[name="mucus"]:checked');
const mucus = mucusRadio ? mucusRadio.value : ""; // Get the value of the selected radio button
if (date && mucus) {
saveObservation(date, mucus);
displayLog();
checkFertilityStatus();
}
});
// Save observation to local storage
function saveObservation(date, mucus) {
let observations = JSON.parse(localStorage.getItem("observations")) || [];
const existingIndex = observations.findIndex(obs => obs.date === date);
if (existingIndex !== -1) {
observations[existingIndex] = { date, mucus };
} else {
observations.push({ date, mucus });
}
observations.sort((a, b) => new Date(b.date) - new Date(a.date));
localStorage.setItem("observations", JSON.stringify(observations));
}
// Delete observation from local storage
function deleteObservation(index) {
let observations = JSON.parse(localStorage.getItem("observations")) || [];
observations.splice(index, 1);
localStorage.setItem("observations", JSON.stringify(observations));
displayLog();
checkFertilityStatus();
}
// Display the log of observations
function displayLog() {
const observations = JSON.parse(localStorage.getItem("observations")) || [];
observations.sort((a, b) => new Date(b.date) - new Date(a.date));
if (observations.length === 0) {
logList.innerHTML = "";
message.innerText = "Enter data to track fertility";
return;
}
message.innerText = "";
logList.innerHTML = observations.map((obs, index) => {
const formattedDate = formatDisplayDate(obs.date);
return `<li>${formattedDate}: ${obs.mucus} <button onclick="deleteObservation(${index})">Delete</button></li>`;
}).join('');
}
// Check the fertility status based on observations
function checkFertilityStatus() {
const observations = JSON.parse(localStorage.getItem("observations")) || [];
if (observations.length === 0) {
document.body.style.backgroundColor = "#f4f4f4";
message.innerText = "Enter data to track fertility";
return;
}
const today = new Date().toLocaleDateString('en-CA');
const yesterday = new Date(Date.now() - 86400000).toLocaleDateString('en-CA');
const todayObservation = observations.find(obs => obs.date === today);
const yesterdayObservation = observations.find(obs => obs.date === yesterday);
const todayHasMucus = todayObservation && todayObservation.mucus === "yes";
const yesterdayHasMucus = yesterdayObservation && yesterdayObservation.mucus === "yes";
const todayMissing = !todayObservation;
const yesterdayMissing = !yesterdayObservation;
if (todayHasMucus || yesterdayHasMucus || todayMissing || yesterdayMissing) {
message.innerText = "Pregnancy is possible today";
document.body.style.backgroundColor = "#9B5D9B";
message.style.backgroundColor = "#6A0D91";
message.style.color = "#FFFFFF";
} else {
message.innerText = "Pregnancy is unlikely today";
document.body.style.backgroundColor = "#FEFB87";
message.style.backgroundColor = "#FFF826";
message.style.color = "#000000";
}
}
// Format date for display
function formatDisplayDate(dateString) {
const date = new Date(dateString);
const options = { day: 'numeric', month: 'short', year: 'numeric' };
return date.toLocaleDateString(undefined, options);
}
// Format date for storage
function formatDate(dateString) {
const date = new Date(dateString);
return date.toLocaleDateString('en-CA');
}
// Initialize the display
displayLog();
checkFertilityStatus();
// Function to delete observation globally
window.deleteObservation = deleteObservation;
// Service Worker Update Logic
if ('serviceWorker' in navigator) {
const serviceWorkerUrl = new URL('service-worker.js', window.location.href);
navigator.serviceWorker.register(serviceWorkerUrl.pathname).then(function(registration) {
console.log('Service Worker registered with scope:', registration.scope);
// Check if a new service worker is available
registration.addEventListener('updatefound', function() {
const installingWorker = registration.installing;
installingWorker.onstatechange = function() {
if (installingWorker.state === 'installed' && navigator.serviceWorker.controller) {
// New service worker has been installed
console.log('New content is available; please refresh.');
// Notify the user and automatically refresh
if (confirm('A new version is available. Refresh to update?')) {
installingWorker.postMessage({ action: 'skipWaiting' });
window.location.reload(); // Force refresh after new worker is activated
}
}
};
});
}).catch(function(error) {
console.log('Service Worker registration failed:', error);
});
}
});