UNPKG

@selfcommunity/react-ui

Version:

React UI Components to integrate a Community created with SelfCommunity Platform.

103 lines (102 loc) 5.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = tslib_1.__importStar(require("react")); const react_intl_1 = require("react-intl"); const LoadingButton_1 = tslib_1.__importDefault(require("@mui/lab/LoadingButton")); const styles_1 = require("@mui/material/styles"); const material_1 = require("@mui/material"); const Errors_1 = require("../../../../constants/Errors"); const classnames_1 = tslib_1.__importDefault(require("classnames")); const notistack_1 = require("notistack"); const Icon_1 = tslib_1.__importDefault(require("@mui/material/Icon")); const types_1 = require("@selfcommunity/types"); const api_services_1 = require("@selfcommunity/api-services"); const utils_1 = require("@selfcommunity/utils"); const react_core_1 = require("@selfcommunity/react-core"); const errors_1 = require("../../../../utils/errors"); const constants_1 = require("../../constants"); const classes = { root: `${constants_1.PREFIX}-action-follow-root`, button: `${constants_1.PREFIX}-action-follow-button`, iconized: `${constants_1.PREFIX}-action-follow-iconized`, followed: `${constants_1.PREFIX}-action-follow-followed` }; const Root = (0, styles_1.styled)(material_1.Box, { name: constants_1.PREFIX, slot: 'ActionFollowRoot' })(() => ({})); function Follow(props) { // PROPS const { className = null, feedObjectId = null, feedObject = null, feedObjectType = types_1.SCContributionType.POST, handleFollow, iconized = true } = props, rest = tslib_1.__rest(props, ["className", "feedObjectId", "feedObject", "feedObjectType", "handleFollow", "iconized"]); // STATE const { obj, setObj } = (0, react_core_1.useSCFetchFeedObject)({ id: feedObjectId, feedObject, feedObjectType }); const [isFollowing, setIsFollowing] = (0, react_1.useState)(false); // CONTEXT const scContext = (0, react_core_1.useSCContext)(); const scUserContext = (0, react_core_1.useSCUser)(); const { enqueueSnackbar } = (0, notistack_1.useSnackbar)(); /** * Perform follow/unfollow * Post, Discussion, Status */ const performFollow = (0, react_1.useMemo)(() => () => { return api_services_1.http .request({ url: api_services_1.Endpoints.FollowContribution.url({ type: obj.type, id: obj.id }), method: api_services_1.Endpoints.FollowContribution.method }) .then((res) => { if (res.status >= 300) { return Promise.reject(res); } return Promise.resolve(res.data); }); }, [obj]); /** * Handle follow object * Even if a user is blocked, can perform this action */ function follow() { if (!scUserContext.user) { scContext.settings.handleAnonymousAction(); } else { setIsFollowing(true); performFollow() .then((data) => { const isFollow = !obj.followed; setObj(Object.assign({}, obj, { followed: isFollow })); setIsFollowing(false); handleFollow && handleFollow(isFollow); }) .catch((error) => { utils_1.Logger.error(Errors_1.SCOPE_SC_UI, error); if (!(0, errors_1.catchUnauthorizedActionByBlockedUser)(error, scUserContext.managers.blockedUsers.isBlocked(obj.author), enqueueSnackbar)) { enqueueSnackbar((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.common.error.action", defaultMessage: "ui.common.error.action" }), { variant: 'error', autoHideDuration: 3000 }); } setIsFollowing(false); }); } } /** * Renders vote action if withAction==true * @return {JSX.Element} */ function renderFollowButton() { let btnLabel = (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.feedObject.follow", defaultMessage: "ui.feedObject.follow" }); if (obj.followed) { btnLabel = (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.feedObject.unfollow", defaultMessage: "ui.feedObject.unfollow" }); } return ((0, jsx_runtime_1.jsx)(react_1.default.Fragment, { children: scUserContext.user && obj.author.id !== scUserContext.user.id && !obj.deleted && ((0, jsx_runtime_1.jsx)(material_1.Tooltip, Object.assign({ title: btnLabel }, { children: (0, jsx_runtime_1.jsx)(LoadingButton_1.default, Object.assign({ className: (0, classnames_1.default)(classes.button, { [classes.iconized]: iconized, [classes.followed]: obj.followed }), loading: isFollowing, variant: iconized ? 'text' : 'contained', disabled: isFollowing, onClick: follow }, { children: iconized ? (0, jsx_runtime_1.jsx)(Icon_1.default, { children: obj.followed ? 'bookmark_added' : 'bookmark_border' }) : btnLabel })) }))) })); } /** * Renders share action */ return ((0, jsx_runtime_1.jsx)(Root, Object.assign({ className: (0, classnames_1.default)(classes.root, className) }, rest, { children: renderFollowButton() }))); } exports.default = Follow;