diff --git a/app/components/ChartModal.js b/app/components/ChartModal.js index 2e10f55..5316713 100644 --- a/app/components/ChartModal.js +++ b/app/components/ChartModal.js @@ -1,122 +1,111 @@ "use client"; -import { CartesianGrid, Line, LineChart, ResponsiveContainer, XAxis, YAxis } from 'recharts'; -import { calculateAverageIntake, calculateDayAverageLoss, sortEntries } from '../constants'; -import { Button, Stack, TextField } from '@mui/material'; -import { useState } from 'react'; +import { + calculateAverageIntake, + calculateDayAverageLoss, + sortEntries, +} from "../constants"; +import { Button, Stack, TextField, Typography } from "@mui/material"; +import { useState } from "react"; +import Graph from "./Graph"; +import { getCharts } from "../charts"; export default function ChartModal({ user }) { + const [show, setShow] = useState(false); + const [daysToGraph, setDaysToGraph] = useState(30); + const [daysToAverage, setDaysToAverage] = useState(7); - const [show, setShow] = useState(false); - const [days, setDays] = useState(30); - - const data = []; - var entries = []; - var lastEntry = {}; + const data = []; + var entries = []; - if (days) { - const lastDayEntries = user.entries.slice(0, days); - entries = sortEntries(lastDayEntries, true); - for (var i = 1; i < entries.length; i++) { - const curEntry = entries[i - 1]; - const nextEntry = entries[i]; - - const curWeekWeight = calculateDayAverageLoss(user, curEntry, 7); - const nextWeekWeight = calculateDayAverageLoss(user, nextEntry, 7); - - const curWeekCalories = calculateAverageIntake(user, curEntry, 7); - const nextWeekCalories = calculateAverageIntake(user, nextEntry, 7); - - const prevDataWeightVariance = data.length > 0 ? data[i - 2].weightVariance : 0; - const prevDataCaloricVariance = data.length > 0 ? data[i - 2].caloricVariance : 0; - const prevWeightChange = data.length > 0 ? data[i - 2].weightChange : 0; - const prevCaloricChange = data.length > 0 ? data[i - 2].caloricChange : 0; - - data.push({ - date: curEntry.date, - weightChange: ((nextWeekWeight - curWeekWeight)) + prevWeightChange, - caloricChange: ((nextWeekCalories - curWeekCalories)) + prevCaloricChange, - weightVariance: ((nextWeekWeight - curWeekWeight)), - caloricVariance: ((nextWeekCalories - curWeekCalories)), - }); - } + if (daysToGraph && daysToAverage) { + const lastDayEntries = user.entries.slice(0, daysToGraph); + entries = sortEntries(lastDayEntries, true); + for (var i = 1; i < entries.length; i++) { + const curEntry = entries[i - 1]; + const nextEntry = entries[i]; - lastEntry = entries[0]; - } - - if (user.entries.length <= 1) { - return; - } + const curWeight = parseFloat( + calculateDayAverageLoss(user, curEntry, daysToAverage) + ); + const nextWeight = parseFloat( + calculateDayAverageLoss(user, nextEntry, daysToAverage) + ); - return ( - <> - - {show && - <> - - setDays(parseInt(e.target.value))} name="steps" type="number" label="Days" value={days} /> - -
Weight Average
- - - - - - - - + const curCalories = parseFloat( + calculateAverageIntake(user, curEntry, daysToAverage) + ); + const nextCalories = parseFloat( + calculateAverageIntake(user, nextEntry, daysToAverage) + ); -
Weight Change
- - - - - - - - + // i - 2 because i starts at 1 + const prevWeightChange = + data.length > 0 ? data[i - 2].totalWeightChange : 0; + const prevCaloricChange = + data.length > 0 ? data[i - 2].totalCaloricChange : 0; -
Weight Variance
- - - - - - - - + data.push({ + date: curEntry.date, + totalWeightChange: nextWeight - curWeight + prevWeightChange, + totalCaloricChange: nextCalories - curCalories + prevCaloricChange, + dailyWeightChange: nextWeight - curWeight, + dailyCaloricChange: nextCalories - curCalories, + }); + } + data.forEach((e) => { + e.totalWeightChange = e.totalWeightChange.toFixed(2); + e.dailyWeightChange = e.dailyWeightChange.toFixed(2); + }); + } -
Caloric Average
- - - - - - - - + if (user.entries.length <= 1) { + return; + } -
Caloric Change
- - - - - - - - + return ( + <> + + {show && ( + <> + + setDaysToGraph(parseInt(e.target.value))} + name="steps" + type="number" + label="Number of Days to show on Graph" + value={daysToGraph} + /> + setDaysToAverage(parseInt(e.target.value))} + name="steps" + type="number" + label="Number of Days used to average your Weight" + value={daysToAverage} + /> + -
Caloric Variance
- - - - - - - - - - } + {getCharts(data, entries).map((c, i) => ( + + ))} - ) + )} + + ); } diff --git a/app/components/DayEntryEdit.js b/app/components/DayEntryEdit.js index 985c2da..7a42bfd 100644 --- a/app/components/DayEntryEdit.js +++ b/app/components/DayEntryEdit.js @@ -19,10 +19,12 @@ export default function DayEntryEdit({ user, entry, setEntry, open, hideEdit, sa setDateRequired(!entry.date); if (entry.date) { - const dateAlreadyExists = user.entries.map(e => e.date).indexOf(entry.date) > -1; - if (dateAlreadyExists) { - alert('Date ' + entry.date + ' has already been logged.'); - return; + if (!entry.id) { + const dateAlreadyExists = user.entries.map(e => e.date).indexOf(entry.date) > -1; + if (dateAlreadyExists) { + alert('Date ' + entry.date + ' has already been logged.'); + return; + } } save(); hideEdit();