Wątek przeniesiony 2021-01-03 22:00 z Java przez cerrato.

Dodawanie do koszyka

0

Cześć,
Mam problem z dodawaniem produktów do koszyka. Chce by dodało mi nazwę produktu, cene, ilosc. Niestety nazwy i ceny mi nie dodaje. Ilość dodaje z wartością NAN. Jedyna wartość, która popranie się zmienia to inCart.

Generalnie jak wybiorę produkt i kliknę dodaj do koszyka to wybiera mi właściwy produkt, ale do koszyka nie dodaje się on prawidłowo.

Wszystkie produkty mam w pliku food.js (niżej wrzuciłem jego kawałek).

Wydaje mi się, że rozwiązaniem mojego problemu jest wrzucenie tych produktów z food.js do initialState do product{}, na zasadzie:

products: {
        BagelAndCreamCheese: {
            name: 'Bagel And Cream Cheese',
            price: 12,
            numbers: 0,
            inCart: false,
        },

ale niestety nie wiem jak to zrobic, bo reczne przepisywanie chyba mija się z celem.

Gorąca prośba o pomoc.

basketReducer

import { ADD_PRODUCT_BASKET, GET_NUMBERS_BASKET } from '../../actions/types'

const initialState = {
    
    basketNumbers: 0,
    cartCost: 0,
    products: [],     
}

export default (state = initialState, action) => {
    switch(action.type){
        case ADD_PRODUCT_BASKET:
            let addQuantity = { ...state.products[action.payload]}

            addQuantity.numbers += 1;
            addQuantity.inCart = true;
            console.log(addQuantity);

            return {
                ...state,
                basketNumbers: state.basketNumbers + 1,
                cartCost: state.cartCost + state.products[action.payload],
                products: {
                    ...state.products,
                    [action.payload]: addQuantity,

                }
            }
            case GET_NUMBERS_BASKET:
                return {
                    ...state
                }
        default:
            return state;
    }
}

foods.js

const dummyData =[
    {   
        "id" : 1,
        "category":"breakfast",
        "name" : "Bagel And Cream Cheese",
        "img" : "https://i.ibb.co/pdfGzyp/breakfast1.png" ,
        "price": 12.00,
        "shortDescription" : "Quality food at low price.",
        "fullDescription" : "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum is simply dummy text of the printing and typesetting industry.  "
    },
    {   
        "id" : 2,
        "category":"breakfast",
        "name" : "Breakfast Sandwich",
        "img" : "https://i.ibb.co/8bJV04d/breakfast2.png" ,
        "price": 15,
        "shortDescription" : "Quality food at low price.",
        "fullDescription" : "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum is simply dummy text of the printing and typesetting industry.  "
    },    
    {   
        "id" : 3,
        "category":"breakfast",
        "name" : "Baked Chicken",
        "img" : "https://i.ibb.co/xjkVyVZ/breakfast3.png" ,
        "price": 22,
        "shortDescription" : "Quality food at low price.",
        "fullDescription" : "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum is simply dummy text of the printing and typesetting industry.  "
    },    
]

export default dummyData;
0

@nobo: To masz dziwne:

let addQuantity = { ...state.products[action.payload]}

co jest w action.payload ?
Czy gdzieś przekazujesz do reducera dane produktu?

1 użytkowników online, w tym zalogowanych: 0, gości: 1