import catalog from '../__test__/catalog.json'
import metrics from '../__test__/metrics.json'
import datasets from '../__test__/datasets.json'

export function cancelAllPromise(component) {
  component.cancelablePromise.map( promise => promise.cancel())
}

export const makeCancelable = (promise) => {
  let hasCanceled_ = false;

  const wrappedPromise = new Promise((resolve, reject) => {
    promise.then((val) =>
      hasCanceled_ ? reject({isCanceled: true}) : resolve(val)
    );
    promise.catch((error) =>
      hasCanceled_ ? reject({isCanceled: true}) : reject(error)
    );
  });

  return {
    promise: wrappedPromise,
    cancel() {
      hasCanceled_ = true;
    },
  };
};

function _f(data) {
  return new Promise( (resolve, reject) => {
    if (data) {
      return resolve(data)
    } else {
      return reject()
    }
  })
}

function waitDataAndSetState(dataPromise, component, stateName) {
  const cancelablePromise = makeCancelable(dataPromise
    .then(data => {
      component.setState({stateName});
    })
    .catch(err => {
      if (!component.state.errors.includes(err.message)) {
        const errors = [...component.state.errors, err.message]
        component.setState({ errors })
      }
      throw err
    })
  )

    component.setState({cancelablePromise})
    return cancelablePromise
}

export function fetchMetrics(component, catalogId) {
  if (!catalogId) return Promise.reject(new Error('catalogId is required'))
  const fetchPromise = catalogId === '1' ? _f(metrics) : _f()
  return waitDataAndSetState(fetchPromise, component, metrics)
}

export function fetchCatalog(component, catalogId) {
  if (!catalogId) return Promise.reject(new Error('catalogId is required'))
  const fetchPromise = catalogId === '1' ? _f(catalog) : _f()
  return waitDataAndSetState(fetchPromise, component, catalog)
}

export function fetchDatasets(component) {
  const fetchPromise = _f(datasets)
  return waitDataAndSetState(fetchPromise, component, datasets)
}

@@ -36,24 +36,73 @@
 talog.json'%0A
+import catalogs from '../__test__/catalogs.json'%0A
 import metri
@@ -143,24 +143,23 @@
 %0Aimport 
-datasets
+harvest
  from '.
@@ -173,572 +173,363 @@
 t__/
-datasets
+harvest
 .json'%0A
-%0Aex
+im
 port 
-function cancelAllPromise(component) %7B%0A  component.cancelablePromise.map( promise =%3E promise.cancel())%0A%7D%0A%0Aexport const makeCancelable = (promise) =%3E %7B%0A  let hasCanceled_ = false;%0A%0A  const wrappedPromise = new Promise((resolve, reject) =%3E %7B%0A    promise.then((val) =%3E%0A      hasCanceled_ ? reject(%7BisCanceled: true%7D) : resolve(val)%0A    );%0A    promise.catch((error) =%3E%0A      hasCanceled_ ? reject(%7BisCanceled: true%7D) : reject(error)%0A    );%0A  %7D);%0A%0A  return %7B%0A    promise: wrappedPromise,%0A    cancel() %7B%0A      hasCanceled_ = true;%0A    %7D,%0A  %7D;
+harvests from '../__test__/harvests.json'%0Aimport datasets from '../__test__/datasets.json'%0A%0Aexport function fetchMetrics(catalogId) %7B%0A  if (!catalogId) return Promise.reject(new Error('catalogId is required'));%0A  if (catalogId === '1') return Promise.resolve(metrics)%0A  return Promise.reject(new Error('metrics not found'))
 %0A%7D
-;
 %0A%0A
+export 
 func
@@ -537,28 +537,59 @@
 ion 
-_f(data) %7B%0A 
+fetchCatalog(catalogId) %7B%0A  if (!catalogId)
  return 
 new 
@@ -588,81 +588,108 @@
 urn 
-new 
 Promise
-( (resolve, reject) =%3E %7B%0A  
+.reject(new Error('catalogId is required'));%0A
   if (
-data) %7B%0A      return 
+catalogId === '1') return Promise.
 resolve(
 data
@@ -688,796 +688,506 @@
 lve(
-data)%0A    %7D else %7B%0A      return reject()%0A    %7D%0A  %7D)%0A%7D%0A%0Afunction waitDataAndSetState(dataPromise, component, stateName) %7B%0A  const cancelablePromise = makeCancelable(dataPromise%0A    .then(data =%3E %7B%0A      component.setState(%7BstateName%7D);%0A    %7D)%0A    .catch(err =%3E %7B%0A      if (!component.state.errors.includes(err.message)) %7B%0A        const errors = %5B...component.state.errors, err.message%5D%0A        component.setState(%7B errors %7D)%0A      %7D%0A      throw err%0A    %7D)%0A  )%0A%0A    component.setStat
+catalog)%0A  return Promise.reject(new Error('catalog not found'))%0A%7D%0A%0Aexport function fetchCatalogs() %7B%0A  return Promise.resolv
 e(
-%7B
 ca
-ncelablePromise%7D)%0A    return cancelablePromise%0A%7D%0A%0Aexport function fetchMetrics(component, catalogId) %7B%0A  if (!catalogId) return Promise.reject(new Error('catalogId is required'))%0A  const fetchPromise = catalogId === '1' ? _f(metrics) : _f()%0A  return waitDataAndSetState(fetchPromise, component, metrics
+talogs) // Finding a solution to Promise.reject()%0A%7D%0A%0Aexport function fetchHarvest(catalogId, harvestId) %7B%0A  if (!catalogId) return Promise.reject(new Error('catalogId is required'))%0A  if (!harvestId) return Promise.reject(new Error('harvestId is required'))%0A  if (catalogId === '1') return Promise.resolve(harvest)%0A  return Promise.reject(new Error('harvest not found')
 )%0A%7D%0A
@@ -1212,27 +1212,17 @@
 etch
-Catalog(component, 
+Harvests(
 cata
@@ -1312,275 +1312,233 @@
 )%0A  
-const fetchPromise = catalogId === '1' ? _f(catalog) : _f()%0A  return waitDataAndSetState(fetchPromise, component, catalog)%0A%7D%0A%0Aexport function fetchDatasets(component) %7B%0A  const fetchPromise = _f(datasets)%0A  return waitDataAndSetState(fetchPromise, component, datasets
+if (catalogId === '1') return Promise.resolve(harvests)%0A  return Promise.reject(new Error('harvests not found'))%0A%7D%0A%0Aexport function fetchDatasets() %7B%0A  return Promise.resolve(datasets) // Finding a solution to Promise.reject(
 )%0A%7D%0A

Update fetch.js mock