Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Commit

Permalink
Add carbs and fats to calculator (major)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorge Cuadra committed Sep 1, 2018
1 parent 9dda1c7 commit 1be66e5
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 15 deletions.
14 changes: 6 additions & 8 deletions ROADMAP.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Roadmap

- [ ] add carbs and fats to calculator and generator
- [ ] fix import/export torrent

## Plan

- [ ] lock specific menu items

## Forms

- [ ] validate inputs
Expand All @@ -18,14 +21,9 @@
- https://ndb.nal.usda.gov/ndb/doc/index
- https://developer.nutritionix.com/

## graphs

- force graph from uber

## Icebox

- [ ] offline PWA
- [ ] fix multi-undo removeRecipe
- [ ] menu plan like trello https://github.com/STRML/react-grid-layout
- [ ] lock specific menu items
- [ ] drag n drop rearrangement of servings as breakfast, lunch, dinner, snacks
- [ ] fix multi-undo removeRecipe
- [ ] offline PWA
13 changes: 11 additions & 2 deletions src/Layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,18 @@ class Layout extends Component {

handleMenuGenerate() {
const { state } = this;
const { CALORIES_TOTAL, PROTEIN_TOTAL } = getIn(state, 'settings', {});
const { CALORIES_TOTAL, CARBS_TOTAL, FAT_TOTAL, PROTEIN_TOTAL } = getIn(
state,
'settings',
{},
);

const settings = calculateSettings({ CALORIES_TOTAL, PROTEIN_TOTAL });
const settings = calculateSettings({
CALORIES_TOTAL,
CARBS_TOTAL,
FAT_TOTAL,
PROTEIN_TOTAL,
});

const { menu, calories, carbs, fat, protein } = calculateDayMenu({
recipes: state.recipes.sort(randomSort),
Expand Down
19 changes: 16 additions & 3 deletions src/calculator/Calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import { Select } from 'rmwc';
import { TextField, TextFieldHelperText } from 'rmwc/TextField';
import { Typography } from 'rmwc/Typography';

import { calculateCaloriesTotal, calculateProteinTotal } from './utils';
import {
calculateCaloriesTotal,
calculateCarbsTotal,
calculateFatTotal,
calculateProteinTotal,
} from './utils';
import { macroOptions, kinobodyProgramModeOptions } from './constants';
import { withCoreContext } from '../CoreContext';
class Calculator extends React.Component {
Expand All @@ -34,16 +39,24 @@ class Calculator extends React.Component {
})}
onSubmit={(values, { setSubmitting }) => {
const CALORIES_TOTAL = calculateCaloriesTotal(values);
const FAT_TOTAL = calculateFatTotal(CALORIES_TOTAL);
const PROTEIN_TOTAL = calculateProteinTotal(values, CALORIES_TOTAL);
const CARBS_TOTAL = calculateCarbsTotal({
caloriesTotal: CALORIES_TOTAL,
fatTotal: FAT_TOTAL,
proteinTotal: PROTEIN_TOTAL,
});

handleCalculatorUpdate({
...values,
bodyWeight: parseInt(values.bodyWeight),
CALORIES_TOTAL,
CARBS_TOTAL,
FAT_TOTAL,
PROTEIN_TOTAL,
});
setSubmitting(false);
this.setState({ isSaved: true });

this.setState({ isSaved: true }, () => setSubmitting(false));
}}
render={({
errors,
Expand Down
18 changes: 18 additions & 0 deletions src/calculator/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,21 @@ export function calculateProteinTotal(
const proteinFactor = macroValues[kinobodyMacroOption] || caloriesTotal * 0.3;
return parseInt(bodyWeight, 10) * coeficient * proteinFactor;
}

export const FAT_CALORIES_PER_GRAM = 9;
export const CARBS_CALORIES_PER_GRAM = 4;
export const PROTEIN_CALORIES_PER_GRAM = 4;

export function calculateCarbsTotal({ caloriesTotal, fatTotal, proteinTotal }) {
const caloriesRemaining =
caloriesTotal -
fatTotal * FAT_CALORIES_PER_GRAM -
proteinTotal * PROTEIN_CALORIES_PER_GRAM;
const carbsTotal = caloriesRemaining / CARBS_CALORIES_PER_GRAM;
return carbsTotal;
}

export function calculateFatTotal(caloriesTotal) {
const fatTotal = (caloriesTotal * 0.25) / FAT_CALORIES_PER_GRAM;
return fatTotal;
}
26 changes: 24 additions & 2 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
const { List } = require('immutable');

const CALORIES_TOLERANCE = 50; // calories
const CARBS_TOLERANCE = 10; // grams
const FAT_TOLERANCE = 10; // grams
const PROTEIN_TOLERANCE = 10; // grams

export function calculateSettings({ CALORIES_TOTAL, PROTEIN_TOTAL }) {
export function calculateSettings({
CALORIES_TOTAL,
CARBS_TOTAL,
FAT_TOTAL,
PROTEIN_TOTAL,
}) {
return {
CALORIES_LOWER_BOUND: CALORIES_TOTAL - CALORIES_TOLERANCE,
CALORIES_UPPER_BOUND: CALORIES_TOTAL + CALORIES_TOLERANCE,
CARBS_LOWER_BOUND: CARBS_TOTAL - CARBS_TOLERANCE,
CARBS_UPPER_BOUND: CARBS_TOTAL + CARBS_TOLERANCE,
FAT_LOWER_BOUND: FAT_TOTAL - FAT_TOLERANCE,
FAT_UPPER_BOUND: FAT_TOTAL + FAT_TOLERANCE,
PROTEIN_LOWER_BOUND: PROTEIN_TOTAL - PROTEIN_TOLERANCE,
PROTEIN_UPPER_BOUND: PROTEIN_TOTAL + PROTEIN_TOLERANCE,
};
Expand Down Expand Up @@ -36,11 +47,22 @@ export function calculateDayMenu({
calories < settings.CALORIES_LOWER_BOUND ||
settings.CALORIES_UPPER_BOUND < calories;

const isCarbsOutOfBounds =
protein < settings.CARBS_LOWER_BOUND ||
settings.CARBS_UPPER_BOUND < carbs;

const isFatOutOfBounds =
protein < settings.FAT_LOWER_BOUND || settings.FAT_UPPER_BOUND < fat;

const isProteinOutOfBounds =
protein < settings.PROTEIN_LOWER_BOUND ||
settings.PROTEIN_UPPER_BOUND < protein;

const shouldRestart = isCaloriesOutOfBounds || isProteinOutOfBounds;
const shouldRestart =
isCaloriesOutOfBounds ||
isCarbsOutOfBounds ||
isFatOutOfBounds ||
isProteinOutOfBounds;

if (shouldRestart) {
return calculateDayMenu({
Expand Down

0 comments on commit 1be66e5

Please sign in to comment.