-
Notifications
You must be signed in to change notification settings - Fork 0
/
greeting.js
178 lines (153 loc) · 8.36 KB
/
greeting.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// ========================== S T A R T - G R E E T I N G =========================
function saveName() {
var name = document.getElementById("nameInput").value;
if (localStorage.getItem("name") != name) {
// Reload the current page
location.reload();
localStorage.setItem("name", name);
// document.getElementById("nameInputContainer").style.display = "none";
// Reload the current page
location.reload();
}
}
// 24 hours format
// function loadName() {
// var name = localStorage.getItem("name");
// if (name != null) {
// setInterval(function () {
// // Get the current time in hours and minutes
// var now = new Date();
// var hour = now.getHours();
// var minute = now.getMinutes();
// // Determine the appropriate greeting based on the time of day
// }
// var greeting;
// if (hour >= 0 && hour < 12) {
// greeting = "Good morning";
// // Display the greeting with the current time in minutes
// document.getElementById("greeting").innerHTML = "<div class='greeting'><h1>" + hour + ":" + (minute < 10 ? "0" : "") + minute + "am, " + greeting + ", " + name + "!</h1><p>Welcome back to our food planner. We're glad to have you here!</p></div>";
// } else if (hour >= 12 && hour < 18) {
// greeting = "Good afternoon";
// // Display the greeting with the current time in minutes
// document.getElementById("greeting").innerHTML = "<div class='greeting'><h1>" + hour + ":" + (minute < 10 ? "0" : "") + minute + "pm, " + greeting + ", " + name + "!</h1><p>Welcome back to our food planner. We're glad to have you here!</p></div>";
// } else {
// greeting = "Good evening";
// // Display the greeting with the current time in minutes
// document.getElementById("greeting").innerHTML = "<div class='greeting'><h1>" + hour + ":" + (minute < 10 ? "0" : "") + minute + "pm, " + greeting + ", " + name + "!</h1><p>Welcome back to our food planner. We're glad to have you here!</p></div>";
// }
// }, 1000); // Update every 1000 milliseconds (1 second)
// document.getElementById("greeting").innerHTML = greeting + ", " + name + "!";
// document.getElementById("nameInputContainer").style.display = "none";
// }
// }
// 12 hours format
function loadName() {
var name = localStorage.getItem("name");
if (name != null) {
setInterval(function () {
// Get the current time in hours and minutes
var now = new Date();
var hour = now.getHours();
var minute = now.getMinutes();
// Determine the appropriate greeting based on the time of day
var ampm;
var status;
// Determine the appropriate greeting based on the time of day
var greeting;
if (hour >= 0 && hour < 12) {
greeting = "Good morning";
}
else if (hour >= 12 && hour < 18) {
greeting = "Good afternoon";
} else {
greeting = "Good evening";
}
// Determine the appropriate day status based on the time of day
if (hour >= 0 && hour < 6) {
status = ' <i class="fa-regular fa-moon"></i>';
status = ' <i class="fa-solid fa-cloud-moon"></i>';
}
else if (hour >= 6 && hour < 12) {
status = ' <i class="fa-regular fa-sun"></i>';
status = '<i class="fa-solid fa-cloud-sun"></i>';
}
else if (hour >= 12 && hour < 18) {
status = ' <i class="fa-regular fa-sun"></i>';
status = '<i class="fa-solid fa-cloud-sun"></i>';
} else {
status = ' <i class="fa-regular fa-moon"></i>';
status = ' <i class="fa-solid fa-cloud-moon"></i>';
}
if (hour >= 12) {
ampm = 'pm';
hour -= 12;
} else {
ampm = 'am';
}
hour = hour ? hour : 12; // handle midnight (0 hours)
// Display the greeting with the current time in minutes
document.getElementById("greeting").innerHTML = "<div class='greeting'><h4>" + hour + ":" + (minute < 10 ? "0" : "") + minute + " " + ampm + status + " " + greeting + " " + name + "!</h4></div>";
}, 1000); // Update every 1000 milliseconds (1 second)
// document.getElementById("greeting").innerHTML = greeting + ", " + name + "!";
document.getElementById("nameInputContainer").style.display = "none";
}
}
// ========================== E N D - G R E E T I N G =========================
// ========================== S T A R T - M O T I V A T I O N A L - M E S S A G E S =========================
// Define the URL of the API that provides the motivational messages
const API_URL = "https://type.fit/api/quotes";
// Define the default messages to use when no internet connection is available
const DEFAULT_MESSAGES = [
"Believe in yourself and all that you are. Know that there is something inside you that is greater than any obstacle.",
"You are never too old to set a new goal or to dream a new dream.",
"Success is not final, failure is not fatal: It is the courage to continue that counts.",
"Chase your dreams but always know the road that will lead you home again.",
];
// Get the last time a message was displayed from localStorage
let lastDisplayed = localStorage.getItem("lastDisplayed");
// If there is no lastDisplayed value in localStorage, set it to 0
if (!lastDisplayed) {
lastDisplayed = 0;
localStorage.setItem("lastDisplayed", lastDisplayed);
}
// Get the current time in milliseconds
const now = new Date().getTime();
// Calculate the time since the last message was displayed
const timeSinceLastDisplayed = now - lastDisplayed;
// If more than 24 hours have passed since the last message was displayed
// if (timeSinceLastDisplayed > 24 * 60 * 60 * 1000) {
if (timeSinceLastDisplayed > 1000) {
// Make a request to the API to get a new message
fetch(API_URL)
.then((response) => response.json())
.then((data) => {
// Get a random message from the API response
const randomIndex = Math.floor(Math.random() * data.length);
const randomMessage = data[randomIndex];
// Display the new message to the user
const messageElement = document.getElementById("message");
messageElement.innerHTML = '<i class="fa-solid fa-quote-left fa-fade" style="color: #f9a03f;"></i> ' + randomMessage.text;
// Show the motivation message div
const motivationDiv = document.getElementById("motivation-message");
motivationDiv.style.display = "block";
// Update the lastDisplayed value in localStorage with the current time
localStorage.setItem("lastDisplayed", now);
})
.catch((error) => {
// If there is an error fetching the message from the API, use the next default message instead
const lastDisplayedIndex = parseInt(lastDisplayed);
const nextIndex = (lastDisplayedIndex + 1) % DEFAULT_MESSAGES.length;
const messageElement = document.getElementById("message");
messageElement.innerHTML = '<i class="fa-solid fa-quote-left fa-fade" style="color: #f9a03f;"></i> ' + DEFAULT_MESSAGES[nextIndex];
// Show the motivation message div
const motivationDiv = document.getElementById("motivation-message");
motivationDiv.style.display = "block";
// Update the lastDisplayed value in localStorage with the current time
localStorage.setItem("lastDisplayed", nextIndex.toString());
});
} else {
// If less than 24 hours have passed since the last message was displayed, hide the motivation message div
const motivationDiv = document.getElementById("motivation-message");
motivationDiv.style.display = "none";
}
// ========================== E N D - M O T I V A T I O N A L - M E S S A G E S =========================