UNPKG

markbello-iguazu-rest

Version:

A Redux REST caching library that follows Iguazu patterns

170 lines (156 loc) 4.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.loadResource = loadResource; exports.loadCollection = loadCollection; exports.createResource = createResource; exports.updateResource = updateResource; exports.destroyResource = destroyResource; exports.patchResource = patchResource; var _selectors = require("../selectors"); var _executeFetch = _interopRequireDefault(require("./executeFetch")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /* * Copyright 2018 American Express Travel Related Services Company, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express * or implied. See the License for the specific language governing * permissions and limitations under the License. */ function loadResource(_ref) { var resource = _ref.resource, id = _ref.id, opts = _ref.opts, forceFetch = _ref.forceFetch; return function (dispatch, getState) { var state = getState(); var promise; if ((0, _selectors.resourceIsLoaded)({ resource: resource, id: id })(state) && !forceFetch) { var data = (0, _selectors.getResource)({ resource: resource, id: id })(state); promise = data instanceof Error ? Promise.reject(data) : Promise.resolve(data); } else if ((0, _selectors.resourceIsLoading)({ resource: resource, id: id })(state) && !forceFetch) { promise = (0, _selectors.getResourceLoadPromise)({ resource: resource, id: id })(state); } else { promise = dispatch((0, _executeFetch.default)({ resource: resource, id: id, opts: opts, actionType: 'LOAD' })); } return promise; }; } function loadCollection(_ref2) { var resource = _ref2.resource, id = _ref2.id, opts = _ref2.opts, forceFetch = _ref2.forceFetch; return function (dispatch, getState) { var state = getState(); var promise; if ((0, _selectors.collectionIsLoaded)({ resource: resource, id: id, opts: opts })(state) && !forceFetch) { var data = (0, _selectors.getCollection)({ resource: resource, id: id, opts: opts })(state); promise = data instanceof Error ? Promise.reject(data) : Promise.resolve(data); } else if ((0, _selectors.collectionIsLoading)({ resource: resource, id: id, opts: opts })(state) && !forceFetch) { promise = (0, _selectors.getCollectionLoadPromise)({ resource: resource, id: id, opts: opts })(state); } else { promise = dispatch((0, _executeFetch.default)({ resource: resource, id: id, opts: opts, actionType: 'LOAD_COLLECTION' })); } return promise; }; } function createResource(_ref3) { var resource = _ref3.resource, id = _ref3.id, opts = _ref3.opts; return function (dispatch) { return dispatch((0, _executeFetch.default)({ resource: resource, id: id, opts: opts, actionType: 'CREATE' })); }; } function updateResource(_ref4) { var resource = _ref4.resource, id = _ref4.id, opts = _ref4.opts; return function (dispatch) { return dispatch((0, _executeFetch.default)({ resource: resource, id: id, opts: opts, actionType: 'UPDATE' })); }; } function destroyResource(_ref5) { var resource = _ref5.resource, id = _ref5.id, opts = _ref5.opts; return function (dispatch) { return dispatch((0, _executeFetch.default)({ resource: resource, id: id, opts: opts, actionType: 'DESTROY' })); }; } function patchResource(_ref6) { var resource = _ref6.resource, id = _ref6.id, opts = _ref6.opts; return function (dispatch) { return dispatch((0, _executeFetch.default)({ resource: resource, id: id, opts: opts, actionType: 'PATCH' })); }; }