Skip to content

Commit

Permalink
add water widget
Browse files Browse the repository at this point in the history
  • Loading branch information
narlock committed Jun 2, 2024
1 parent 9ce7779 commit a5417f2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 10 deletions.
20 changes: 10 additions & 10 deletions public/js/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as ProfileUtils from '../lib/graph/profileLoader.js'
import * as ProfileWidget from '.././lib/profileWidget.js'
import * as HabitLoader from '.././lib/graph/habitLoader.js'
import * as HabitWidget from '.././lib/habitWidget.js'
import * as WeightLoader from '.././lib/graph/weightTrackLoader.js'
import * as WeightWidget from '.././lib/weightWidget.js'
import * as ProfileUtils from '../lib/graph/profileLoader.js';
import * as ProfileWidget from '.././lib/profileWidget.js';
import * as HabitLoader from '.././lib/graph/habitLoader.js';
import * as HabitWidget from '.././lib/habitWidget.js';
import * as WeightLoader from '.././lib/graph/weightTrackLoader.js';
import * as WeightWidget from '.././lib/weightWidget.js';
import * as WaterWidget from '../lib/waterWidget.js';

var PROFILE;

Expand Down Expand Up @@ -89,10 +90,9 @@ async function populateWidgetData(boxElement, widgetName) {
break;
case 'water':
// Add header to box
const waterHeader = document.createElement('h2');
waterHeader.textContent = 'Water Management';
boxElement.appendChild(waterHeader);
// Add widget to box and display
var waterWidget = await WaterWidget.createWaterInterface();
console.log(waterWidget);
boxElement.appendChild(waterWidget);
break;
default:
// Check for habit widget
Expand Down
55 changes: 55 additions & 0 deletions public/lib/waterWidget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import * as WaterLoader from '../lib/graph/waterLoader.js';
import * as Utils from '../lib/utils/utils.js';
import * as ProfileUtils from '../lib/graph/profileLoader.js';

var TODAY_WATER_AMOUNT;
var TODAY_AS_STRING = Utils.todayAsString();

async function createWaterInterface() {
var response = await WaterLoader.getWaterEntryAmount(1, TODAY_AS_STRING);
if(response) {
TODAY_WATER_AMOUNT = response.entryAmount;
}

if(TODAY_WATER_AMOUNT) {
// Entry has been created for today
console.log("Entry existed for today");
return displayWaterEntryToday(TODAY_WATER_AMOUNT);
} else {
// Entry has not been created
console.log("Entry did NOT exist for today");
var response = await WaterLoader.createWaterEntry(1, TODAY_AS_STRING);
console.log(response);
TODAY_WATER_AMOUNT = response.entryAmount;
return displayWaterEntryToday(TODAY_WATER_AMOUNT);
}
}

function displayWaterEntryToday(amount) {
var interfaceDiv = document.createElement('div');
interfaceDiv.id = 'waterBox';

// Add header
var header = document.createElement('h2');
header.innerText = `You've drank ${amount} water today`;
interfaceDiv.appendChild(header);

// Add button interface to add
var span = document.createElement('span');

var input = document.createElement('input')
input.type = 'text';
var button = document.createElement('button');
button.innerText = 'Add Water';
button.addEventListener('click', async () => {
console.log(input.value);
await WaterLoader.addWaterToEntry(1, TODAY_AS_STRING, input.value);
window.location.reload(true);
});
span.appendChild(input);
span.appendChild(button);
interfaceDiv.appendChild(span);
return interfaceDiv;
}

export { createWaterInterface }

0 comments on commit a5417f2

Please sign in to comment.