@stackend/api
Version:
JS bindings to api.stackend.com
201 lines • 8.96 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.groupBlogEntries = exports.hasBlogEntries = exports.getBlogEntries = exports.getGroupBlogState = exports.UPDATE_AUTH_BLOG = exports.UPDATE_GROUP_BLOG_ENTRY = exports.INVALIDATE_GROUP_BLOG_ENTRIES = exports.RECEIVE_GROUP_BLOG_ENTRIES = exports.REQUEST_GROUP_BLOG_ENTRIES = void 0;
var get_1 = __importDefault(require("lodash/get"));
var concat_1 = __importDefault(require("lodash/concat"));
var immutability_helper_1 = __importDefault(require("immutability-helper"));
var createReducer_1 = __importDefault(require("../api/createReducer"));
var api_1 = require("../api");
var PaginatedCollection_1 = require("../api/PaginatedCollection");
//Action Type
exports.REQUEST_GROUP_BLOG_ENTRIES = 'REQUEST_GROUP_BLOG_ENTRIES';
exports.RECEIVE_GROUP_BLOG_ENTRIES = 'RECEIVE_GROUP_BLOG_ENTRIES';
exports.INVALIDATE_GROUP_BLOG_ENTRIES = 'INVALIDATE_GROUP_BLOG_ENTRIES';
exports.UPDATE_GROUP_BLOG_ENTRY = 'UPDATE_GROUP_BLOG_ENTRY';
exports.UPDATE_AUTH_BLOG = 'UPDATE_AUTH_BLOG';
/**
* Get the state for a given blogKey
* @param groupBlogEntriesState
* @param blogKey
*/
function getGroupBlogState(groupBlogEntriesState, blogKey) {
return groupBlogEntriesState[blogKey] || null;
}
exports.getGroupBlogState = getGroupBlogState;
/**
* Get the blog entries
* @param groupBlogEntriesState
* @param blogKey
*/
function getBlogEntries(groupBlogEntriesState, blogKey) {
var _a;
var x = groupBlogEntriesState[blogKey];
if (x) {
return ((_a = x.json) === null || _a === void 0 ? void 0 : _a.resultPaginated) || null;
}
return null;
}
exports.getBlogEntries = getBlogEntries;
/**
* Check if relevant blog entries exists in store
* @param groupBlogEntriesState
* @param blogKey
* @param pageSize
* @param p
* @param categoryId
*/
function hasBlogEntries(groupBlogEntriesState, blogKey, pageSize, p, categoryId, goToBlogEntry) {
var pe = getBlogEntries(groupBlogEntriesState, blogKey);
if (pe) {
if (pe.page < p) {
return false;
}
if (pe.pageSize != pageSize) {
return false;
}
if (goToBlogEntry && !pe.entries.some(function (entry) { return entry.permalink === goToBlogEntry; })) {
return false;
}
return true;
// FIXME: categoryId included in request, but not key
}
return false;
}
exports.hasBlogEntries = hasBlogEntries;
// FIXME: The reducer stores a lot of crap. The entire json response
exports.groupBlogEntries = (0, createReducer_1.default)({}, {
REQUEST_GROUP_BLOG_ENTRIES: function (state, action) {
var _a;
return (0, immutability_helper_1.default)(state, (_a = {},
_a[action.blogKey] = {
$apply: function (context) {
return (0, immutability_helper_1.default)(context || {}, {
isFetching: { $set: true },
didInvalidate: { $set: false },
// @ts-ignore
json: { $set: (0, get_1.default)(state, "[".concat(action.blogKey, "].json"), {}) }
});
}
},
_a));
},
RECEIVE_GROUP_BLOG_ENTRIES: function (state, action) {
var _a, _b;
if (action.json.error) {
api_1.logger.warn(exports.RECEIVE_GROUP_BLOG_ENTRIES, 'Error:', action.blogKey, (0, api_1.getJsonErrorText)(action.json));
return (0, immutability_helper_1.default)(state, (_a = {},
_a[action.blogKey] = {
$apply: function (context) {
return (0, immutability_helper_1.default)(context || {}, {
isFetching: { $set: false },
didInvalidate: { $set: false },
lastUpdated: { $set: action.receivedAt },
json: { $merge: { resultPaginated: (0, PaginatedCollection_1.emptyPaginatedCollection)() } },
error: { $set: action.json.error }
});
}
},
_a));
}
// Combine the existing and new entries, update the existing if needed
var origEntries = (0, get_1.default)(getGroupBlogState(state, action.blogKey), 'json.resultPaginated.entries', []);
var addEntries = [];
action.json.resultPaginated.entries.forEach(function (e) {
var existingEntry = origEntries.find(function (o) { return o.id === e.id; });
if (existingEntry) {
Object.assign(existingEntry, e);
}
else {
addEntries.push(e);
}
});
var uniqueBlogEntries = (0, concat_1.default)(origEntries, addEntries);
//console.log("RECEIVE_GROUP_BLOG_ENTRIES", action.json);
return (0, immutability_helper_1.default)(state, (_b = {},
_b[action.blogKey] = {
$apply: function (context) {
return (0, immutability_helper_1.default)(context || {}, {
isFetching: { $set: false },
didInvalidate: { $set: false },
lastUpdated: { $set: action.receivedAt },
json: {
$apply: function (context) {
var op = {
resultPaginated: {
entries: { $set: uniqueBlogEntries }
}
};
/* FIXME: Improve this. The check is typically needed when this is called from saveEntry */
if (action.json.likes) {
op.likes = { $set: action.json.likes };
}
if (action.json.likesByCurrentUser) {
op.likesByCurrentUser = { $set: action.json.likesByCurrentUser };
}
if (action.json.blog) {
op.blog = { $set: action.json.blog };
}
if (action.json.authBlog) {
op.authBlog = { $set: action.json.authBlog };
}
if (action.json.blogKey) {
op.blogKey = { $set: action.json.blogKey };
}
if (action.json.categories) {
op.categories = { $set: action.json.categories };
}
return (0, immutability_helper_1.default)(Object.assign({}, context, { resultPaginated: action.json.resultPaginated }), op);
}
}
});
}
},
_b));
},
INVALIDATE_GROUP_BLOG_ENTRIES: function (state, action) {
var _a;
return __assign(__assign({}, state), (_a = {},
_a[action.blogKey] = {
didInvalidate: true
},
_a));
},
UPDATE_GROUP_BLOG_ENTRY: function (state, action) {
var _a, _b;
// Last index is the updated entry
var updatedBlogEntry = action.json.resultPaginated.entries[action.json.resultPaginated.entries.length - 1];
var indexOfUpdatedEntry = state[action.blogKey].json.resultPaginated.entries
.map(function (blogEntry) { return blogEntry.id; })
.indexOf(updatedBlogEntry.id);
return (0, immutability_helper_1.default)(state, (_a = {},
_a[action.blogKey] = {
isFetching: { $set: false },
didInvalidate: { $set: false },
lastUpdated: { $set: action.receivedAt },
json: {
resultPaginated: {
entries: (_b = {},
_b[indexOfUpdatedEntry] = { $set: updatedBlogEntry },
_b)
}
}
},
_a));
}
});
exports.default = exports.groupBlogEntries;
//# sourceMappingURL=groupBlogEntriesReducer.js.map