{"size": 1576, "ext": "js", "max_stars_repo_path": "src/store/sagas/infoSaga.js", "max_stars_repo_name": "Debarshi95/My-Movies", "max_stars_repo_stars_event_min_datetime": "2021-12-28T09:26:20.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-31T08:23:38.000Z", "max_issues_repo_path": "src/store/sagas/infoSaga.js", "max_issues_repo_name": "Debarshi95/My-Movies", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/store/sagas/infoSaga.js", "max_forks_repo_name": "Debarshi95/My-Movies", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 33.5319148936, "max_line_length": 88, "alphanum_fraction": 0.7106598985}
import { all, call, put, takeLatest } from 'redux-saga/effects';
import { getCollection, getItemInfo, getRecommendations } from '../../services/tmdbApi';
import { Creators, Types } from '../actions/commonActions';
import fetchEntity from './fetchEntitySaga';

function* fetchItemInfo({ itemId, itemType }) {
  const { successGetItemInfo, failureGetItemInfo } = Creators;
  try {
    const res = yield call(getItemInfo, itemId, itemType);

    if (res.status === 200) {
      const data = yield res.json();
      yield put(successGetItemInfo(data));
    }
  } catch (error) {
    yield put(failureGetItemInfo(error?.message));
  }
}

function* fetchRecommended({ itemId, itemType }) {
  const { successGetRecommendation, failureGetRecommendation } = Creators;
  const request = getRecommendations.bind(null, itemId, itemType);
  yield fetchEntity(request, failureGetRecommendation, successGetRecommendation);
}

function* fetchCollection({ itemId }) {
  const { successGetCollection, failureGetCollection } = Creators;
  try {
    const res = yield call(getCollection, itemId);

    if (res.status === 200) {
      const data = yield res.json();
      const resData = { results: data.parts };
      yield put(successGetCollection(resData));
    }
  } catch (error) {
    yield put(failureGetCollection(error?.message));
  }
}
export default function* infoSaga() {
  yield all([
    takeLatest(Types.REQUEST_GET_ITEM_INFO, fetchItemInfo),
    takeLatest(Types.REQUEST_GET_RECOMMENDATION, fetchRecommended),
    takeLatest(Types.REQUEST_GET_COLLECTION, fetchCollection),
  ]);
}
