import {
    LOAD_TWEETS,
    REMOVE_TWEET,
    NEW_TWEET,
    LIKE_TWEET,
    UNLIKE_TWEET,
} from "../actionTypes";

const tweets = (state = [], action) => {
    switch (action.type) {
        case LOAD_TWEETS:
            return [...state, ...action.tweets.results];
        case NEW_TWEET:
            return [action.tweet, ...state];
        case REMOVE_TWEET:
            return state.filter((tweet) => tweet._id !== action.id);
        case LIKE_TWEET:
            return state.map((tweet) => {
                if (tweet._id === action.id) {
                    tweet.likesNumber += 1;
                }
                return tweet;
            });
        case UNLIKE_TWEET:
            return state.map((tweet) => {
                if (tweet._id === action.id) {
                    tweet.likesNumber -= 1;
                }
                return tweet;
            });
        default:
            return state;
    }
};

export default tweets;

{'avg_line_length': 25.864864864864863, 'max_line_length': 68, 'alphanum_fraction': 0.46394984326018807, 'licenses': ['MIT'], 'repository_name': 'shubhammehra4/tweetter', 'path': 'client/src/store/reducers/tweets.js', 'size': 957, 'lang': 'JavaScript'}