From a8dc4ef6936d883c151955ced68127b07bb4efec Mon Sep 17 00:00:00 2001 From: Brendan Birdsong Date: Wed, 27 Sep 2023 10:52:13 -0500 Subject: [PATCH] bug fixes --- app/components/ChartModal.js | 49 +++++++++++++++++++----------------- app/constants.js | 2 +- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/app/components/ChartModal.js b/app/components/ChartModal.js index a6f4b1c..ebfbc7c 100644 --- a/app/components/ChartModal.js +++ b/app/components/ChartModal.js @@ -11,30 +11,33 @@ export default function ChartModal({ user }) { const [days, setDays] = useState(30); const data = []; - const lastDayEntries = user.entries.slice(0, days); - const 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 (days) { + const lastDayEntries = user.entries.slice(0, days); + const 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 (user.entries.length <= 1) { diff --git a/app/constants.js b/app/constants.js index e69f799..60e9b77 100644 --- a/app/constants.js +++ b/app/constants.js @@ -26,7 +26,7 @@ export const calculateDayMetabolicRate = (u, entry) => { const bodyFatPercentage = u.bodyFatPercentage / 100; const fatMass = bodyWeightKg * bodyFatPercentage; const leanBodyMass = bodyWeightKg - fatMass; - const gender = u.gender === 'M' ? 1 : 2; + const gender = u.gender === 'M' ? 1 : 0; const age = moment().diff(u.birthDate, 'years'); const baseCals = (13.587 * leanBodyMass) + (9.613 * fatMass) + (198 * gender) - (3.351 * age) + 674; const baseWithActivity = baseCals * (u.activityModifier);