From 049e2fb57e09a555a72d6ce557159d9cbd322d78 Mon Sep 17 00:00:00 2001 From: = <=> Date: Fri, 29 Jan 2021 21:15:17 +0100 Subject: [PATCH] Add removing products from meal lis --- src/components/ProductCard/index.js | 2 +- src/pages/Home/actions.js | 4 ++-- src/pages/Home/reducer.js | 35 ++++++++++++++++++----------- src/pages/Home/saga.js | 27 +++++++++------------- src/pages/Home/selectors.js | 4 ++++ 5 files changed, 40 insertions(+), 32 deletions(-) diff --git a/src/components/ProductCard/index.js b/src/components/ProductCard/index.js index 1a81f34..8ff51c1 100644 --- a/src/components/ProductCard/index.js +++ b/src/components/ProductCard/index.js @@ -14,7 +14,7 @@ const ProductCard = ({ mealId, id, product, quantity, unit }) => { const { label, calories, fat, protein, carbohydrates, eco, verified } = product const removeProductFormMeal = () => { - dispatch(removeProductFromMealAction({ mealId, id })) + dispatch(removeProductFromMealAction({ mealId, productId: id })) } const calculatedCalories = calculateMacro(unit, calories, quantity) diff --git a/src/pages/Home/actions.js b/src/pages/Home/actions.js index 6e6d801..6d718ab 100644 --- a/src/pages/Home/actions.js +++ b/src/pages/Home/actions.js @@ -125,10 +125,10 @@ export const addProductsToMealErrorAction = ({error}) => ({ error, }) -export const removeProductFromMealAction = ({ mealId, id }) => ({ +export const removeProductFromMealAction = ({ mealId, productId }) => ({ type: REMOVE_PRODUCT_FROM_MEAL_REQUEST, mealId, - id + productId }) export const removeProductFromMealSuccessAction = ({ meal }) => ({ diff --git a/src/pages/Home/reducer.js b/src/pages/Home/reducer.js index 27082f8..ec580cb 100644 --- a/src/pages/Home/reducer.js +++ b/src/pages/Home/reducer.js @@ -17,8 +17,10 @@ import { SEARCH_PRODUCT_BY_BARCODE_ERROR, SEARCH_PRODUCT_BY_LABEL_REQUEST, SEARCH_PRODUCT_BY_LABEL_SUCCESS, + REMOVE_PRODUCT_FROM_MEAL_SUCCESS, SEARCH_PRODUCT_BY_LABEL_ERROR, - SET_SELECTED_MEAL, + REMOVE_PRODUCT_FROM_MEAL_ERROR, + SET_SELECTED_MEAL, REMOVE_PRODUCT_FROM_MEAL_REQUEST, } from './constants'; import { sortMeals } from 'utils/sort'; @@ -69,12 +71,28 @@ export const initialState = { label: '', products: [], date: null, + productId: null, }, meals: defaultMeals }; const homePageReducer = produce((draft, action) => { switch(action.type) { + case REMOVE_PRODUCT_FROM_MEAL_REQUEST: + draft.form.id = action.mealId; + draft.form.productId = action.productId; + break; + + case REMOVE_PRODUCT_FROM_MEAL_SUCCESS: + draft.meals = sortMeals(draft.meals, action.meal, [action.meal.label]) + draft.totalCalories = sumMacro(draft.meals, 'calories') + draft.totalFats = sumMacro(draft.meals, 'fat') + draft.totalCarbohydrates = sumMacro(draft.meals, 'carbohydrates') + draft.totalProteins = sumMacro(draft.meals, 'protein') + draft.isLoading = false; + break; + + case SET_SELECTED_MEAL: draft.form.id = action.id; draft.form.label = action.label.toLowerCase(); @@ -131,26 +149,16 @@ const homePageReducer = produce((draft, action) => { break; case ADD_PRODUCTS_TO_MEAL_SUCCESS: - draft.meals = sortMeals(defaultMeals, action.meals, [action.meal.label]) - console.log(draft.totalCalories, sumMacro(draft.meals, 'calories')) - draft.dailyCalories = action.dailyCalories; - draft.dailyFats = action.dailyFats; - draft.dailyProteins = action.dailyProteins; - draft.dailyCarbohydrates = action.dailyCarbs; + draft.meals = sortMeals(draft.meals, action.meal, [action.meal.label]) draft.totalCalories = sumMacro(draft.meals, 'calories') draft.totalFats = sumMacro(draft.meals, 'fat') draft.totalCarbohydrates = sumMacro(draft.meals, 'carbohydrates') draft.totalProteins = sumMacro(draft.meals, 'protein') - draft.isLoading = false; break; case CREATE_MEAL_SUCCESS: - draft.meals = sortMeals(defaultMeals, action.meals, [action.meal.label]) - draft.dailyCalories = action.dailyCalories; - draft.dailyFats = action.dailyFats; - draft.dailyProteins = action.dailyProteins; - draft.dailyCarbohydrates = action.dailyCarbs; + draft.meals = sortMeals(defaultMeals, action.meal, [action.meal.label]) draft.totalCalories = sumMacro(draft.meals, 'calories') draft.totalFats = sumMacro(draft.meals, 'fat') draft.totalCarbohydrates = sumMacro(draft.meals, 'carbohydrates') @@ -175,6 +183,7 @@ const homePageReducer = produce((draft, action) => { case ADD_PRODUCTS_TO_MEAL_ERROR: case SEARCH_PRODUCT_BY_LABEL_ERROR: case SEARCH_PRODUCT_BY_BARCODE_ERROR: + case REMOVE_PRODUCT_FROM_MEAL_ERROR: draft.isLoading = false; draft.error = action.error; break; diff --git a/src/pages/Home/saga.js b/src/pages/Home/saga.js index 63dc15d..a87e851 100644 --- a/src/pages/Home/saga.js +++ b/src/pages/Home/saga.js @@ -1,8 +1,8 @@ import { takeLatest, call, put, select } from 'redux-saga/effects'; import {api, request, routes} from 'utils'; -import { GET_MEALS_REQUEST, UPDATE_MEAL_REQUEST, SEARCH_PRODUCT_BY_BARCODE_REQUEST, SEARCH_PRODUCT_BY_LABEL_REQUEST, CREATE_MEAL_REQUEST, ADD_PRODUCTS_TO_MEAL_REQUEST } from './constants'; -import { searchProductByLabelErrorAction, addProductsToSuccessAction, addProductsToMealErrorAction, searchProductByLabelSuccessAction, createMealSuccessAction, createMealErrorAction, searchProductByBarcodeSuccessAction, searchProductByBarcodeErrorAction, updateMealErrorAction, updateMealSuccessAction, getMealsSuccessAction, getMealsErrorAction } from './actions'; -import { makeSelectLabel, makeSelectFormProducts, makeSelectMealLabel, makeSelectMealId, makeSelectBarcode, makeSelectDate, } from './selectors' +import { GET_MEALS_REQUEST, REMOVE_PRODUCT_FROM_MEAL_REQUEST, SEARCH_PRODUCT_BY_BARCODE_REQUEST, SEARCH_PRODUCT_BY_LABEL_REQUEST, CREATE_MEAL_REQUEST, ADD_PRODUCTS_TO_MEAL_REQUEST } from './constants'; +import { searchProductByLabelErrorAction, removeProductFromMealSuccessAction, removeProductFromMealErrorAction, addProductsToSuccessAction, addProductsToMealErrorAction, searchProductByLabelSuccessAction, createMealSuccessAction, createMealErrorAction, searchProductByBarcodeSuccessAction, searchProductByBarcodeErrorAction, updateMealErrorAction, updateMealSuccessAction, getMealsSuccessAction, getMealsErrorAction } from './actions'; +import { makeSelectLabel, makeSelectFormProducts, makeSelectMealLabel, makeSelectMealId, makeSelectBarcode, makeSelectProductId, makeSelectDate, } from './selectors' import { makeSelectTokens } from 'containers/App/selectors' import {push} from "connected-react-router"; @@ -27,27 +27,24 @@ export function* getMeals() { } } -export function* updateMeal() { +export function* removeProductFromMeal() { const { access } = yield select(makeSelectTokens()); - const products = yield select(makeSelectFormProducts()); - const label = yield select(makeSelectMealLabel()); - const date = yield select(makeSelectDate()); + const productId = yield select(makeSelectProductId()); const mealId = yield select(makeSelectMealId()); - const requestURL = `${api.meals}/${mealId}`; + const requestURL = `${api.meals}/${mealId}/products`; const requestParameters = { - method: 'PUT', + method: 'PATCH', headers: { Accept: 'application/json', 'Content-Type': 'application/json', Authorization: `Bearer ${access.token}`, }, - body: JSON.stringify({ label, products, date }), + body: JSON.stringify({ productId }), }; try { const meal = yield call(request, requestURL, requestParameters); - console.log('pages/home/saga/UPDATE_MEAL', meal) - // yield put(updateMealSuccessAction({ meal })); + yield put(removeProductFromMealSuccessAction({ meal })); } catch (error) { - yield put(updateMealErrorAction({error: error.message})); + yield put(removeProductFromMealErrorAction({error: error.message})); } } @@ -59,8 +56,6 @@ export function* createMeal() { const requestURL = api.meals; - console.log(products) - const requestParameters = { method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', Authorization: `Bearer ${access.token}`, }, @@ -147,7 +142,7 @@ export default function* MealPageSaga() { yield takeLatest(SEARCH_PRODUCT_BY_BARCODE_REQUEST, searchProductByBarcode); yield takeLatest(SEARCH_PRODUCT_BY_LABEL_REQUEST, searchProductByLabel); yield takeLatest(GET_MEALS_REQUEST, getMeals); - yield takeLatest(UPDATE_MEAL_REQUEST, updateMeal); + yield takeLatest(REMOVE_PRODUCT_FROM_MEAL_REQUEST, removeProductFromMeal); yield takeLatest(CREATE_MEAL_REQUEST, createMeal); yield takeLatest(ADD_PRODUCTS_TO_MEAL_REQUEST, addProductsToMeal); } diff --git a/src/pages/Home/selectors.js b/src/pages/Home/selectors.js index 45cb86d..e11ede9 100644 --- a/src/pages/Home/selectors.js +++ b/src/pages/Home/selectors.js @@ -33,6 +33,9 @@ const makeSelectMealLabel = () => const makeSelectMealId = () => createSelector(selectHomePageDomain, (substate) => substate.form.id); +const makeSelectProductId = () => + createSelector(selectHomePageDomain, (substate) => substate.form.productId); + const makeSelectFormProducts = () => createSelector(selectHomePageDomain, (substate) => substate.form.products); @@ -74,6 +77,7 @@ export { makeSelectFormProducts, makeSelectMealLabel, makeSelectMealId, + makeSelectProductId, makeSelectDate, makeSelectLabel, makeSelectProducts,