{"size": 1606, "ext": "js", "max_stars_repo_path": "client/src/actions/sportActions.js", "max_stars_repo_name": "lordendbear/Sportsemblr.2", "max_stars_repo_stars_event_min_datetime": "2018-03-09T15:09:08.000Z", "max_stars_repo_stars_event_max_datetime": "2018-03-09T15:09:08.000Z", "max_issues_repo_path": "client/src/actions/sportActions.js", "max_issues_repo_name": "lordendbear/Sportsemblr.2", "max_issues_repo_issues_event_min_datetime": "2018-03-09T15:29:35.000Z", "max_issues_repo_issues_event_max_datetime": "2018-06-25T05:45:18.000Z", "max_forks_repo_path": "client/src/actions/sportActions.js", "max_forks_repo_name": "lordendbear/Sportsemblr.2", "max_forks_repo_forks_event_min_datetime": "2018-03-12T09:38:53.000Z", "max_forks_repo_forks_event_max_datetime": "2018-03-12T09:38:53.000Z"}
{"max_stars_count": 1, "max_issues_count": 46, "max_forks_count": 1, "avg_line_length": 23.9701492537, "max_line_length": 92, "alphanum_fraction": 0.5529265255}
import * as types from './actionTypes';
import SportApi from '../api/sportApi';
import * as notificationActions from './notificationActions';

export function loadSportsSuccess(sports) {
    return {
        type: types.LOAD_SPORTS_SUCCESS,
        sports
    };
}

export function createSportSuccess(sport) {
    return {
        type: types.CREATE_SPORT_SUCCESS,
        sport
    };
}

export function updateSportSuccess(sport) {
    return {
        type: types.UPDATE_SPORT_SUCCESS,
        sport
    };
}

export function deleteSportSuccess(id) {
    return {
        type: types.DELETE_SPORT_SUCCESS,
        id
    };
}

export function deleteSport(id) {
    return (dispatch) => {
        return SportApi.deleteSport(id)
            .then(() => {
                dispatch(deleteSportSuccess(id));
            });
    };
}

export function loadSports() {
    return (dispatch) => {
        return SportApi.getAll()
            .then(sports => {
                dispatch(loadSportsSuccess(sports));
            });
    };
}

export function saveSport(sport) {
    return (dispatch) => {
        return SportApi.saveSport(sport)
            .then(savedSport => {
                if (sport.id) {
                    dispatch(updateSportSuccess(savedSport));
                } else {
                    dispatch(createSportSuccess(savedSport));
                }

                dispatch(notificationActions.success({ message: 'Saved' }));
            })
            .catch(err => {
                dispatch(notificationActions.error({ message: 'Something went wrong...' }));
            });
    };
}