UNPKG

@stackend/api

Version:

JS bindings to api.stackend.com

302 lines 12.8 kB
"use strict"; 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.fetchGroupMembers = exports.receiveGroupMembers = exports.receiveGroupsAuth = exports.invalidateGroups = exports.receiveGroups = exports.requestGroups = exports.applyForMembership = exports.unsubscribe = exports.subscribe = exports.addGroup = exports.fetchGroup = exports.getGroupByPermalink = exports.getGroupById = exports.getMyGroups = exports.fetchMyGroups = exports.RECEIVE_GROUP_MEMBERS = exports.RECEIVE_GROUPS_AUTH = exports.INVALIDATE_GROUPS = exports.RECEIVE_GROUPS = exports.REQUEST_GROUPS = void 0; // @flow var get_1 = __importDefault(require("lodash/get")); var index_1 = require("./index"); var LoadingState_1 = __importDefault(require("../api/LoadingState")); exports.REQUEST_GROUPS = 'REQUEST_GROUPS'; exports.RECEIVE_GROUPS = 'RECEIVE_GROUPS'; exports.INVALIDATE_GROUPS = 'INVALIDATE_GROUPS'; exports.RECEIVE_GROUPS_AUTH = 'RECEIVE_GROUPS_AUTH'; exports.RECEIVE_GROUP_MEMBERS = 'RECEIVE_GROUP_MEMBERS'; /** * Fetch my groups * @param refresh */ function fetchMyGroups(refresh) { var _this = this; if (refresh === void 0) { refresh = false; } return function (dispatch, getState) { return __awaiter(_this, void 0, void 0, function () { var groups, json; return __generator(this, function (_a) { switch (_a.label) { case 0: groups = getState().groups; if (!(groups.myGroups.loadingState === LoadingState_1.default.NOT_STARTED || (refresh && groups.myGroups.loadingState === LoadingState_1.default.READY))) return [3 /*break*/, 2]; dispatch(requestGroups(true)); return [4 /*yield*/, dispatch((0, index_1.listMyGroups)({}))]; case 1: json = _a.sent(); dispatch(receiveGroups({ entries: json.groups.entries, mine: true })); dispatch(receiveGroupsAuth({ entries: json.groupAuth })); _a.label = 2; case 2: return [2 /*return*/]; } }); }); }; } exports.fetchMyGroups = fetchMyGroups; /** * Get my groups from * @param groups */ function getMyGroups(groups) { if (groups.myGroups.loadingState !== LoadingState_1.default.READY) { return []; } var r = []; groups.myGroups.ids.forEach(function (id) { var g = groups.entries[id]; if (g) { r.push(g); } }); return r; } exports.getMyGroups = getMyGroups; /** * Get a group by id from the store * @param groups * @param id */ function getGroupById(groups, id) { return groups.entries[id] || null; } exports.getGroupById = getGroupById; /** * Get a group by permalink from the store * @param groups * @param permalink */ function getGroupByPermalink(groups, permalink) { var id = groups.idByPermalink[permalink]; if (!id) { return null; } return getGroupById(groups, id); } exports.getGroupByPermalink = getGroupByPermalink; /** * Request a group */ function fetchGroup(_a) { var _this = this; var groupPermalink = _a.groupPermalink, groupId = _a.groupId, _b = _a.refresh, refresh = _b === void 0 ? false : _b; return function (dispatch, getState) { return __awaiter(_this, void 0, void 0, function () { var groups_1, group, id, json; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!refresh) { groups_1 = getState().groups; group = null; if (groupId) { group = groups_1.entries[groupId]; } else { id = Object.keys(groups_1.entries).find(function (id) { var _a; return ((_a = groups_1.entries[id]) === null || _a === void 0 ? void 0 : _a.permalink) === groupPermalink; }); group = groups_1.entries[id]; } if (group) { return [2 /*return*/, group]; } } dispatch(requestGroups()); return [4 /*yield*/, dispatch((0, index_1.getGroup)({ groupPermalink: groupPermalink, groupId: groupId }))]; case 1: json = _a.sent(); dispatch(receiveGroups({ entries: json.group ? [json.group] : [] })); return [2 /*return*/, json.group]; } }); }); }; } exports.fetchGroup = fetchGroup; /** * Request a group * @deprecated use fetchGroup instead */ exports.addGroup = fetchGroup; /** * Subscribe to a group * @param groupPermalink * @param groupId */ function subscribe(_a) { var _this = this; var groupPermalink = _a.groupPermalink, groupId = _a.groupId; return function (dispatch /*, getState: any*/) { return __awaiter(_this, void 0, void 0, function () { var json, myGroups; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, dispatch((0, index_1.subscribe)({ groupPermalink: groupPermalink, groupId: groupId }))]; case 1: json = _a.sent(); console.log('subscribe', json); try { dispatch(fetchGroupMembers({ groupId: json.groupId })); } catch (e) { console.error("Error: couldn't find groupId in subscribe response:", e); } return [4 /*yield*/, dispatch((0, index_1.listMyGroups)({}))]; case 2: myGroups = _a.sent(); dispatch(receiveGroupsAuth({ entries: (0, get_1.default)(myGroups, 'groupAuth') })); return [2 /*return*/, json]; } }); }); }; } exports.subscribe = subscribe; /** * Unsubscribe from a group * @param groupPermalink * @param groupId */ function unsubscribe(_a) { var _this = this; var groupPermalink = _a.groupPermalink, groupId = _a.groupId; return function (dispatch) { return __awaiter(_this, void 0, void 0, function () { var json, myGroups; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, dispatch((0, index_1.unsubscribe)({ groupPermalink: groupPermalink, groupId: groupId }))]; case 1: json = _a.sent(); try { dispatch(fetchGroupMembers({ groupId: json.groupId })); } catch (e) { console.error("Error: couldn't find groupId in unsubscribe response:", e); } return [4 /*yield*/, dispatch((0, index_1.listMyGroups)({}))]; case 2: myGroups = _a.sent(); dispatch(receiveGroupsAuth({ entries: (0, get_1.default)(myGroups, 'groupAuth') })); return [2 /*return*/, json]; } }); }); }; } exports.unsubscribe = unsubscribe; function applyForMembership(_a) { var _this = this; var groupPermalink = _a.groupPermalink, groupId = _a.groupId; return function (dispatch /*, getState: any*/) { return __awaiter(_this, void 0, void 0, function () { var json, myGroups; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, dispatch((0, index_1.applyForMembership)({ groupPermalink: groupPermalink, groupId: groupId }))]; case 1: json = _a.sent(); return [4 /*yield*/, dispatch((0, index_1.listMyGroups)({}))]; case 2: myGroups = _a.sent(); dispatch(receiveGroupsAuth({ entries: (0, get_1.default)(myGroups, 'groupAuth') })); return [2 /*return*/, json]; } }); }); }; } exports.applyForMembership = applyForMembership; function requestGroups(mine) { return { type: exports.REQUEST_GROUPS, mine: mine }; } exports.requestGroups = requestGroups; function receiveGroups(_a) { var entries = _a.entries, mine = _a.mine; return { type: exports.RECEIVE_GROUPS, entries: entries, mine: mine }; } exports.receiveGroups = receiveGroups; function invalidateGroups() { return { type: exports.INVALIDATE_GROUPS }; } exports.invalidateGroups = invalidateGroups; /** * Receives a list of the current users auth-status for all groups the user is part of. */ function receiveGroupsAuth(_a) { var entries = _a.entries; return { type: exports.RECEIVE_GROUPS_AUTH, entries: entries }; } exports.receiveGroupsAuth = receiveGroupsAuth; function receiveGroupMembers(_a) { var groupMembers = _a.groupMembers; return { type: exports.RECEIVE_GROUP_MEMBERS, groupMembers: groupMembers }; } exports.receiveGroupMembers = receiveGroupMembers; /** * Requests and receive comments and store them in redux-state */ function fetchGroupMembers(_a) { var _this = this; var groupId = _a.groupId, groupPermalink = _a.groupPermalink; return function (dispatch) { return __awaiter(_this, void 0, void 0, function () { var json; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, dispatch((0, index_1.listMembers)({ groupId: groupId, groupPermalink: groupPermalink }))]; case 1: json = _b.sent(); dispatch(receiveGroups({ entries: json.group })); dispatch(receiveGroupMembers({ groupMembers: (_a = {}, _a[json.groupId] = json.groupMembers, _a) })); return [2 /*return*/, json]; } }); }); }; } exports.fetchGroupMembers = fetchGroupMembers; //# sourceMappingURL=groupActions.js.map