kazagkazag
7/8/2016 - 12:41 PM

redux-05.jsx

export const fetchAllStart = () => {
    return {
        type: constants.FETCH_ALL
    };
};

export const fetchAllSuccess = (items) => {
    return {
        type: constants.FETCH_ALL_SUCCESS,
        items
    };
};

export const fetchAllError = (errorMessage) => {
    return {
        type: constants.FETCH_ALL_ERROR,
        errorMessage
    };
};

export const fetchAll = () => {
    return (dispatch) => {
        dispatch(fetchAllStart());

        return fetch(constants.FETCH_ALL_URL)
            .then(response => {
                if(response.ok) {
                    return response.json();
                }
                else {
                    throw new Error(`${response.status} - ${response.statusText}`);
                }
            })
            .then(data => {
                dispatch(fetchAllSuccess(data.accounts));
            })
            .catch(error => {
                dispatch(fetchAllError(error.message));
            });
    }
};