Skip to content

Commit

Permalink
disables scaling controls when no recipe is loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
klalicki committed Dec 14, 2023
1 parent 8c39b70 commit 8d14350
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/components/TargetScaleSetter/TargetScaleSetter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useContext, useEffect, useState } from "react";
import { ScalerContext, ScalerContextType } from "../../context/ScalerContext";
import { NumberInput } from "@mantine/core";
export const TargetScaleSetter = () => {
const { setTargetWeight, targetWeight } = useContext(
const { setTargetWeight, targetWeight, isRecipeLoaded } = useContext(
ScalerContext
) as ScalerContextType;

Expand All @@ -27,12 +27,14 @@ export const TargetScaleSetter = () => {
value={unitCount}
label={"Number of units (pizza, loaf, etc)"}
onChange={setUnitCount}
disabled={!isRecipeLoaded}
/>{" "}
<NumberInput
className=" w-60"
value={unitWeight}
label={"Dough Weight per unit (g)"}
onChange={setUnitWeight}
disabled={!isRecipeLoaded}
/>
<NumberInput
className=" w-40"
Expand All @@ -43,6 +45,7 @@ export const TargetScaleSetter = () => {
value={percentOver}
label={"Percent Overage"}
onChange={setPercentOver}
disabled={!isRecipeLoaded}
/>
</div>
<p aria-live="polite">total recipe weight: {targetWeight}g</p>
Expand Down
11 changes: 8 additions & 3 deletions src/context/ScalerContext.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PropsWithChildren, createContext, useEffect, useState } from "react";
import { sampleData } from "../data/sampleData";
// import { sampleData } from "../data/sampleData";
import {
ZRecipeData,
RecipeData,
Expand All @@ -15,6 +15,7 @@ export type ScalerContextType = {
ingredientList: RecipeIngredient[];
schemaErrorText: string;
loadFromJSON: (jsonData: string) => void;
isRecipeLoaded: boolean;
};

export const ScalerContext = createContext<ScalerContextType | null>(null);
Expand All @@ -26,17 +27,18 @@ export const ScalerWrapper = (props: PropsWithChildren) => {
const [targetWeight, setTargetWeight] = useState(3102);
const [ingredientList, setIngredientList] = useState<RecipeIngredient[]>([]);
const [schemaErrorText, setSchemaErrorText] = useState("");
const [isRecipeLoaded, setIsRecipeLoaded] = useState(false);

const loadFromJSON = (jsonData: string) => {
try {
const newDataObj = JSON.parse(jsonData);
validateAndLoad(newDataObj);
// setSchemaErrorText("");
} catch {
setSchemaErrorText(
"Error loading the recipe file. File is not readable as a JSON file"
);
}
console.log(jsonData);
};

const validateAndLoad = (newData: any) => {
Expand All @@ -57,6 +59,7 @@ export const ScalerWrapper = (props: PropsWithChildren) => {
// handles calculating the total weight of the original recipe for the scaling function

const loadRecipe = (newRecipe: RecipeData) => {
setSchemaErrorText("");
//add ingredient dictionary to context
setIngredientList(newRecipe.ingredients);
// calculate the total weight of the original recipe
Expand All @@ -74,6 +77,7 @@ export const ScalerWrapper = (props: PropsWithChildren) => {
setRecipe(newRecipe);
setRecipeWeight(totalWeight);
scaleRecipe();
setIsRecipeLoaded(true);
// console.log(scaledRecipe);
};
// scales the recipe to the new target weight and saves it to scaledRecipe
Expand All @@ -90,7 +94,7 @@ export const ScalerWrapper = (props: PropsWithChildren) => {
setScaledRecipe({ ...recipe, steps: newSteps });
};
useEffect(() => {
validateAndLoad(sampleData);
// validateAndLoad(sampleData);
scaleRecipe();
console.log(scaledRecipe);
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand All @@ -105,6 +109,7 @@ export const ScalerWrapper = (props: PropsWithChildren) => {
ingredientList,
schemaErrorText,
loadFromJSON,
isRecipeLoaded,
}}
>
{props.children}
Expand Down

0 comments on commit 8d14350

Please sign in to comment.