UNPKG

@risecx/carespace-chat-ui

Version:

carespace-chat-ui React component

184 lines (148 loc) 7.05 kB
import _regeneratorRuntime from "@babel/runtime/regenerator"; function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } import React, { Component, Fragment } from 'react'; import axios from 'axios'; import Loading from '../Loading'; import ProgramEmptyMessage from './ProgramsEmptyMessage'; import { reactLocalStorage } from 'reactjs-localstorage'; import './Programs.css'; var Programs = /*#__PURE__*/function (_Component) { _inheritsLoose(Programs, _Component); function Programs(props) { var _this; _this = _Component.call(this, props) || this; _defineProperty(_assertThisInitialized(_this), "onClickHandler", function (item) { _this.props.onPayload({ payload: item.welcomeMessage.conversation, title: item.name }); setTimeout(_this.props.onSelect, 100); }); _defineProperty(_assertThisInitialized(_this), "toggleSubscribeHandler", /*#__PURE__*/function () { var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(programId) { var user, urlAPI, _yield$axios$post, data, programIndex, programs; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: user = reactLocalStorage.get('userId'); urlAPI = _this.props.clientConfig.urlAPI; _context.next = 4; return axios.post(urlAPI + "/programs/" + programId + "/subscribe", { _id: user }); case 4: _yield$axios$post = _context.sent; data = _yield$axios$post.data; programIndex = _this.state.programsList.findIndex(function (p) { return p._id === programId; }); programs = [].concat(_this.state.programsList); programs[programIndex] = data; _this.setState({ programsList: programs }); case 10: case "end": return _context.stop(); } } }, _callee); })); return function (_x) { return _ref.apply(this, arguments); }; }()); _this.state = { programsList: [], loading: true }; return _this; } var _proto = Programs.prototype; _proto.componentWillMount = /*#__PURE__*/function () { var _componentWillMount = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() { var urlAPI, _yield$axios$get, data; return _regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: urlAPI = this.props.clientConfig.urlAPI; _context2.next = 3; return axios.get(urlAPI + "/programs"); case 3: _yield$axios$get = _context2.sent; data = _yield$axios$get.data; this.setState({ programsList: data.filter(function (p) { return p["public"]; }), loading: false }); case 6: case "end": return _context2.stop(); } } }, _callee2, this); })); function componentWillMount() { return _componentWillMount.apply(this, arguments); } return componentWillMount; }(); _proto.isSubscribed = function isSubscribed(users) { var user = reactLocalStorage.get('user'); if (user) { var _JSON$parse = JSON.parse(user), _id = _JSON$parse._id; return users.includes(_id); } return false; }; _proto.render = function render() { var _this2 = this; var loading = this.state.loading; return /*#__PURE__*/React.createElement("div", { className: "programs-container" }, /*#__PURE__*/React.createElement(Loading, this.state), this.isEmpty && /*#__PURE__*/React.createElement(ProgramEmptyMessage, null), !loading && !this.isEmpty && /*#__PURE__*/React.createElement("div", { className: "programs-list" }, /*#__PURE__*/React.createElement("ul", null, this.state.programsList.map(function (program, index) { return /*#__PURE__*/React.createElement("li", { key: index }, /*#__PURE__*/React.createElement("a", null, program.name), _this2.isSubscribed(program.users) ? /*#__PURE__*/React.createElement("button", { className: "unsubscribe", title: "Unsubscribe", onClick: function onClick() { return _this2.toggleSubscribeHandler(program._id); } }, "Unsubscribe") : /*#__PURE__*/React.createElement("button", { className: "subscribe", onClick: function onClick() { return _this2.toggleSubscribeHandler(program._id); } }, "Subscribe")); })))); }; _createClass(Programs, [{ key: "computedProgramsList", get: function get() { return this.state.programsList.filter(function (p) { return !!p.welcomeMessage.conversation; }); } }, { key: "isEmpty", get: function get() { return this.state.programsList.length === 0; } }]); return Programs; }(Component); export { Programs as default };