/* like mutation */
import ACTION from '../actions/actiontsTypes';

const initialState = {
    favouriteGoods: [],
    favouriteGoodsIDsArray: [],
    isFetching: false,
    error: null
};

export default function (state = initialState, action) {

    switch (action.type) {
        case ACTION.FAVOURITE_GOODS_REQUEST: {
            return {
                ...state,
                isFetching: true,
                error: null
            }
        }
        case ACTION.FAVOURITE_GOODS_RESPONSE: {

            const favouriteGoodsIDsArray = [];
            action.data.map(elem => {
                favouriteGoodsIDsArray.push(elem.GoodsAttributes[0].id);
            });

            return {
                ...state,
                favouriteGoods: action.data,
                favouriteGoodsIDsArray: favouriteGoodsIDsArray,
                isFetching: false,
                error: null
            }
        }
        case ACTION.FAVOURITE_GOODS_ERROR: {
            return {
                ...state,
                error: action.error.response.data,
                isFetching: false
            }
        }
        default: {
            return state
        }
    }
}

