Add removing products from meal lis

This commit is contained in:
= 2021-01-29 21:15:17 +01:00
parent 6c8bbc6e3f
commit 049e2fb57e
5 changed files with 40 additions and 32 deletions

View File

@ -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)

View File

@ -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 }) => ({

View File

@ -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;

View File

@ -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);
}

View File

@ -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,