Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locale Data in Enviornment Variable #71

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ PORT=8080
TZ=America/New_York
CHOREBOT_VERIFICATION_TOKEN=chorebot token here
ENV=dev
LONGITUDE:logitudehere
LATITUDE:latitudehere
16 changes: 9 additions & 7 deletions public/js/whiteboard.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/* global moment, io, SunCalc */

const longitude = -73.67577;
const latitude = 42.72927;
let locale;
// 30 minutes in milliseconds (60000 ms in 1 min)
//Time Constants(in Milliseconds)
const oneMinute = 60000;
Expand Down Expand Up @@ -90,7 +88,7 @@ function updateDate() {
document.querySelector("#date").innerHTML = todaysDate.format("D MMM YY");
document.querySelector("#time").innerHTML = todaysDate.format("HH:mm");

const times = SunCalc.getTimes(new Date(), latitude, longitude);
const times = SunCalc.getTimes(new Date(), locale.latitude, locale.longitude);
const now = Date.now();
// if the current time falls between 30 minutes after sunrise and
// 30 minutes after sunset, then we use the light stylesheet, otherwise
Expand Down Expand Up @@ -195,8 +193,6 @@ function handleDispatch(determinant, complaint, location) {
dispatchTimeoutID = setTimeout(clearDispatch, threeMinutes);
}

updateDate();

const socket = io.connect();
socket.on("notes", (noteResponse) => {
updateNotes(noteResponse);
Expand Down Expand Up @@ -230,4 +226,10 @@ socket.on("dispatch", (dispatchResponse) => {
// Refreshes the page allowing us to update the UI without ever touching the tv
socket.on("refresh", () => window.location.reload());

setInterval(() => updateDate(), 2000);
const response = fetch('/locale')
.then((response) => response.json())
.then((data) => {
locale = data;
updateDate();
setInterval(() => updateDate(), 2000);
});
Comment on lines +229 to +235
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be handled without the let locale; up above, particularly because locale should be immutable. Maybe something like:

const locale = await fetch("/locale");
const [{lat}, {long}] = locale.json();
setInterval(() => updateDate(lat, long), 2000);

This may not be entirely correct syntax-wise, but I think it's a sleeker way to handle it than using .then(). I realize it involves slight restructuring of updateDate(), but it could be handled in other ways, too. You may be able to leave lat and long as globals consts and be done. Let me know your thoughts.

5 changes: 5 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const pool = mariadb.createPool({
const PORT = process.env.PORT || 8080;
const WEBSITE_ACCESS_TOKEN = process.env.WEBSITE_ACCESS_TOKEN;
const HERALD_TOKEN = process.env.HERALD_TOKEN;
const LOCALE = {longitude: process.env.LONGITUDE, latitude: process.env.LATITUDE};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please abbreviate the JS variables to lat and long, respectively, just for brevity's sake. No problem keeping the env vars as the full word—that's your choice.


// app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
Expand All @@ -34,6 +35,10 @@ app.get('/suncalc.js', (_, res) => {
res.sendFile(path.join(__dirname, 'node_modules', 'suncalc', 'suncalc.js'));
});

app.get('/locale', (_, res) => {
res.json(LOCALE);
});

app.get('/', (req, res) => {
if (WEBSITE_ACCESS_TOKEN !== req.query.token) {
res.sendStatus(403);
Expand Down