import { takeEvery, put, call } from 'redux-saga/effects';
/**
* @namespace sagas/results
*/
import { select } from 'redux-saga-test-plan/matchers';
import Constants, { GAME_TYPE } from '../constants';
import { requestGet } from '../utils/request';
import {
parseTeamStatistic,
parseMatches,
} from '../utils/parser';
import {
GET_LEAGUE_TABLE_SUCCESS,
GET_RESULTS_TABLES_SUCCESS,
GET_LEAGUE_TABLE,
GET_RESULTS_TABLES,
SHOW_ERROR_MODAL,
} from '../reducers';
import { getGameType } from '../store/bets';
/**
* @memberof sagas/results
* @typedef "getLeagueTable/response.data"
* @type {object}
* @property {number} competition
* @property {number} season
* @property {number} week
* @property {string} value Parsed data for evry match (match-points-last5)
*/
/**
* Represent table of statistics for current season
*
* @memberof sagas/results
* @async
* @param {object} action
* @property {object} action
* @property {object} action.payload
* @property {number} action.payload.league League ID
*
* @yields {call} /game/standings/{leagueId}
* @yields {put} GET_LEAGUE_TABLE_SUCCESS
* @yields {put} SHOW_ERROR_MODAL
*/
function* getLeagueTable(action) {
let response;
try {
const gameType = yield select(getGameType);
const url = gameType === GAME_TYPE.TURBO
? Constants.API_URLS.TURBO_LEAGUE_TABLE : Constants.API_URLS.LEAGUE_TABLE;
response = yield call(requestGet, `${url}/${action.payload.league}`, true);
if (response.status === -1) {
throw new Error(response.error.message);
} else {
yield put({
type: GET_LEAGUE_TABLE_SUCCESS,
payload: {
teams: parseTeamStatistic(response.data.value),
leagueTableWeek: response.data.week,
},
});
}
} catch (error) {
yield put({
type: SHOW_ERROR_MODAL,
payload: {
activeModal: error.modal || Constants.MODALS.GENERAL_ERROR,
message: error.message,
},
});
}
}
/**
* @memberof sagas/results
* @typedef "getResults/response.data"
* @type {object}
* @property {number} leagueId
* @property {number} roundId
* @property {number} season
* @property {number} currentWeek
* @property {Array} results For evry week contain week id, matches and results
*/
/**
* Represent tables of results for all finished weeks
*
* @memberof sagas/results
* @async
* @param {object} action
* @property {object} action
* @property {object} action.payload
* @param {number} action.payload.league League ID
*
* @yields {call} /game/league/{leagueId}/results
* @yields {put} GET_RESULTS_TABLES_SUCCESS
* @yields {put} SHOW_ERROR_MODAL
*/
function* getResults(action) {
let response;
try {
const gameType = yield select(getGameType);
const url = gameType === GAME_TYPE.TURBO
? Constants.API_URLS.TURBO_RESULTS_TABLES : Constants.API_URLS.RESULTS_TABLES;
response = yield call(requestGet, url.replace('{leagueID}', action.payload.league), true);
if (response.status === -1) {
throw new Error(response.error.message);
} else {
response.data.results.forEach((resut) => {
if (typeof resut.matches === 'string') {
// eslint-disable-next-line no-param-reassign
resut.matches = parseMatches(resut.matches);
// eslint-disable-next-line no-param-reassign
resut.results = parseMatches(resut.results);
}
});
yield put({
type: GET_RESULTS_TABLES_SUCCESS,
payload: {
results: response.data.results,
},
});
}
} catch (error) {
yield put({
type: SHOW_ERROR_MODAL,
payload: {
activeModal: error.modal || Constants.MODALS.GENERAL_ERROR,
message: error.message,
},
});
}
}
/**
* @memberof sagas/results
* @async
* @returns {void}
*/
export default function* resultsSaga() {
yield takeEvery(GET_LEAGUE_TABLE, getLeagueTable);
yield takeEvery(GET_RESULTS_TABLES, getResults);
}