UNPKG

@stackend/api

Version:

JS bindings to api.stackend.com

241 lines 11.1 kB
"use strict"; 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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GroupComments = exports.COMMENT_REMOVED = exports.INVALIDATE_GROUP_COMMENTS = exports.UPDATE_COMMENT = exports.RECEIVE_COMMENTS = exports.REQUEST_COMMENTS = exports.RECEIVE_GROUP_COMMENTS = exports.REQUEST_GROUP_COMMENTS = void 0; // @flow var get_1 = __importDefault(require("lodash/get")); var concat_1 = __importDefault(require("lodash/concat")); var assign_1 = __importDefault(require("lodash/assign")); var immutability_helper_1 = __importDefault(require("immutability-helper")); var commentAction = __importStar(require("./commentAction")); var PaginatedCollection_1 = require("../api/PaginatedCollection"); //Action Type exports.REQUEST_GROUP_COMMENTS = 'REQUEST_GROUP_COMMENTS'; exports.RECEIVE_GROUP_COMMENTS = 'RECEIVE_GROUP_COMMENTS'; exports.REQUEST_COMMENTS = 'REQUEST_COMMENTS'; exports.RECEIVE_COMMENTS = 'RECEIVE_COMMENTS'; exports.UPDATE_COMMENT = 'UPDATE_COMMENT'; exports.INVALIDATE_GROUP_COMMENTS = 'INVALIDATE_GROUP_COMMENTS'; exports.COMMENT_REMOVED = 'COMMENT_REMOVED'; //Reducer function GroupComments(state, action) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r; var _s, _t, _u; if (state === void 0) { state = {}; } var key = ''; switch (action.type) { case exports.REQUEST_GROUP_COMMENTS: key = commentAction._getCommentsStateKey(action); return Object.assign({}, state, (_a = {}, _a[key] = { isFetching: true, didInvalidate: false, json: typeof state[key] !== 'undefined' ? state[key].json : '' }, _a)); case exports.RECEIVE_GROUP_COMMENTS: key = commentAction._getCommentsStateKey(action); if (state[key].json) { var json = state[key].json; json.comments = Object.assign({}, state[key].json.comments, action.json.comments); json.likesByCurrentUser = Object.assign({}, state[key].json.likesByCurrentUser, action.json.likesByCurrentUser); return Object.assign({}, state, (_b = {}, _b[key] = { isFetching: false, didInvalidate: false, lastUpdated: action.receivedAt, json: json }, _b)); } else { return Object.assign({}, state, (_c = {}, _c[key] = { isFetching: false, didInvalidate: false, lastUpdated: action.receivedAt, json: action.json }, _c)); } case exports.REQUEST_COMMENTS: { key = commentAction._getCommentsStateKey(action); var requestBlogEntryComments = Object.assign({}, state[key] && state[key].json ? state[key].json.comments[action.referenceId] : {}, { isFetching: true, didInvalidate: false }); var requestBlogEntiesWithComments = Object.assign({}, state[key] && state[key].json ? state[key].json.comments : {}, (_d = {}, _d[action.referenceId] = requestBlogEntryComments, _d)); var likesByCurrentUser = Object.assign({}, state[key] && state[key].json ? state[key].json.likesByCurrentUser : {}); return Object.assign({}, state, (_e = {}, _e[key] = { isFetching: true, didInvalidate: false, json: { comments: requestBlogEntiesWithComments, likesByCurrentUser: likesByCurrentUser } }, _e)); } case exports.RECEIVE_COMMENTS: { var referenceId = action.referenceId; key = commentAction._getCommentsStateKey(action); if (action.json.error) { var s = { isFetching: false, didInvalidate: false, lastUpdated: action.receivedAt, entries: [] }; // @ts-ignore return (0, immutability_helper_1.default)(state, (_f = {}, _f[key] = { isFetching: { $set: false }, didInvalidate: { $set: false }, lastUpdated: { $set: action.receivedAt }, error: { $set: action.json.error }, json: { comments: (_g = {}, _g[referenceId] = { $set: s }, _g) } }, _f)); } var origComments_1 = (0, get_1.default)(state, "[".concat(key, "].json.comments[").concat(referenceId, "].entries"), []); var newComments_1 = []; action.json.comments.entries.forEach(function (e) { var orig = origComments_1.find(function (o) { return o.id === e.id; }); if (orig) { (0, assign_1.default)(orig, e); } else { newComments_1.push(e); } }); var referenceIdUniqueComments = (0, concat_1.default)(origComments_1, newComments_1); var pagination = (0, get_1.default)(state, "[".concat(key, "].json.comments[").concat(referenceId, "]"), (0, PaginatedCollection_1.emptyPaginatedCollection)()); delete pagination['entries']; pagination.totalSize += action.json.comments.entries.length; var x = (0, immutability_helper_1.default)(action.json.comments, { entries: { $set: referenceIdUniqueComments } }); // Work around for $merge not being able to $set var first = typeof state[key] === 'undefined' || typeof state[key].json.comments[referenceId] === 'undefined'; var op = first ? '$set' : '$merge'; return (0, immutability_helper_1.default)(state, (_h = {}, _h[key] = { isFetching: { $set: false }, didInvalidate: { $set: false }, lastUpdated: { $set: action.receivedAt }, json: { likesByCurrentUser: { $merge: action.json.likesByCurrentUser }, comments: (_j = {}, _j[referenceId] = (_k = {}, _k[op] = __assign(__assign({}, pagination), x), _k), _j) } }, _h)); } case exports.UPDATE_COMMENT: { key = commentAction._getCommentsStateKey(action); var updatedComment = action.json; var indexOfUpdatedComment = state[key].json.comments[action.referenceId].entries .map(function (comment) { return comment.id; }) .indexOf(updatedComment.id); return (0, immutability_helper_1.default)(state, (_l = {}, _l[key] = { isFetching: { $set: false }, didInvalidate: { $set: false }, lastUpdated: { $set: action.receivedAt }, json: { comments: (_m = {}, _m[action.referenceId] = { entries: (_o = {}, _o[indexOfUpdatedComment] = { $set: action.json }, _o) }, _m) } }, _l)); } case exports.COMMENT_REMOVED: { key = commentAction._getCommentsStateKey(action); var entries = (_u = (_t = (_s = state[key]) === null || _s === void 0 ? void 0 : _s.json) === null || _t === void 0 ? void 0 : _t.comments[action.referenceId]) === null || _u === void 0 ? void 0 : _u.entries; if (!entries) { return state; } var idx = entries.findIndex(function (c) { return c.id === action.id; }); if (idx === -1) { return state; } return (0, immutability_helper_1.default)(state, (_p = {}, _p[key] = { lastUpdated: { $set: Date.now() }, json: { comments: (_q = {}, _q[action.referenceId] = { entries: { $splice: [[idx, 1]] } }, _q) } }, _p)); } case exports.INVALIDATE_GROUP_COMMENTS: key = commentAction._getCommentsStateKey(action); return Object.assign({}, state, (_r = {}, _r[key] = { didInvalidate: true }, _r)); default: return state; } } exports.GroupComments = GroupComments; exports.default = GroupComments; //# sourceMappingURL=commentReducer.js.map