From 50c20889b9d48b3d0bf65d56d0aa290b21090faa Mon Sep 17 00:00:00 2001 From: QNeee Date: Thu, 20 Apr 2023 04:58:43 +0300 Subject: [PATCH] end --- .../CheckBox/CheckBox.jsx | 36 ++++++++++++------- .../RecipeInngredientsList.jsx | 9 ++--- src/redux/userRecipes/userRecipesSlice.js | 1 - 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/components/RecipeInngredientsList/CheckBox/CheckBox.jsx b/src/components/RecipeInngredientsList/CheckBox/CheckBox.jsx index 9e50c33..fd79c1c 100644 --- a/src/components/RecipeInngredientsList/CheckBox/CheckBox.jsx +++ b/src/components/RecipeInngredientsList/CheckBox/CheckBox.jsx @@ -1,4 +1,4 @@ -import { useDispatch } from 'react-redux'; +import { useDispatch, useSelector } from 'react-redux'; import { Box, @@ -12,17 +12,27 @@ import * as ReactDOMServer from 'react-dom/server'; import { Notify } from 'notiflix'; import { useState } from 'react'; import ButtonLoader from 'components/RecipePageHero/RecipeHeroContent/ButtonLoader/ButtonLoader'; +import { selectShoppingList } from 'redux/userRecipes/userRecipesSelectors'; -const CustomCheckbox = ({ recipeId, ingredientId, measure, isChecked }) => { - const elem = document.getElementById(ingredientId); - const elem1 = document.getElementById(measure); +const CustomCheckbox = ({ recipeId, ingredientId, measure, ttl }) => { + const shoppingList = useSelector(selectShoppingList); + const checkFunc = () => { + const recipesId = shoppingList + ?.filter(item => item.id === ingredientId) + .flatMap(item => item.recipesId); + if (recipesId.includes(recipeId)) { + return true; + } + return false; + }; const [check, setCheck] = useState(false); - const [check1, setCheck1] = useState(false); const dispatch = useDispatch(); const handleChange = async e => { - if (check || check1) return; - if (isChecked) { - setCheck1(true); + const elem = document.getElementById(ingredientId); + const elem1 = document.getElementById(ingredientId + ttl); + if (check) return; + if (checkFunc()) { + setCheck(true); if (elem1) { elem1.innerHTML = ''; } @@ -35,15 +45,15 @@ const CustomCheckbox = ({ recipeId, ingredientId, measure, isChecked }) => { ); - elem.insertAdjacentHTML('beforeend', buttonLoaderHtml); + elem?.insertAdjacentHTML('beforeend', buttonLoaderHtml); const arr = []; const reqObj = {}; reqObj['measure'] = measure; reqObj['ingredientId'] = ingredientId; arr.push(reqObj, recipeId); await dispatch(removeFromShoppingList(arr)); - elem.removeChild(elem.lastChild); - return setCheck1(false); + elem?.removeChild(elem.lastChild); + return setCheck(false); } setCheck(true); const buttonLoaderHtml = ReactDOMServer.renderToStaticMarkup( @@ -65,9 +75,9 @@ const CustomCheckbox = ({ recipeId, ingredientId, measure, isChecked }) => { }; return ( - {isChecked && ( + {checkFunc() && ( { const list = useSelector(selectRecipe); // eslint-disable-line - const shoppingList = useSelector(selectShoppingList); return ( @@ -30,12 +29,10 @@ const RecipeInngredientsList = ({ ingredients, recipeId }) => { - {ingredients?.map(({ _id, ttl, desc, thb, measure }) => { - const isChecked = shoppingList?.filter(item => item.id === _id).flatMap(item => item.recipesId).includes(recipeId); + {ingredients?.map(({ _id, ttl, thb, measure }) => { if (!_id) { return null; } - return ( @@ -52,7 +49,7 @@ const RecipeInngredientsList = ({ ingredients, recipeId }) => { recipeId={recipeId} ingredientId={_id} measure={measure} - isChecked={isChecked} + ttl={ttl} /> diff --git a/src/redux/userRecipes/userRecipesSlice.js b/src/redux/userRecipes/userRecipesSlice.js index 22861df..9482df3 100644 --- a/src/redux/userRecipes/userRecipesSlice.js +++ b/src/redux/userRecipes/userRecipesSlice.js @@ -101,7 +101,6 @@ const userRecipesSlice = createSlice({ state.isLoading = true; }) .addCase(removeFromShoppingList.fulfilled, (state, { payload }) => { - console.log(payload); state.shoppingList = payload.shoppingList; state.isLoading = false; state.error = null;