UNPKG

@stackend/api

Version:

JS bindings to api.stackend.com

322 lines 16.3 kB
"use strict"; 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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.deleteForumThreadEntry = exports.likeForumThreadEntry = exports.receiveVoteForumThread = exports.rateForumThreadEntry = exports.updateForumThreadEntry = exports.receiveForumThreads = exports.requestForumThreads = exports.fetchForumThreadEntries = exports.fetchForumThreads = exports.editForumThread = void 0; // @flow var get_1 = __importDefault(require("lodash/get")); var forumApi = __importStar(require("./index")); var index_1 = require("./index"); var likeApi = __importStar(require("../like")); var api = __importStar(require("../api")); var forumActions = __importStar(require("./forumActions")); var forumThreadReducer_1 = require("./forumThreadReducer"); var likeActions_1 = require("../like/likeActions"); function editForumThread(_a) { var _this = this; var subject = _a.subject, text = _a.text, categoryId = _a.categoryId, forumThreadPermalink = _a.forumThreadPermalink, forumPermalink = _a.forumPermalink, _b = _a.isAI, isAI = _b === void 0 ? false : _b; return function (dispatch) { return __awaiter(_this, void 0, void 0, function () { var json; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, dispatch(forumApi.editForumThread({ subject: subject, text: text, categoryId: categoryId, forumThreadPermalink: forumThreadPermalink, forumPermalink: forumPermalink, isAI: isAI }))]; case 1: json = _a.sent(); if (json.error) { console.error(forumPermalink + ': ' + api.getJsonErrorText(json)); return [2 /*return*/, { error: "Couldn't edit forum thread :" + api.getJsonErrorText(json) }]; } return [2 /*return*/, json]; } }); }); }; } exports.editForumThread = editForumThread; /** * Requests and receive forumThreads and store them in redux-state */ function fetchForumThreads(_a) { var _this = this; var forumPermalink = _a.forumPermalink, page = _a.page, _b = _a.pageSize, pageSize = _b === void 0 ? 25 : _b; return function (dispatch) { return __awaiter(_this, void 0, void 0, function () { var json; return __generator(this, function (_a) { switch (_a.label) { case 0: dispatch(requestForumThreads()); return [4 /*yield*/, dispatch(forumApi.listThreads({ forumPermalink: forumPermalink, pageSize: pageSize, p: page }))]; case 1: json = _a.sent(); if (json.error) { console.error(forumPermalink + ': ' + api.getJsonErrorText(json)); return [2 /*return*/, { error: "Couldn't fetchForumThreads :" + api.getJsonErrorText(json) }]; } dispatch((0, likeActions_1.receiveLikes)(json.likes)); dispatch(forumActions.receiveForums({ entries: Object.keys(json.__relatedObjects) .filter(function (entryKey) { return json.__relatedObjects[entryKey].__type === 'net.josh.community.forum.impl.ForumImpl'; }) .reduce(function (obj, key) { return obj.concat(json.__relatedObjects[key]); }, []) })); dispatch(receiveForumThreads({ entries: (0, get_1.default)(json, 'threadsPaginated.entries', []), pageSize: pageSize, forumPermalink: forumPermalink })); return [2 /*return*/, json]; } }); }); }; } exports.fetchForumThreads = fetchForumThreads; /** * Requests and receive forumThreads and store them in redux-state * if entryId is specified we fetch the page that this entry is located on */ function fetchForumThreadEntries(_a) { var _this = this; var forumPermalink = _a.forumPermalink, forumThreadPermalink = _a.forumThreadPermalink, entryId = _a.entryId, _b = _a.pageSize, pageSize = _b === void 0 ? 15 : _b, p = _a.p; return function (dispatch) { return __awaiter(_this, void 0, void 0, function () { var data_1, e_1; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, dispatch(forumApi.listEntries({ forumPermalink: forumPermalink, forumThreadPermalink: forumThreadPermalink, entryId: entryId, pageSize: pageSize, p: p, isQna: forumPermalink === 'question' }))]; case 1: data_1 = _a.sent(); if (data_1.error) { console.error("couldn't fetchForumThreadEntries :", api.getJsonErrorText(data_1)); return [2 /*return*/, { error: "couldn't fetchForumThreadEntries :" + api.getJsonErrorText(data_1) }]; } dispatch((0, likeActions_1.receiveLikes)(data_1.likes)); dispatch(forumActions.receiveForums({ entries: Object.keys(data_1.__relatedObjects) .filter(function (entryKey) { return data_1.__relatedObjects[entryKey].__type === 'net.josh.community.forum.impl.ForumImpl'; }) .reduce(function (obj, key) { return obj.concat(data_1.__relatedObjects[key]); }, []) })); dispatch(receiveForumThreads({ entries: data_1.entriesPaginated.entries.concat(data_1.thread), forumPermalink: forumPermalink, pageSize: pageSize })); return [2 /*return*/, data_1.entriesPaginated]; case 2: e_1 = _a.sent(); console.error("couldn't fetchForumThreadEntries :", e_1); return [2 /*return*/, { error: "couldn't fetchForumThreadEntries :" + e_1 }]; case 3: return [2 /*return*/]; } }); }); }; } exports.fetchForumThreadEntries = fetchForumThreadEntries; function requestForumThreads() { return { type: forumThreadReducer_1.REQUEST_FORUM_THREADS }; } exports.requestForumThreads = requestForumThreads; function receiveForumThreads(_a) { var entries = _a.entries, forumPermalink = _a.forumPermalink, pageSize = _a.pageSize; return { type: forumThreadReducer_1.RECEIVE_FORUM_THREADS, entries: entries, forumPermalink: forumPermalink, pageSize: pageSize }; } exports.receiveForumThreads = receiveForumThreads; function updateForumThreadEntry(_a) { var entry = _a.entry, forumPermalink = _a.forumPermalink; return { type: forumThreadReducer_1.UPDATE_FORUM_THREAD_ENTRY, entry: entry, forumPermalink: forumPermalink }; } exports.updateForumThreadEntry = updateForumThreadEntry; /** * Requests and receive comments and store them in redux-state */ function rateForumThreadEntry(_a) { var _this = this; var forumThreadEntry = _a.forumThreadEntry, score = _a.score; return function (dispatch, getState) { return __awaiter(_this, void 0, void 0, function () { var forumId, voteJson, currentForum; return __generator(this, function (_a) { switch (_a.label) { case 0: forumId = forumThreadEntry.forumRef.id; return [4 /*yield*/, dispatch(forumApi.vote({ forumThreadEntry: forumThreadEntry, score: score }))]; case 1: voteJson = _a.sent(); currentForum = getState().forums.entries.filter(function (forum) { return !!forum && forum.id === forumId; })[0]; /* FIXME: re add this if (score === 2) { dispatch(sendEventToGA(forumApi.gaLikeEventObject({ forumThreadEntry }))); } else { dispatch(sendEventToGA(forumApi.gaDislikeEventObject({ forumThreadEntry }))); } */ return [2 /*return*/, dispatch(receiveVoteForumThread({ voteJson: voteJson, forumPermalink: currentForum.permalink }))]; } }); }); }; } exports.rateForumThreadEntry = rateForumThreadEntry; function receiveVoteForumThread(_a) { var voteJson = _a.voteJson, forumPermalink = _a.forumPermalink; return { type: forumThreadReducer_1.RECEIVE_VOTE_FORUM_THREAD, voteJson: voteJson, forumPermalink: forumPermalink }; } exports.receiveVoteForumThread = receiveVoteForumThread; function likeForumThreadEntry(_a) { var _this = this; var referenceId = _a.referenceId, likedByCurrentUser = _a.likedByCurrentUser, context = _a.context; return function (dispatch, getState) { return __awaiter(_this, void 0, void 0, function () { var forumThreadEntry, obfuscatedReference, forumPermalink, receivedLikes; return __generator(this, function (_a) { switch (_a.label) { case 0: forumThreadEntry = forumApi.getThreadEntryFromRedux({ forumThreads: getState().forumThreads, id: referenceId }); if (!forumThreadEntry) { throw Error("Can't find forumThreadEntry in redux Store"); } obfuscatedReference = (0, get_1.default)(forumThreadEntry, ".obfuscatedReference"); forumPermalink = (0, get_1.default)(forumThreadEntry, ".forumRef.permalink", undefined); if (!forumPermalink) { return [2 /*return*/]; } if (!obfuscatedReference) { throw Error("can't get obfuscatedReference from forumThreadEntry in redux"); } if (!likedByCurrentUser) return [3 /*break*/, 2]; return [4 /*yield*/, dispatch(likeApi.removeLike({ obfuscatedReference: obfuscatedReference, context: context }))]; case 1: receivedLikes = _a.sent(); return [3 /*break*/, 4]; case 2: return [4 /*yield*/, dispatch(likeApi.like({ obfuscatedReference: obfuscatedReference, context: context }))]; case 3: receivedLikes = _a.sent(); _a.label = 4; case 4: return [2 /*return*/, dispatch(_receiveLikeForumThreadEntry({ receivedLikes: receivedLikes, referenceId: referenceId, forumPermalink: forumPermalink }))]; } }); }); }; } exports.likeForumThreadEntry = likeForumThreadEntry; function _receiveLikeForumThreadEntry(_a) { var receivedLikes = _a.receivedLikes, forumPermalink = _a.forumPermalink, referenceId = _a.referenceId; return { type: forumThreadReducer_1.RECEIVE_LIKE_FORUM_THREAD, receivedLikes: receivedLikes, referenceId: referenceId, forumPermalink: forumPermalink }; } /** * Delete forum thread entries and update state */ function deleteForumThreadEntry(_a) { var _this = this; var forumThreadEntryId = _a.forumThreadEntryId, modalName = _a.modalName; return function (dispatch /*, getState: GetState*/) { return __awaiter(_this, void 0, void 0, function () { var json; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, dispatch((0, index_1.removeForumThreadEntry)({ entryId: forumThreadEntryId }))]; case 1: json = _a.sent(); !!json.entry && dispatch(_deleteForumThreadEntry({ entry: json.entry })); return [2 /*return*/]; } }); }); }; } exports.deleteForumThreadEntry = deleteForumThreadEntry; function _deleteForumThreadEntry(_a) { var entry = _a.entry; return { type: forumThreadReducer_1.DELETE_FORUM_THREAD, entry: entry }; } //# sourceMappingURL=forumThreadActions.js.map