markbello-iguazu-rest
Version:
A Redux REST caching library that follows Iguazu patterns
96 lines (90 loc) • 2.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.queryResource = queryResource;
exports.queryCollection = queryCollection;
var _selectors = require("../selectors");
var _crud = require("./crud");
var _asyncSideEffects = require("./asyncSideEffects");
/*
* 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 queryResource(_ref) {
var resource = _ref.resource,
id = _ref.id,
opts = _ref.opts,
forceFetch = _ref.forceFetch;
return function (dispatch, getState) {
var state = getState();
var data = (0, _selectors.getResource)({
resource: resource,
id: id,
opts: opts
})(state);
var status = (0, _selectors.resourceIsLoaded)({
resource: resource,
id: id,
opts: opts
})(state) && !forceFetch ? 'complete' : 'loading';
var error = data instanceof Error && data;
var promise = dispatch((0, _crud.loadResource)({
resource: resource,
id: id,
opts: opts,
forceFetch: forceFetch
}));
(0, _asyncSideEffects.handleQueryPromiseRejection)(promise);
return {
data: data,
status: status,
error: error,
promise: promise
};
};
}
function queryCollection(_ref2) {
var resource = _ref2.resource,
id = _ref2.id,
opts = _ref2.opts,
forceFetch = _ref2.forceFetch;
return function (dispatch, getState) {
var state = getState();
var data = (0, _selectors.getCollection)({
resource: resource,
id: id,
opts: opts
})(state);
var status = (0, _selectors.collectionIsLoaded)({
resource: resource,
id: id,
opts: opts
})(state) && !forceFetch ? 'complete' : 'loading';
var error = data instanceof Error && data;
var promise = dispatch((0, _crud.loadCollection)({
resource: resource,
id: id,
opts: opts,
forceFetch: forceFetch
}));
(0, _asyncSideEffects.handleQueryPromiseRejection)(promise);
return {
data: data,
status: status,
error: error,
promise: promise
};
};
}