{"size": 1111, "ext": "js", "max_stars_repo_path": "src/reducers/tweets.js", "max_stars_repo_name": "charlp1/tweets", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/reducers/tweets.js", "max_issues_repo_name": "charlp1/tweets", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/reducers/tweets.js", "max_forks_repo_name": "charlp1/tweets", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 21.3653846154, "max_line_length": 43, "alphanum_fraction": 0.3771377138}
import {
    ADD_TWEET,
    EDIT_TWEET,
    DELETE_TWEET,
} from './../constants/tweets';

export const tweetsInitialState = {
    counter: 1,
    data: {}
};

export const tweets = (
    state = tweetsInitialState,
    action
) => {
    const {counter, data} = state;
    const {type, payload} = action;

    switch(type) {
        case ADD_TWEET:
            return {
                ...state,
                counter: (counter + 1),
                data: {
                    ...data,
                    [counter]: {
                        ...payload,
                        id: counter
                    }
                }
            }
        case EDIT_TWEET:
            return {
                ...state,
                data: {
                    ...data,
                    [payload.id]: payload
                }
            };
        case DELETE_TWEET:
            let updatedData = { ...data };
            delete updatedData[payload.id];

            return {
                ...state,
                data: updatedData
            };
        default:
            return state;
    }
};
