import { call, put, takeLatest, takeEvery } from 'redux-saga/effects';
import * as types from '../actions/types';
import * as GeneralService from '../services/GeneralService';
import * as GeneralAction from '../actions/GeneralAction';
import { Toast } from './../components';
import * as UserAction from '../actions/UserAction';

function* getConuntryList() {
  try {
    const res = yield call(GeneralService.getCountryList);
    if (res.success) {
      yield put(GeneralAction.getCountryListSuccess(res.data));
    } else {
      yield put(GeneralAction.getCountryListFail(res.message));
    }
  } catch (error) {
    yield put(GeneralAction.getCountryListFail('Something went wrong'));
    yield put (UserAction.logoutRequest())
    Toast.errorBottom('Token expired!');
  }
}

export default function* watchAuthRequests() {
  yield takeLatest(types.GET_COUNTRY_LIST_REQUEST, getConuntryList);
}
