All files createIterableSetRequestStatestState.js

100% Statements 24/24
100% Branches 6/6
100% Functions 8/8
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32    5x 136x 136x 136x 136x   136x 108x 64x 64x 4x   64x 64x 6x   58x   64x     136x 7x 5x 3x   136x      
import createIterableRequestState from "./createIterableRequestState"
 
const createIterableSetRequestState = (setResource) => {
  const result = (valueOrFn) => delegateSetResource(valueOrFn)
  result.setLoading = (valueOrFn) => delegateSetResource(valueOrFn, 'loading')
  result.setError = (valueOrFn) => delegateSetResource(valueOrFn, 'error')
  result.setData = (valueOrFn) => delegateSetResource(valueOrFn, 'data')
  //todo refactor
  const delegateSetResource = (valueOrFn, key) => {
    return setResource((resource) => {
      let resourceOrValue = valueOrFn
      if (typeof valueOrFn === 'function') {
        resourceOrValue = valueOrFn(key ? resource[key] : resource)
      }
      let newResource = {...resource}
      if (key) {
        newResource[key] = resourceOrValue
      } else {
        newResource = resourceOrValue
      }
      return createIterableRequestState(newResource)
    })
  }
  result[Symbol.iterator] = function* () {
    yield result['setLoading']
    yield result['setError']
    yield result['setData']
  }
  return result
}
 
export default createIterableSetRequestState