
import { call, put } from "redux-saga/effects";
import {
  fetchCompanySuccess,
  fetchCompanyFailure,
  fetchCompaniesSuccess,
  fetchCompaniesFailure,
  fetchEmphasisCompaniesSuccess,
  fetchEmphasisCompaniesFailure
} from "../actions/companies";

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


export function* countCompanyViewRequestSaga(action) {

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

}

export function* requestCompaniesSaga(action) {
  try {

    const res = yield call(api.companies.fetchCompanies, action.payload);

    if (!!res.data)
      yield put(fetchCompaniesSuccess(res));
    else
      yield put(fetchCompaniesFailure(res));

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

export function* requestCompanySaga(action) {
  try {
    const res = yield call(api.companies.fetchCompany, action.payload);

    if (!!res)
      yield put(fetchCompanySuccess(res));
    else {
      yield put(fetchCompanyFailure(res));
    }

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

export function* requestEmphasisCompaniesSaga(action) {
  try {

    const res = yield call(api.companies.fetchCompanies, action.payload);

    if (!!res.data)
      yield put(fetchEmphasisCompaniesSuccess(res));
    else
      yield put(fetchEmphasisCompaniesFailure(res));

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