@copart/reference-data

0.0.16 • Public • Published

Reference Data Library

NPM Version

Installation

npm i @copart/reference-data

Library to fetch the Reference Data when provided the basic configuration.

Basically it is using the polyfill for React.createContext API, called create-react-context (it is this library's dependency). As we are using react version < 16, we are using it. If we upgrade it to a version > 16, then we can change it to use React.createContext.

Example Usage

import { ReferenceData } from '@copart/reference-data'

The root component which is going to use reference data context needs to be wrapped by ReferenceData.Provider and a params props needs to be passed as below

<ReferenceData.Provider params={params}> // param format is given below
  <div>
  </div>
</ReferenceData.Provider>

Any descendants of Provider can use the reference data context as below:

<ReferenceData.Consumer>
  {(referenceData) => (
    referenceData.items // items is being set in the context
  }
</ReferenceData.Consumer>

Param format

{
  refData: REFERENCE_DATA_DEPENDENCIES, // example given below
  countryCode,
  xhrConfig,
}

Example configuration format

REFERENCE_DATA_DEPENDENCIES:-

  {[MAKE]: entity(
    'makes',
    '/cobalt/${country}/api/v1/references/makes?skip_pagination=true&lot_type_cd=${lot_type_cd}',
    'make_id',
    'make_nm',
    { lot_type_cd: 'V' },
    ['data', 'data']
  )}
  • [MAKE] -> Key with which we can get the particular reference data.
  • 'makes' -> Will be used to show errorMessage // not being used currently
  • url -> url to fetch the entity's reference data
  • 'make_id' -> key
  • 'make_nm' -> parameter used for sorting // not being used currently
  • { lot_type_cd: 'V' } -> params passed, should match the variable name in the url inside the curly braces
  • ['data', 'data'] -> this will indicate the path from which we're getting the response

xhrConfig example -

export const xhrConfig = (xhrConf) => {
  const accessToken = getAccessToken() // will be taken by cookies/localStorage
  const headers = xhrConf.headers
  if (xhrConf.url.indexOf('solrg') !== -1) {
    headers['Cache-Control'] = 'no-store, must-revalidate, no-cache, max-age=0'
    headers.Expires = 'Mon, 01 Jan 1990 00:00:00 GMT'
    headers.Pragma = 'no-cache'
    return xhrConf
  }
  if (
    xhrConf.url.indexOf('referencedata-ws') !== -1 &&
    xhrConf.url.indexOf('referencedata-ws/states') === -1 &&
    xhrConf.url.indexOf('sprocs') === -1
  ) {
    headers.Version = '2.0'
  }
  if (accessToken && headers) {
    headers.Authorization = `bearer ${accessToken}`
    headers['Content-Type'] = 'application/json'
    headers.AUTHORIZATIONROLE = 'germany_executive'
    headers.SELECTEDYARD = 5001
    headers.LANGUAGECODE = 'en'
    headers.partnerCode = 'en'
    headers.country = 'DEU'
    headers.site = 'CPRTDE'
  }
  headers.correlationID = `cobalt-uuid-${uuid()}`
  return xhrConf
}

This data will be available to all the reference data requests using axios.interceptors

Response and how to fetch Dependency Reference Data

Response can be used by Render callback pattern (https://reactpatterns.com/),

  • response has two properties (items and actions)

  • items is an object again having two properties, success and failure

    • Success Example ->
  key -> stlmt_model
  value -> {
    "ids":["S","V"],
    "entities":{
      "S": {
        "stlmt_model_cd":"S","stlmt_model_desc":"Standard","status":"A","source_sys":"DataLoad","index":0
      },
      "V": {
        "stlmt_model_cd":"V","stlmt_model_desc":"Vermittler","status":"A","source_sys":"DataLoad","index":1
      }
    }
  }
  • actions has getNewData (for now this is the only function is there), which will help us in fetching the dependent reference data. Most of the times the reference data is fetched at the very beginning. But, in some cases we need to fetch the reference data as an entity changes, for example - if we are changing the Make, then we need to fetch the Models according to the new Make. we need to pass configuration of the dependent entity here in the same format as above.

Dependents (0)

Package Sidebar

Install

npm i @copart/reference-data

Weekly Downloads

20

Version

0.0.16

License

none

Unpacked Size

340 kB

Total Files

6

Last publish

Collaborators

  • omkarkyatham
  • saiteja.murarisetty
  • mapothani
  • shivani_padma
  • saiprasad_3103
  • dineshgrandhi--copart
  • yilinliu7
  • dbkokkirig
  • mdelong422
  • chandrashekar.gandla
  • arpeechara
  • ravikirancopart
  • akashpatti
  • nikhilnarula
  • shwetanairco
  • lizhangxcopart
  • raghurudraraju-copart
  • adganapath
  • jagannadhtipirneni
  • prasanth.ivs