diff --git a/code/client/src/components/CreateGoal.js b/code/client/src/components/CreateGoal.js index 48c1bbbd9..b35540fda 100644 --- a/code/client/src/components/CreateGoal.js +++ b/code/client/src/components/CreateGoal.js @@ -11,8 +11,8 @@ import { InputLabel, Select, MenuItem, + Button } from "@mui/material"; -import { Button } from "@mui/material"; import { box, title, diff --git a/code/client/src/components/DailyData.js b/code/client/src/components/DailyData.js index b683c0071..fa16d2ed1 100644 --- a/code/client/src/components/DailyData.js +++ b/code/client/src/components/DailyData.js @@ -1,4 +1,5 @@ import React, { useState, useRef, useEffect } from "react"; +import apiClient from "../services/apiClient.js"; import { Box, Typography, @@ -8,6 +9,7 @@ import { InputAdornment, Collapse, Paper, + Button } from "@mui/material"; import { DayPicker } from "react-day-picker"; import "react-day-picker/dist/style.css"; @@ -26,40 +28,56 @@ import { } from "./style/styles.js"; function DailyData() { - const [weight, setWeight] = useState(""); - const [steps, setSteps] = useState(""); - const [sleep, setSleep] = useState(""); - const [mood, setMood] = useState(3); - const [exercise, setExercise] = useState("No"); - const [exerciseType, setExerciseType] = useState(""); - const [exerciseTime, setExerciseTime] = useState({ - type: "", - exerciseTimeValue: 0, + + const [formData, setFormData] = useState({ + // entryDate: "", + weight: "", + steps: "", + sleep: "", + water: "", + exercise: "" }); - const [water, setWater] = useState(""); - const [breakfast, setBreakfast] = useState(""); - const [lunch, setLunch] = useState(""); - const [dinner, setDinner] = useState(""); + + // Handle input changes and update formData state + const handleChange = (e) => { + const { name, value } = e.target; + setFormData((prevData) => ({ + ...prevData, + [name]: value, + })); + }; + + // const [mood, setMood] = useState(3); + // const [breakfast, setBreakfast] = useState(""); + // const [lunch, setLunch] = useState(""); + // const [dinner, setDinner] = useState(""); + // date const [date, setDate] = useState(null); // + const [anchorEl, setAnchorEl] = useState(null); // control Popper content const [open, setOpen] = useState(false); // control Popper open/close + const handleDateChange = (selectedDate) => { setDate(selectedDate); setOpen(false); // close after chosing date }; + const handleTextFieldClick = (event) => { setAnchorEl(event.currentTarget); setOpen((prevOpen) => !prevOpen); // open/close calendar }; + const formatDate = (date) => { return date ? date.toLocaleDateString("en-CA") : ""; }; - const handleMoodChange = (event, newValue) => { - setMood(newValue); - }; + // const handleMoodChange = (event, newValue) => { + // setMood(newValue); + // }; + const calendarRef = useRef(null); + useEffect(() => { function handleClickOutside(event) { if (calendarRef.current && !calendarRef.current.contains(event.target)) { @@ -78,15 +96,30 @@ function DailyData() { }; }, [open]); + const handleSubmit = async (event) => { + event.preventDefault(); // Prevent default form submission behavior (e.g., page reload) + + // Clear any existing messages before processing the form + // setSuccessMessage(''); + // setErrorMessage(''); + + try { + await apiClient.post("/enter-daily-data", formData); + console.log("Daily entry processed") + // setSuccessMessage('Profile updated!'); + } catch (err) { + console.log("Error submitting daily entry", err); + // setErrorMessage('Error: Failed to update profile. Please try again'); + } + }; + return ( Enter your data here: - {/* - {date} - */} +
setWeight(e.target.value)} + name="weight" + value={formData.weight} + onChange={handleChange} InputProps={{ endAdornment: ( @@ -141,8 +175,9 @@ function DailyData() { label="Count" variant="filled" fullWidth - value={steps} - onChange={(e) => setSteps(e.target.value)} + name="steps" + value={formData.steps} + onChange={handleChange} InputProps={{ endAdornment: ( @@ -161,8 +196,9 @@ function DailyData() { label="Sleep" variant="filled" fullWidth - value={sleep} - onChange={(e) => setSleep(e.target.value)} + name="sleep" + value={formData.sleep} + onChange={handleChange} InputProps={{ endAdornment: ( @@ -182,8 +218,9 @@ function DailyData() { label="Water" variant="filled" fullWidth - value={water} - onChange={(e) => setWater(e.target.value)} + name="water" + value={formData.water} + onChange={handleChange} InputProps={{ endAdornment: ( @@ -203,15 +240,10 @@ function DailyData() { - setExerciseTime({ - ...exerciseTime, - exerciseTimeValue: e.target.value, - }) - } + value={formData.exercise} + onChange={handleChange} required InputLabelProps={{ sx: inputLable, @@ -224,19 +256,6 @@ function DailyData() { /> - {/* How long did you exercise */} - {/* */} - {/* Mood */} {/* Mood @@ -461,7 +480,15 @@ function DailyData() { */} + + {/* Submit Button */} + + + +
); } diff --git a/code/server/db.js b/code/server/db.js index fd4d690e4..fccbfc046 100644 --- a/code/server/db.js +++ b/code/server/db.js @@ -49,6 +49,19 @@ let userSchema = new Schema({ collection: 'users' }) +let dailyEntrySchema = new Schema({ + dailyEntryId: { type: Number, required: true }, + userId: { type: Number, required: true }, + entryDate: { type: Date }, + weight: { type: Number }, + steps: { type: Number }, + sleep: { type: Number }, + water: { type: Number }, + exercise: { type: Number } +}, { + collection: 'daily_entries' +}) + module.exports = { getModel: () => { if (connection == null) { @@ -56,7 +69,8 @@ module.exports = { console.log("Connected to MongoDB!") userModel = connection.model("User", userSchema); goalModel = connection.model("Goal", goalSchema); - models = {userModel: userModel, goalModel: goalModel} + dailyEntryModel = connection.model("DailyEntry", dailyEntrySchema); + models = {userModel: userModel, goalModel: goalModel, dailyEntryModel: dailyEntryModel} } return models }, diff --git a/code/server/initdb.js b/code/server/initdb.js index 10892ec70..4bca6850e 100644 --- a/code/server/initdb.js +++ b/code/server/initdb.js @@ -4,12 +4,14 @@ const db = require("./db.js") const User = db.getModel().userModel const Goal = db.getModel().goalModel +const DailyEntry = db.getModel().dailyEntryModel async function init() { try { // Option to delete existing data for full reset // await User.deleteMany({}) // await Goal.deleteMany({}) + // await DailyEntry.deleteMany({}) let goal1 = new Goal({ goalId: 50001, @@ -46,12 +48,24 @@ async function init() { // createdAt: "2024-09-01", // testing the default param in schema works goals: [] }) + + let dailyEntry1 = new DailyEntry({ + dailyEntryId: 1, + userId: 10001, + entryDate: "2024-10-04", + weight: 150, + steps: 9999, + sleep: 7.5, + water: 4, + exercise: 45 + }) await Promise.all([ user1.save(), user2.save(), goal1.save(), - goal2.save() + goal2.save(), + dailyEntry1.save() ]) let users = await User.find({}) @@ -60,6 +74,9 @@ async function init() { let goals = await Goal.find({}) console.log(goals) + let dailyEntries = await DailyEntry.find({}) + console.log(dailyEntries) + } catch(error) { console.log(error) } finally { diff --git a/code/server/server.js b/code/server/server.js index 5c455a121..2290c3b26 100644 --- a/code/server/server.js +++ b/code/server/server.js @@ -10,6 +10,7 @@ const app = express(); const port = process.env.PORT || 5000 const User = db.getModel().userModel const Goal = db.getModel().goalModel +const DailyEntry = db.getModel().dailyEntryModel const bcrypt = require('bcrypt'); const jwt = require('jsonwebtoken'); @@ -166,6 +167,37 @@ app.post('/create-goal', async (req, res) => { } }) + +app.post('/enter-daily-data', async (req, res) => { + + try { + const { weight, steps, sleep, water, exercise } = req.body; + + // generate new ID based on max goal ID + let lastEntry = await DailyEntry.find().sort({"dailyEntryId": -1}).limit(1) + let newEntryId = lastEntry[0].dailyEntryId + 1 + + const newDailyEntry = new DailyEntry({ + dailyEntryId: newEntryId, + userId: 10001, // UPDATE TO USERID BASED ON WHO IS LOGGED IN + entryDate: "2024-10-04", // TO DO: FIGURE OUT HOW TO GRAB THIS FROM FORM + weight: weight, + steps: steps, + sleep: sleep, + water: water, + exercise: exercise + }) + + await newDailyEntry.save(); + res.status(201).json({ message: 'Daily entry created successfully!' }); + + } catch (error) { + res.status(500).json({ message: 'Error creating daily entry' }); + } + +}) + + //Listen for incoming connections app.listen(port, () => { console.log(`Server running on port ${port}`);