spatineo / tuulituhohaukka

0 stars 0 forks source link

Hahmotellaan rajapinta katalogitiedon hakemiselle #24

Open sampov2 opened 3 years ago

sampov2 commented 3 years ago

Hahmotellaan API, jota käyttöliittymä käyttää hakeakseen katalogilta tiedot

sampov2 commented 3 years ago
import { store } from '../App'
import { loadRootCatalog } from '../Store/Actions/data'

interface RootCatalog {
  type?: string,
  stack_version?: string,
  description?: string,
  links?: Array<any>
}

interface AllDatasetsType {
  datasets: [{
    id: string,
    title: string
  }]
}

// Helper function 
function getCatalog(store, url) {
  const ReduxState = store.getState()
  const ret = ReduxState.dataReducer.cache[url];
  if (!ret) {
    store.dispatch(loadCatalog(url))
  }
  return ret || {};
}

export const getAllDatasets : AllDatasetsType = () => {
  // 1. get root catalog
  // 2. get all dataset catalogs
  // 3. read id and title from each dataset catalog and return them as an array of objects { datasets: [{id: 'foo', title: 'bar'}, {...} ]}

  const ReduxState = store.getState()
  const rootCatalog: RootCatalog = ReduxState.dataReducer.cache.catalog

  // Check if object is empty
  if (rootCatalog && Object.keys(rootCatalog).length === 0) {
    store.dispatch(loadCatalog('/Testdata/root.json'))
    return { datasets: [] }
  }
  else {
    console.log('Rootcatalog in api function: ', rootCatalog)
    if (rootCatalog.links) {
      const dataSets = rootCatalog.links.filter((link: any) => link.rel === 'child').map(link => {
        var catalog = getCatalog(store, link.href);
        /*
        var catalog = ReduxState.dataReducer.cache[link.href];
        if (!catalog) {
          store.dispatch(loadCatalog(link.href))
        }
        */
        return catalog || {};
      })

      return { dataSets: dataSets }
    }
  }
}

export const getBandsForDataset = (id : string) => {
  // 1. get root catalog
  // 2. get all dataset catalogs
  // 3. find the dataset catalog with given id
  // 4. return bands from selecte dataset catalog contents
  return { bands: [{ id: 'foo'}] };
}

export const getItemsForDatasetAndTime = (datasetId : string, inspectionTime : string) => {
  // 1. get root catalog
  // 2. get all dataset catalogs
  // 3. find the dataset catalog with given id
  // 4. identify dataset-time catalog that overlap with "inspectionTime" and the next dataset-time catalog (in time order)
  // 5. get dataset-time catalogs that were identified in step 4
  // 6. get all items in the two dataset-time catalogs identified in step 4
  // 7. place items from step 6 in order (per time) and select the first item where startdate (or time) is after inspectionTime 
  // 8. return data

  return { items: [ /* items */ ] };

  // If a catalog or item is not loaded yet, start loading it and do not proceed further, return an empty result: { items: [] }
}