all files / redux/modules/stars/ stars.ts

87.88% Statements 29/33
62.5% Branches 5/8
90% Functions 9/10
87.5% Lines 28/32
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77            14× 14×                                 14×                                                      
"use strict";
/** Action Types */
exports.STARS_REQUEST = 'STARS_REQUEST';
exports.STARS_SUCCESS = 'STARS_SUCCESS';
exports.STARS_FAILURE = 'STARS_FAILURE';
/** Initial State */
var initialState = {
    isFetching: false,
};
/** Reducer */
function starsReducer(state, action) {
    if (state === void 0) { state = initialState; }
    switch (action.type) {
        case exports.STARS_REQUEST:
            return Object.assign({}, state, {
                isFetching: true,
            });
        case exports.STARS_SUCCESS:
            return Object.assign({}, state, {
                isFetching: false,
                count: action.count,
            });
        case exports.STARS_FAILURE:
            return Object.assign({}, state, {
                isFetching: false,
                error: true,
                message: action.message,
            });
        default:
            return state;
    }
}
exports.starsReducer = starsReducer;
/** Async Action Creator */
function getStars() {
    return function (dispatch) {
        dispatch(starsRequest());
        return fetch('https://api.github.com/repos/abcd/abcd')
            .then(function (res) {
            if (res.ok) {
                return res.json()
                    .then(function (res) { return dispatch(starsSuccess(res.stargazers_count)); });
            }
            else {
                return res.json()
                    .then(function (res) { return dispatch(starsFailure(res)); });
            }
        })
            .catch(function (err) { return dispatch(starsFailure(err)); });
    };
}
exports.getStars = getStars;
/** Action Creator */
function starsRequest() {
    return {
        type: exports.STARS_REQUEST,
    };
}
exports.starsRequest = starsRequest;
/** Action Creator */
function starsSuccess(count) {
    return {
        type: exports.STARS_SUCCESS,
        count: count,
    };
}
exports.starsSuccess = starsSuccess;
/** Action Creator */
function starsFailure(message) {
    return {
        type: exports.STARS_FAILURE,
        message: message,
    };
}
exports.starsFailure = starsFailure;