From 32053d9ff9c8a3e48ee65c0397b36f51372bf80d Mon Sep 17 00:00:00 2001 From: Ava DeCroix Date: Mon, 1 May 2023 14:18:19 -0400 Subject: [PATCH] fixed search and added routes for past --- db_app/src/components/LogMeals.js | 71 ++------- db_app/src/components/Past.js | 242 ++++++++++++++++++------------ db_app/src/components/ThisWeek.js | 112 ++++---------- 3 files changed, 186 insertions(+), 239 deletions(-) diff --git a/db_app/src/components/LogMeals.js b/db_app/src/components/LogMeals.js index 2d2adba..4728460 100644 --- a/db_app/src/components/LogMeals.js +++ b/db_app/src/components/LogMeals.js @@ -80,84 +80,39 @@ function weekStart(){ const net_id = ReactSession.get("net_id"); -//to set nutritional goal for the week -const [goalInput, setGoalInput] = useState({ - total_cal: 0, - total_fat: 0, - total_sat_fat: 0, - total_trans_fat: 0, - total_carbs: 0, - total_fiber: 0, - total_sugar: 0, - total_protein: 0, - total_sodium: 0, - total_potassium: 0, - total_cholesterol: 0, - } -); -const{total_cal, total_fat, total_sat_fat, total_trans_fat, total_carbs, total_fiber, - total_sugar, total_protein, total_sodium, total_potassium, total_cholesterol} = goalInput - -const changeGoalHandler = evt =>{ - setGoalInput({...goalInput, [evt.target.name]: [evt.target.value] }) -} - -const submitGoalHandler = evt => { - evt.preventDefault(); - console.log(goalInput) - console.log(net_id) - console.log(weekStart()) - Axios.post("http://3.219.93.142:8000/api/goal", - { - net_id: net_id, - week_date: weekStart(), - total_cal: Number(total_cal[0]), - total_fat: Number(total_fat[0]), - total_sat_fat: Number(total_sat_fat[0]), - total_trans_fat: Number(total_trans_fat[0]), - total_carbs: Number(total_carbs[0]), - total_fiber: Number(total_fiber[0]), - total_sugar: Number(total_sugar[0]), - total_protein: Number(total_protein[0]), - total_sodium: Number(total_sodium[0]), - total_potassium: Number(total_potassium[0]), - total_cholesterol: Number(total_cholesterol[0]) - }).then((response) => { - console.log(response); - console.log(response.status); - }) -}; //to find a food item from an on campus location to your weekly journal const [keyword, setKeyword] = useState({ search_term:"" }) -const [searchItems, setSearchItems] = useState([{}]); +const [searchItems, setSearchItems] = useState([]); +const [sendItems, setSendItems] = useState([{}]); + const{search_term} = keyword const removeItem = (index) => { - setSearchItems([ - ...searchItems.slice(0, index), - ...searchItems.slice(index + 1) + setSendItems([ + ...sendItems.slice(0, index), + ...sendItems.slice(index + 1) ]); } function handleCheck (i) { console.log(i); - if (searchItems.indexOf(i) > -1){ + if (sendItems.indexOf(i) > -1){ //get index and delete - var index = searchItems.indexOf(i) + var index = sendItems.indexOf(i) removeItem(index); console.log(`removed ${i}`); } else{ - setSearchItems(searchItems => [...searchItems, i]); + setSendItems(sendItems => [...sendItems, i]); console.log(`added ${i}`); } @@ -178,8 +133,9 @@ const submitSearchHandler = evt => { }).then((response) => { console.log(response); console.log(response.status); - console.log(response.data); - setSearchItems(response.data); + console.log('Data:') + console.log(response.data.results); + setSearchItems(response.data.results); }) }; @@ -335,9 +291,6 @@ color: 'main', onChange={() => handleCheck(searchitem.item_id)} /> - - {searchitem.item_name} - {searchitem.eatery_id} {searchitem.item_name} diff --git a/db_app/src/components/Past.js b/db_app/src/components/Past.js index 42a04f5..7054ef5 100644 --- a/db_app/src/components/Past.js +++ b/db_app/src/components/Past.js @@ -1,4 +1,4 @@ -import React,{useState} from 'react'; +import React,{useState, useEffect} from 'react'; import {Routes, Route, useNavigate} from 'react-router-dom'; import './Login.css'; import Button from "@mui/material/Button"; @@ -22,7 +22,7 @@ import MenuItem from '@mui/material/MenuItem'; import {red, green, lightBlue, lightGreen} from '@mui/material/colors'; import { ThemeProvider, createTheme } from '@mui/material/styles'; import { ReactSession } from 'react-client-session'; -import { Axios } from 'axios'; +import Axios from 'axios'; import Chip from '@mui/material/Chip'; import Stack from '@mui/material/Stack'; import Select, { SelectChangeEvent } from '@mui/material/Select'; @@ -94,78 +94,43 @@ function weekStart(){ const net_id = ReactSession.get("net_id"); -//to set nutritional goal for the week -const [goalInput, setGoalInput] = useState({ - total_cal: "", - total_fat: "", - total_sat_fat: "", - total_trans_fat: "", - total_carbs: "", - total_fiber: "", - total_sugar: "", - total_protein: "", - total_sodium: "", - total_potassium: "", - total_cholesterol: "", + //Get history of plans for this week + const [pastPlans, setPastPlans] = useState([{}]); + const makePastURL = (net_id) => `http://3.219.93.142:8000/api/${net_id}`; + + const getPastPlans = () => { + const net_id = ReactSession.get("net_id"); + const url_to_query = makePastURL(net_id); + Axios.get(url_to_query).then((response) => { + console.log(response.data); + setPastPlans(response.data); + }); + } -); + //Get history of actual totals for weekly plan (progress) + const [past, setPast] = useState([{}]); + const makeURL = (net_id) => `http://3.219.93.142:8000/api/${net_id}`; + + const getPast = () => { + const net_id = ReactSession.get("net_id"); + const url_to_query = makeURL(net_id); + Axios.get(url_to_query).then((response) => { + console.log(response.data); + setPast(response.data); + }); + + } -const{total_cal, total_fat, total_sat_fat, total_trans_fat, total_carbs, total_fiber, - total_sugar, total_protein, total_sodium, total_potassium, total_cholesterol} = goalInput - -const changeGoalHandler = evt =>{ - setGoalInput({...goalInput, [evt.target.name]: [evt.target.value] }) -} - -const submitGoalHandler = evt => { - evt.preventDefault(); - console.log(goalInput) - Axios.post("http://3.219.93.142:8000/api/", - { - total_cal: total_cal[0], - total_fat: total_fat[0], - total_sat_fat: total_sat_fat[0], - total_trans_fat: total_trans_fat[0], - total_carbs: total_carbs[0], - total_fiber: total_fiber[0], - total_sugar: total_sugar[0], - total_protein: total_protein[0], - total_sodium: total_sodium[0], - total_potassium: total_potassium[0], - total_cholesterol: total_cholesterol[0] - }).then((response) => { - console.log(response); - console.log(response.status); - }) -}; + //run getPast and pastPlans on page load + useEffect(() => { + getPast() + console.log('Past actual in') + getPastPlans() + console.log('Past plans in') + }, []) -//to add an off campus food item or meal to your weekly journal - const [offCampusInput, setOffCampusInput] = useState({ - calories: "", - fat_g: "", - sat_fat_g: "", - trans_fat_g: "", - carbs_g: "", - fiber_g: "", - sugar_g: "", - protein_g: "", - sodium_g: "", - potassium_g: "", - cholesterol_g: "", - } - ); - const { calories, fat_g, sat_fat_g, trans_fat_g, carbs_g, fiber_g,sugar_g, protein_g, - sodium_g, potassium_g, cholesterol_g, } = offCampusInput - - const changeoffCampusHandler = evt => { - setOffCampusInput({ ...offCampusInput, [evt.target.name]: [evt.target.value] }) - } - - const submitoffCampusHandler = evt => { - evt.preventDefault(); - }; @@ -200,48 +165,129 @@ const submitGoalHandler = evt => {

  - Past plans: + Your History

- - +

+     + Past plans by week: +

+ + + +
- - Food - Calories - Fat (g) - Saturated Fat (g) - TransFat (g) - Carbs (g) - Fiber (g) - Sugar (g) - Protein (g) - Sodium (mg) - Potassium (mg) - Cholesterol (mg) + + + + Week + Calories + Fat (g) + Saturated Fat (g) + TransFat (g) + Carbs (g) + Fiber (g) + Sugar (g) + Protein (g) + Sodium (mg) + Potassium (mg) + Cholesterol (mg) - - - - - - - - - - - - - + + {pastPlans.map((pastplan, i) => { + console.log(i); + return( + + + + {pastplan.week} + + + {pastplan.calories} + {pastplan.fat_g} + {pastplan.sat_fat_g} + {pastplan.trans_fat_g} + {pastplan.carbs_g} + {pastplan.fiber_g} + {pastplan.sugar_g} + {pastplan.protein_g} + {pastplan.sodium_mg} + {pastplan.potassium_mg} + {pastplan.cholesterol_mg} + ) + })}
+ + +

+     + Past actual by week: +

+ + + + + + + + + Week + Calories + Fat (g) + Saturated Fat (g) + TransFat (g) + Carbs (g) + Fiber (g) + Sugar (g) + Protein (g) + Sodium (mg) + Potassium (mg) + Cholesterol (mg) + + + + + {past.map((progress, i) => { + console.log(i); + return( + + + + {progress.week} + + + {progress.calories} + {progress.fat_g} + {progress.sat_fat_g} + {progress.trans_fat_g} + {progress.carbs_g} + {progress.fiber_g} + {progress.sugar_g} + {progress.protein_g} + {progress.sodium_mg} + {progress.potassium_mg} + {progress.cholesterol_mg} + + ) + })} + +
+
+
diff --git a/db_app/src/components/ThisWeek.js b/db_app/src/components/ThisWeek.js index fc0f7d3..045e909 100644 --- a/db_app/src/components/ThisWeek.js +++ b/db_app/src/components/ThisWeek.js @@ -94,79 +94,8 @@ function weekStart(){ const net_id = ReactSession.get("net_id"); -//to set nutritional goal for the week -const [goalInput, setGoalInput] = useState({ - total_cal: "", - total_fat: "", - total_sat_fat: "", - total_trans_fat: "", - total_carbs: "", - total_fiber: "", - total_sugar: "", - total_protein: "", - total_sodium: "", - total_potassium: "", - total_cholesterol: "", - } -); - -const{total_cal, total_fat, total_sat_fat, total_trans_fat, total_carbs, total_fiber, - total_sugar, total_protein, total_sodium, total_potassium, total_cholesterol} = goalInput - -const changeGoalHandler = evt =>{ - setGoalInput({...goalInput, [evt.target.name]: [evt.target.value] }) -} - -const submitGoalHandler = evt => { - evt.preventDefault(); - console.log(goalInput) - Axios.post("http://3.219.93.142:8000/api/", - { - total_cal: total_cal[0], - total_fat: total_fat[0], - total_sat_fat: total_sat_fat[0], - total_trans_fat: total_trans_fat[0], - total_carbs: total_carbs[0], - total_fiber: total_fiber[0], - total_sugar: total_sugar[0], - total_protein: total_protein[0], - total_sodium: total_sodium[0], - total_potassium: total_potassium[0], - total_cholesterol: total_cholesterol[0] - }).then((response) => { - console.log(response); - console.log(response.status); - }) -}; - - -//to add an off campus food item or meal to your weekly journal - const [offCampusInput, setOffCampusInput] = useState({ - calories: "", - fat_g: "", - sat_fat_g: "", - trans_fat_g: "", - carbs_g: "", - fiber_g: "", - sugar_g: "", - protein_g: "", - sodium_g: "", - potassium_g: "", - cholesterol_g: "", - } - ); - - const { calories, fat_g, sat_fat_g, trans_fat_g, carbs_g, fiber_g,sugar_g, protein_g, - sodium_g, potassium_g, cholesterol_g, } = offCampusInput - - const changeoffCampusHandler = evt => { - setOffCampusInput({ ...offCampusInput, [evt.target.name]: [evt.target.value] }) - } - - const submitoffCampusHandler = evt => { - evt.preventDefault(); - }; + //Get history of items for this week const [pastItems, setPastItems] = useState([{}]); const makeURL = (net_id) => `http://3.219.93.142:8000/api/week_progress/${net_id}`; @@ -179,28 +108,47 @@ const submitGoalHandler = evt => { }); } - useEffect(() => { - getHistory() - console.log('History in') - }, []) - - const [sum, setSum] = useState([{}]); - const sumURL = (net_id) => `http://3.219.93.142:8000/api/week_sum/${net_id}`; + //Get sum of totals for weekly plan (progress) + const [goals, setGoals] = useState([{}]); + const goalURL = (net_id) => `http://3.219.93.142:8000/api/week_goal/${net_id}`; - const getSum = () => { + const getGoal = () => { const net_id = ReactSession.get("net_id"); - const url_to_query = sumURL(net_id); + const url_to_query = goalURL(net_id); Axios.get(url_to_query).then((response) => { console.log(response.data); - setSum(response.data); + setGoals(response.data); }); } + + //Get weekly plan goals + const [sum, setSum] = useState([{}]); + const sumURL = (net_id) => `http://3.219.93.142:8000/api/week_sum/${net_id}`; + + const getSum = () => { + const net_id = ReactSession.get("net_id"); + const url_to_query = sumURL(net_id); + Axios.get(url_to_query).then((response) => { + console.log(response.data); + setSum(response.data); + }); + + } + + + const [cals, setCals] = useState([{}]); + const [fat, setFat] = useState([{}]); + const [trans, setTrans] = useState([{}]); + + //Run getSum, getHistory, and getPlan on page load useEffect(() => { getHistory() console.log('History in') getSum() console.log('Sum in') + getGoal() + console.log('Goal in') }, [])