UNPKG

redux-resource

Version:
134 lines (109 loc) 5.99 kB
'use strict'; exports.__esModule = true; exports.default = getStatus; var _requestStatuses = require('./request-statuses'); var _requestStatuses2 = _interopRequireDefault(_requestStatuses); var _getPathParts = require('./get-path-parts'); var _getPathParts2 = _interopRequireDefault(_getPathParts); var _warning = require('./warning'); var _warning2 = _interopRequireDefault(_warning); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function getSingleStatus(state, statusLocation, treatIdleAsPending) { const splitPath = (0, _getPathParts2.default)(statusLocation); let status; let currentVal = state; for (let i = 0; i < splitPath.length; i++) { const pathValue = currentVal[splitPath[i]]; if (typeof pathValue === 'undefined') { status = _requestStatuses2.default.IDLE; break; } else if (i === splitPath.length - 1) { status = pathValue; } currentVal = pathValue; } if (process.env.NODE_ENV !== 'production') { const isStatus = status === _requestStatuses2.default.IDLE || status === _requestStatuses2.default.PENDING || status === _requestStatuses2.default.FAILED || status === _requestStatuses2.default.SUCCEEDED; if (!isStatus) { (0, _warning2.default)(`You called "getStatus" with path "${statusLocation}", which resolved ` + `to a value that is not a valid resource status. You may want to ` + `check that this path is correct. ` + `Read more about getStatus on the documentation page: ` + `https://redux-resource.js.org/docs/api-reference/get-status.html`, 'GET_STATUS_PATH'); } if (status === 'NULL') { (0, _warning2.default)(`You called "getStatus" with path "${statusLocation}", which resolved ` + `to a value of NULL. The NULL request status was renamed to IDLE in ` + `Redux Resource v3.0.0. You may need to verify that your code is ` + `compatible with Redux Resource v3.0.0. ` + `For more information, refer to the documentation for request statuses at: ` + `https://redux-resource.js.org/docs/api-reference/request-statuses.html\n\n` + `Also, the migration guide to Redux Resource v3 can be found at: ` + `https://github.com/jamesplease/redux-resource/blob/master/packages/redux-resource/docs/migration-guides/2-to-3.md`, 'INVALID_REQUEST_STATUS_GET_STATUS'); } } const isPending = status === _requestStatuses2.default.PENDING; const isIdle = status === _requestStatuses2.default.IDLE; const treatIdleAsPendingBool = Boolean(treatIdleAsPending); return { idle: isIdle && !treatIdleAsPendingBool, pending: isPending || isIdle && treatIdleAsPendingBool, failed: status === _requestStatuses2.default.FAILED, succeeded: status === _requestStatuses2.default.SUCCEEDED }; } // Returns the status of a particular CRUD action based on `statusLocation`. // // `state`: A piece of the Redux store containing the relevant resources // `action`: The CRUD action in question // `statusLocation`: A location of the meta resource (see `find-meta.js` for more) // `treatIdleAsPending`: Whether or not to count a status of `IDLE` as pending. // // Returns an Object with the following properties: // // { // idle: false, // failed: false, // pending: false, // succeeded: true, // } // // Note that at most _one_ of those properties will be true. It is // possible for them to all be false. function getStatus(state, statusLocations, treatIdleAsPending) { if (!Array.isArray(statusLocations)) { const status = getSingleStatus(state, statusLocations, treatIdleAsPending); if (process.env.NODE_ENV !== 'production') { Object.defineProperty(status, 'null', { get() { (0, _warning2.default)(`You attempted to access a property named "null" from the object returned by ` + `the getStatus method from Redux Resource. This property has been renamed to "idle" ` + `in Redux Resource v3. Please update your application to ` + `use the "idle" rather than "null". For more information, refer to the ` + `documentation for getStatus at: ` + `https://redux-resource.js.org/docs/api-reference/get-status.html\n\n` + `Also, the migration guide to Redux Resource v3 can be found at: ` + `https://github.com/jamesplease/redux-resource/blob/master/packages/redux-resource/docs/migration-guides/2-to-3.md`, `NULL_GET_STATUS_VALUE_ACCESSED`); } }); } return status; } const statusValues = statusLocations.map(loc => getSingleStatus(state, loc, treatIdleAsPending)); let idleValue = true; let pending = false; let failed = false; let succeeded = false; let successCount = 0; let pendingCount = 0; for (let i = 0; i < statusValues.length; i++) { const status = statusValues[i]; if (status.failed) { idleValue = false; failed = true; break; } else if (status.pending) { pendingCount++; } else if (status.succeeded) { successCount++; } } if (!failed && pendingCount > 0) { idleValue = false; pending = true; } else if (successCount === statusValues.length) { idleValue = false; succeeded = true; } const status = { idle: idleValue, pending, failed, succeeded }; if (process.env.NODE_ENV !== 'production') { Object.defineProperty(status, 'null', { get() { (0, _warning2.default)(`You attempted to access a property named "null" from the object returned by ` + `the getStatus method from Redux Resource. This property has been renamed to "idle" ` + `in Redux Resource v3. Please update your application to ` + `use the "idle" rather than "null". For more information, refer to the ` + `documentation for getStatus at: ` + `https://redux-resource.js.org/docs/api-reference/get-status.html\n\n` + `Also, the migration guide to Redux Resource v3 can be found at: ` + `https://github.com/jamesplease/redux-resource/blob/master/packages/redux-resource/docs/migration-guides/2-to-3.md`, `NULL_GET_STATUS_VALUE_ACCESSED`); } }); } return status; }