
import { call, put } from "redux-saga/effects";
import { fetchJobsSuccess, fetchJobsFailure,
  fetchJobSuccess, fetchJobFailure,
  fetchEmphasisJobsSuccess,fetchEmphasisJobsFailure

 } from "../actions/ads";

import api from "../data/api";

export function* countAdViewRequestSaga(action) {

  yield call(api.stats.countAdView, action.id);

}


export function* requestAdsSaga(action) {
  try {

    const res = yield call(api.ads.fetchJobs, action.payload);
  
    if (!!res.data)
      yield put(fetchJobsSuccess(res));
    else
      yield put(fetchJobsFailure(res));

  } catch (err) {
    yield put(fetchJobsFailure(err.response));
  }
}

export function* requestAdSaga(action) {
  try {

    const res = yield call(api.ads.fetchJob, action.payload);

    if (!!res)
      yield put(fetchJobSuccess(res));
    else
      yield put(fetchJobFailure(res));

  } catch (err) {
    yield put(fetchJobFailure(err.response));
  }
}

export function* requestEmphasisJobsSaga(action) {
  try {

    const res = yield call(api.ads.fetchJobs, action.payload);
  
    if (!!res.data)
      yield put(fetchEmphasisJobsSuccess(res));
    else
      yield put(fetchEmphasisJobsFailure(res));

  } catch (err) {
    yield put(fetchEmphasisJobsFailure(err.response));
  }
}