Add removing products from meal lis
This commit is contained in:
parent
6c8bbc6e3f
commit
049e2fb57e
@ -14,7 +14,7 @@ const ProductCard = ({ mealId, id, product, quantity, unit }) => {
|
|||||||
const { label, calories, fat, protein, carbohydrates, eco, verified } = product
|
const { label, calories, fat, protein, carbohydrates, eco, verified } = product
|
||||||
|
|
||||||
const removeProductFormMeal = () => {
|
const removeProductFormMeal = () => {
|
||||||
dispatch(removeProductFromMealAction({ mealId, id }))
|
dispatch(removeProductFromMealAction({ mealId, productId: id }))
|
||||||
}
|
}
|
||||||
|
|
||||||
const calculatedCalories = calculateMacro(unit, calories, quantity)
|
const calculatedCalories = calculateMacro(unit, calories, quantity)
|
||||||
|
@ -125,10 +125,10 @@ export const addProductsToMealErrorAction = ({error}) => ({
|
|||||||
error,
|
error,
|
||||||
})
|
})
|
||||||
|
|
||||||
export const removeProductFromMealAction = ({ mealId, id }) => ({
|
export const removeProductFromMealAction = ({ mealId, productId }) => ({
|
||||||
type: REMOVE_PRODUCT_FROM_MEAL_REQUEST,
|
type: REMOVE_PRODUCT_FROM_MEAL_REQUEST,
|
||||||
mealId,
|
mealId,
|
||||||
id
|
productId
|
||||||
})
|
})
|
||||||
|
|
||||||
export const removeProductFromMealSuccessAction = ({ meal }) => ({
|
export const removeProductFromMealSuccessAction = ({ meal }) => ({
|
||||||
|
@ -17,8 +17,10 @@ import {
|
|||||||
SEARCH_PRODUCT_BY_BARCODE_ERROR,
|
SEARCH_PRODUCT_BY_BARCODE_ERROR,
|
||||||
SEARCH_PRODUCT_BY_LABEL_REQUEST,
|
SEARCH_PRODUCT_BY_LABEL_REQUEST,
|
||||||
SEARCH_PRODUCT_BY_LABEL_SUCCESS,
|
SEARCH_PRODUCT_BY_LABEL_SUCCESS,
|
||||||
|
REMOVE_PRODUCT_FROM_MEAL_SUCCESS,
|
||||||
SEARCH_PRODUCT_BY_LABEL_ERROR,
|
SEARCH_PRODUCT_BY_LABEL_ERROR,
|
||||||
SET_SELECTED_MEAL,
|
REMOVE_PRODUCT_FROM_MEAL_ERROR,
|
||||||
|
SET_SELECTED_MEAL, REMOVE_PRODUCT_FROM_MEAL_REQUEST,
|
||||||
} from './constants';
|
} from './constants';
|
||||||
|
|
||||||
import { sortMeals } from 'utils/sort';
|
import { sortMeals } from 'utils/sort';
|
||||||
@ -69,12 +71,28 @@ export const initialState = {
|
|||||||
label: '',
|
label: '',
|
||||||
products: [],
|
products: [],
|
||||||
date: null,
|
date: null,
|
||||||
|
productId: null,
|
||||||
},
|
},
|
||||||
meals: defaultMeals
|
meals: defaultMeals
|
||||||
};
|
};
|
||||||
|
|
||||||
const homePageReducer = produce((draft, action) => {
|
const homePageReducer = produce((draft, action) => {
|
||||||
switch(action.type) {
|
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:
|
case SET_SELECTED_MEAL:
|
||||||
draft.form.id = action.id;
|
draft.form.id = action.id;
|
||||||
draft.form.label = action.label.toLowerCase();
|
draft.form.label = action.label.toLowerCase();
|
||||||
@ -131,26 +149,16 @@ const homePageReducer = produce((draft, action) => {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case ADD_PRODUCTS_TO_MEAL_SUCCESS:
|
case ADD_PRODUCTS_TO_MEAL_SUCCESS:
|
||||||
draft.meals = sortMeals(defaultMeals, action.meals, [action.meal.label])
|
draft.meals = sortMeals(draft.meals, action.meal, [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.totalCalories = sumMacro(draft.meals, 'calories')
|
draft.totalCalories = sumMacro(draft.meals, 'calories')
|
||||||
draft.totalFats = sumMacro(draft.meals, 'fat')
|
draft.totalFats = sumMacro(draft.meals, 'fat')
|
||||||
draft.totalCarbohydrates = sumMacro(draft.meals, 'carbohydrates')
|
draft.totalCarbohydrates = sumMacro(draft.meals, 'carbohydrates')
|
||||||
draft.totalProteins = sumMacro(draft.meals, 'protein')
|
draft.totalProteins = sumMacro(draft.meals, 'protein')
|
||||||
|
|
||||||
draft.isLoading = false;
|
draft.isLoading = false;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case CREATE_MEAL_SUCCESS:
|
case CREATE_MEAL_SUCCESS:
|
||||||
draft.meals = sortMeals(defaultMeals, action.meals, [action.meal.label])
|
draft.meals = sortMeals(defaultMeals, action.meal, [action.meal.label])
|
||||||
draft.dailyCalories = action.dailyCalories;
|
|
||||||
draft.dailyFats = action.dailyFats;
|
|
||||||
draft.dailyProteins = action.dailyProteins;
|
|
||||||
draft.dailyCarbohydrates = action.dailyCarbs;
|
|
||||||
draft.totalCalories = sumMacro(draft.meals, 'calories')
|
draft.totalCalories = sumMacro(draft.meals, 'calories')
|
||||||
draft.totalFats = sumMacro(draft.meals, 'fat')
|
draft.totalFats = sumMacro(draft.meals, 'fat')
|
||||||
draft.totalCarbohydrates = sumMacro(draft.meals, 'carbohydrates')
|
draft.totalCarbohydrates = sumMacro(draft.meals, 'carbohydrates')
|
||||||
@ -175,6 +183,7 @@ const homePageReducer = produce((draft, action) => {
|
|||||||
case ADD_PRODUCTS_TO_MEAL_ERROR:
|
case ADD_PRODUCTS_TO_MEAL_ERROR:
|
||||||
case SEARCH_PRODUCT_BY_LABEL_ERROR:
|
case SEARCH_PRODUCT_BY_LABEL_ERROR:
|
||||||
case SEARCH_PRODUCT_BY_BARCODE_ERROR:
|
case SEARCH_PRODUCT_BY_BARCODE_ERROR:
|
||||||
|
case REMOVE_PRODUCT_FROM_MEAL_ERROR:
|
||||||
draft.isLoading = false;
|
draft.isLoading = false;
|
||||||
draft.error = action.error;
|
draft.error = action.error;
|
||||||
break;
|
break;
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { takeLatest, call, put, select } from 'redux-saga/effects';
|
import { takeLatest, call, put, select } from 'redux-saga/effects';
|
||||||
import {api, request, routes} from 'utils';
|
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 { 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, addProductsToSuccessAction, addProductsToMealErrorAction, searchProductByLabelSuccessAction, createMealSuccessAction, createMealErrorAction, searchProductByBarcodeSuccessAction, searchProductByBarcodeErrorAction, updateMealErrorAction, updateMealSuccessAction, getMealsSuccessAction, getMealsErrorAction } from './actions';
|
import { searchProductByLabelErrorAction, removeProductFromMealSuccessAction, removeProductFromMealErrorAction, addProductsToSuccessAction, addProductsToMealErrorAction, searchProductByLabelSuccessAction, createMealSuccessAction, createMealErrorAction, searchProductByBarcodeSuccessAction, searchProductByBarcodeErrorAction, updateMealErrorAction, updateMealSuccessAction, getMealsSuccessAction, getMealsErrorAction } from './actions';
|
||||||
import { makeSelectLabel, makeSelectFormProducts, makeSelectMealLabel, makeSelectMealId, makeSelectBarcode, makeSelectDate, } from './selectors'
|
import { makeSelectLabel, makeSelectFormProducts, makeSelectMealLabel, makeSelectMealId, makeSelectBarcode, makeSelectProductId, makeSelectDate, } from './selectors'
|
||||||
import { makeSelectTokens } from 'containers/App/selectors'
|
import { makeSelectTokens } from 'containers/App/selectors'
|
||||||
import {push} from "connected-react-router";
|
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 { access } = yield select(makeSelectTokens());
|
||||||
const products = yield select(makeSelectFormProducts());
|
const productId = yield select(makeSelectProductId());
|
||||||
const label = yield select(makeSelectMealLabel());
|
|
||||||
const date = yield select(makeSelectDate());
|
|
||||||
const mealId = yield select(makeSelectMealId());
|
const mealId = yield select(makeSelectMealId());
|
||||||
|
|
||||||
const requestURL = `${api.meals}/${mealId}`;
|
const requestURL = `${api.meals}/${mealId}/products`;
|
||||||
|
|
||||||
const requestParameters = {
|
const requestParameters = {
|
||||||
method: 'PUT',
|
method: 'PATCH',
|
||||||
headers: { Accept: 'application/json', 'Content-Type': 'application/json', Authorization: `Bearer ${access.token}`, },
|
headers: { Accept: 'application/json', 'Content-Type': 'application/json', Authorization: `Bearer ${access.token}`, },
|
||||||
body: JSON.stringify({ label, products, date }),
|
body: JSON.stringify({ productId }),
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const meal = yield call(request, requestURL, requestParameters);
|
const meal = yield call(request, requestURL, requestParameters);
|
||||||
console.log('pages/home/saga/UPDATE_MEAL', meal)
|
yield put(removeProductFromMealSuccessAction({ meal }));
|
||||||
// yield put(updateMealSuccessAction({ meal }));
|
|
||||||
} catch (error) {
|
} 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;
|
const requestURL = api.meals;
|
||||||
|
|
||||||
console.log(products)
|
|
||||||
|
|
||||||
const requestParameters = {
|
const requestParameters = {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { Accept: 'application/json', 'Content-Type': 'application/json', Authorization: `Bearer ${access.token}`, },
|
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_BARCODE_REQUEST, searchProductByBarcode);
|
||||||
yield takeLatest(SEARCH_PRODUCT_BY_LABEL_REQUEST, searchProductByLabel);
|
yield takeLatest(SEARCH_PRODUCT_BY_LABEL_REQUEST, searchProductByLabel);
|
||||||
yield takeLatest(GET_MEALS_REQUEST, getMeals);
|
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(CREATE_MEAL_REQUEST, createMeal);
|
||||||
yield takeLatest(ADD_PRODUCTS_TO_MEAL_REQUEST, addProductsToMeal);
|
yield takeLatest(ADD_PRODUCTS_TO_MEAL_REQUEST, addProductsToMeal);
|
||||||
}
|
}
|
||||||
|
@ -33,6 +33,9 @@ const makeSelectMealLabel = () =>
|
|||||||
const makeSelectMealId = () =>
|
const makeSelectMealId = () =>
|
||||||
createSelector(selectHomePageDomain, (substate) => substate.form.id);
|
createSelector(selectHomePageDomain, (substate) => substate.form.id);
|
||||||
|
|
||||||
|
const makeSelectProductId = () =>
|
||||||
|
createSelector(selectHomePageDomain, (substate) => substate.form.productId);
|
||||||
|
|
||||||
const makeSelectFormProducts = () =>
|
const makeSelectFormProducts = () =>
|
||||||
createSelector(selectHomePageDomain, (substate) => substate.form.products);
|
createSelector(selectHomePageDomain, (substate) => substate.form.products);
|
||||||
|
|
||||||
@ -74,6 +77,7 @@ export {
|
|||||||
makeSelectFormProducts,
|
makeSelectFormProducts,
|
||||||
makeSelectMealLabel,
|
makeSelectMealLabel,
|
||||||
makeSelectMealId,
|
makeSelectMealId,
|
||||||
|
makeSelectProductId,
|
||||||
makeSelectDate,
|
makeSelectDate,
|
||||||
makeSelectLabel,
|
makeSelectLabel,
|
||||||
makeSelectProducts,
|
makeSelectProducts,
|
||||||
|
Loading…
Reference in New Issue
Block a user