import { Api } from 'routes/Api';
import { put, call } from 'redux-saga/effects';
import { get } from 'services/requests';
import * as actions from 'lib/redux/actions/external-api-actions';
import { Action } from 'lib/types/interfaces';



export function* fetchCovidStats(action: Action) {
	try {
		const res = yield call(() => get({
			url: action.country ? Api.mathdroid + '/countries/' + action.country.toLowerCase() : Api.mathdroid
		}));

		yield put(actions.fetchedCovidStats({ data: res.data, country: action.country ? action.country?.toLowerCase() : 'world' }));
	}
	catch (error) {
		yield put(actions.failedFetchCovidStats({ error }));
	}
}