Skip to content

Commit

Permalink
better implementation of removing and adding items on list
Browse files Browse the repository at this point in the history
  • Loading branch information
BramDecuypere committed Mar 8, 2023
1 parent bca29ef commit ca0609b
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 64 deletions.
126 changes: 97 additions & 29 deletions imports/api/users/methods/active-list-actions.users.method.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { getExtraIngredientAmountByName } from "./../../../ui/organisms/groceries-list/utils/get-extra-ingredient-amount-by-name";
import "./default-recipes.users.method";
import "./default-settings.users.method";

import { Meteor } from "meteor/meteor";
import { Recipe, RecipeIngredient } from "../../recipes/recipes";
import { checkIsIngredientComplete } from "/imports/ui/organisms/groceries-list/utils/is-ingredient-complete";
import { getTotalRecipeAmountByName } from "/imports/ui/organisms/groceries-list/utils/get-total-recipe-amount";

Meteor.methods({
"users.activeList.completeIngredients"(recipeIngredient: RecipeIngredient) {
Expand All @@ -22,10 +25,42 @@ Meteor.methods({
const activeList = { ...currentActiveList };

if (isIngredientSelected) {
activeList.selectedIngredients = activeList.selectedIngredients.filter(
({ name }) => name !== recipeIngredient.name
const isIngredientComplete = checkIsIngredientComplete(
currentActiveList,
recipeIngredient.name
);

if (isIngredientComplete) {
activeList.selectedIngredients = activeList.selectedIngredients.filter(
({ name }) => name !== recipeIngredient.name
);
} else {
activeList.selectedIngredients = activeList.selectedIngredients.map(
(_ingredient) => {
if (_ingredient.name === recipeIngredient.name) {
const recipeAmount = getTotalRecipeAmountByName(
activeList,
recipeIngredient.name
);
const extraIngredientAmount = getExtraIngredientAmountByName(
activeList,
recipeIngredient.name
);

const amount = recipeAmount + extraIngredientAmount;

return {
..._ingredient,
amount,
};
}

return _ingredient;
}
);
}
} else {
console.log("adding recipe ingredient", recipeIngredient);
activeList.selectedIngredients = [
...activeList.selectedIngredients,
recipeIngredient,
Expand Down Expand Up @@ -100,32 +135,57 @@ Meteor.methods({
fields: { activeList: 1 },
}) as Meteor.User;

const isFoundInExtraIngredients =
currentActiveList.extraIngredients.findIndex((recipeIngredient) => {
return recipeIngredient.name === ingredient.name;
}) > -1;

if (isFoundInExtraIngredients) {
const _ingredient = currentActiveList.extraIngredients.find(
(extraIngredient) => extraIngredient.name === ingredient.name
);

if (!_ingredient) {
return;
}

let extraIngredients = currentActiveList.extraIngredients;

if (_ingredient.amount === undefined || _ingredient.amount === 1) {
extraIngredients = extraIngredients.filter(
(_ingredient) => _ingredient.name !== ingredient.name
);
} else if (_ingredient.amount > 1) {
extraIngredients = extraIngredients.map((_ingredient) => ({
..._ingredient,
amount: _ingredient.amount! - 1,
}));
}

return Meteor.users.update(this.userId, {
$set: {
activeList: {
...currentActiveList,
extraIngredients,
},
},
});
}

const isFoundInSelectedIngredients =
currentActiveList.selectedIngredients.findIndex((recipeIngredient) => {
return recipeIngredient.name === ingredient.name;
}) > -1;

let selectedIngredients = currentActiveList.selectedIngredients;
console.log(
"🚀 ~ file: users.methods.ts:131 ~ selectedIngredients:",
selectedIngredients
);

if (isFoundInSelectedIngredients) {
selectedIngredients = selectedIngredients.map((recipeIngredient) => {
console.log(
"🚀 ~ file: users.methods.ts:134 ~ selectedIngredients=selectedIngredients.map ~ recipeIngredient:",
recipeIngredient
);
if (
recipeIngredient.name === ingredient.name &&
recipeIngredient.amount
) {
const amount = recipeIngredient.amount + 1;
console.log(
"🚀 ~ file: users.methods.ts:139 ~ selectedIngredients=selectedIngredients.map ~ amount:",
amount
);

return {
...recipeIngredient,
Expand All @@ -152,10 +212,7 @@ Meteor.methods({
});
},

"users.activeList.addIngredient"(
ingredient: RecipeIngredient,
amount: number
) {
"users.activeList.addIngredient"(ingredient: RecipeIngredient) {
if (!this.userId) {
throw new Meteor.Error("Not authorized.");
}
Expand All @@ -180,18 +237,29 @@ Meteor.methods({
currentActiveList.extraIngredients || [];

if (isIngredientInSelectedIngredientList) {
selectedIngredients = currentActiveList.selectedIngredients.map(
(item) => {
if (item.name === ingredient.name) {
return {
...item,
amount,
};
}

return item;
}
const _ingredient = currentActiveList.selectedIngredients.find(
(_ingredient) => _ingredient.name === ingredient.name
);

if (!_ingredient || !_ingredient.amount) {
} else if (_ingredient.amount > 1) {
selectedIngredients = currentActiveList.selectedIngredients.map(
(item) => {
if (item.name === ingredient.name && item.amount) {
return {
...item,
amount: item.amount - 1,
};
}

return item;
}
);
} else if (_ingredient.amount === 1) {
selectedIngredients = currentActiveList.selectedIngredients.filter(
(item) => item.name !== ingredient.name
);
}
} else if (isIngredientInExtraIngredientsList) {
extraIngredients = extraIngredients.map((_ingredient) => {
if (_ingredient.name === ingredient.name) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ActiveList } from "/interfaces/active-list";

export const getExtraIngredientAmountByName = (
activeList: ActiveList,
name: string
) => {
const foundExtraIngredient = activeList.extraIngredients.find(
(_ingredient) => _ingredient.name === name
);

let totalExtraIngredient = 0;

if (foundExtraIngredient) {
totalExtraIngredient = foundExtraIngredient.amount || 0;
}

return totalExtraIngredient;
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ export const AccordionBody = ({
{sortedIngredientsByDepartment.map((ingredient, idx2) => {
const { name, amount } = ingredient;

const selectedIngredient = activeList.selectedIngredients.find(
(selectedIngredient) => selectedIngredient.name === ingredient.name
);

let calculatedAmount = amount || 0;

if (
amount &&
selectedIngredient &&
selectedIngredient.amount &&
amount - selectedIngredient.amount !== 0
) {
calculatedAmount = amount - selectedIngredient.amount;
}

const selected = checkIsIngredientComplete(activeList, name);

return (
Expand All @@ -44,7 +59,7 @@ export const AccordionBody = ({

{amount && (
<AmountModifier
amount={amount || 0}
amount={calculatedAmount}
onAdd={() => onAddIngredientAmount(ingredient)}
onRemove={() => onDecreaseIngredientAmount(ingredient)}
disabled={selected}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ActiveList } from "/interfaces/active-list";

export const getTotalRecipeAmountByName = (
activeList: ActiveList,
name: string
) => {
const totalRecipeAmount = activeList.recipes.reduce((prev, current) => {
const foundIngredient = current.recipe.food.ingredients.find(
(_ingredient) => _ingredient.name === name
);

if (!foundIngredient) {
return prev;
}

let amount = 0;

if (foundIngredient) {
const foundIngredientAmount = foundIngredient
? foundIngredient.amount || 0
: 0;
debugger;
amount =
(foundIngredientAmount / current.recipe.food.servings) *
current.servings;
}

return prev + amount;
}, 0);

return totalRecipeAmount;
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getExtraIngredientAmountByName } from "./get-extra-ingredient-amount-by-name";
import { getTotalRecipeAmountByName } from "./get-total-recipe-amount";
import { ActiveList } from "/interfaces/active-list";

export const checkIsIngredientComplete = (
Expand All @@ -18,41 +20,9 @@ export const checkIsIngredientComplete = (

const selectedAmountOfIngredient = selectedIngredientToCheck.amount;

const totalRecipeAmount = activeList.recipes.reduce((prev, current) => {
const foundIngredient = current.recipe.food.ingredients.find(
(_ingredient) => _ingredient.name === name
);
const totalRecipeAmount = getTotalRecipeAmountByName(activeList, name);

if (!foundIngredient) {
return prev;
}

let amount = 0;

if (foundIngredient) {
const foundIngredientAmount = foundIngredient
? foundIngredient.amount || 0
: 0;
debugger;
amount =
(foundIngredientAmount / current.recipe.food.servings) *
current.servings;
}

return prev + amount;
}, 0);

const foundExtraIngredient = activeList.extraIngredients.find(
(_ingredient) => _ingredient.name === name
);

let totalExtraIngredient = 0;

if (foundExtraIngredient) {
totalExtraIngredient = foundExtraIngredient.amount || 0;
}

debugger;
const totalExtraIngredient = getExtraIngredientAmountByName(activeList, name);

return (
selectedAmountOfIngredient === totalExtraIngredient + totalRecipeAmount
Expand Down

0 comments on commit ca0609b

Please sign in to comment.