@selfcommunity/react-ui
Version:
React UI Components to integrate a Community created with SelfCommunity Platform.
116 lines (107 loc) • 4.91 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const styles_1 = require("@mui/material/styles");
const Errors_1 = require("../../constants/Errors");
const lab_1 = require("@mui/lab");
const react_intl_1 = require("react-intl");
const notistack_1 = require("notistack");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const utils_1 = require("@selfcommunity/utils");
const system_1 = require("@mui/system");
const errors_1 = require("../../utils/errors");
const react_core_1 = require("@selfcommunity/react-core");
const PREFIX = 'SCFollowUserButton';
const classes = {
root: `${PREFIX}-root`
};
const FollowButton = (0, styles_1.styled)(lab_1.LoadingButton, {
name: PREFIX,
slot: 'Root',
overridesResolver: (props, styles) => styles.root
})(() => ({}));
/**
* > API documentation for the Community-JS Follow User Button component. Learn about the available props and the CSS API.
#### Import
```jsx
import {FollowUserButton} from '@selfcommunity/react-ui';
```
#### Component Name
The name `SCFollowUserButton` can be used when providing style overrides in the theme.
#### CSS
|Rule Name|Global class|Description|
|---|---|---|
|root|.SCFollowUserButton-root|Styles applied to the root element.|
* @param inProps
*/
function FollowUserButton(inProps) {
// PROPS
const props = (0, system_1.useThemeProps)({
props: inProps,
name: PREFIX
});
const { className, userId, user, onFollow } = props, rest = tslib_1.__rest(props, ["className", "userId", "user", "onFollow"]);
// CONTEXT
const scContext = (0, react_core_1.useSCContext)();
const scUserContext = (0, react_1.useContext)(react_core_1.SCUserContext);
const scFollowedManager = scUserContext.managers.followed;
const { enqueueSnackbar } = (0, notistack_1.useSnackbar)();
// STATE
const { scUser } = (0, react_core_1.useSCFetchUser)({ id: userId, user });
const [followed, setFollowed] = (0, react_1.useState)(null);
const [disabled, setDisabled] = (0, react_1.useState)(false);
// CONST
const authUserId = scUserContext.user ? scUserContext.user.id : null;
(0, react_1.useEffect)(() => {
/**
* Call scFollowedManager.isFollowed inside an effect
* to avoid warning rendering child during update parent state
*/
if (scUserContext.user && scUserContext.user.id !== scUser.id) {
setFollowed(scFollowedManager.isFollowed(scUser));
}
}, [authUserId, scFollowedManager.isFollowed]);
const followUser = () => {
if (!followed && react_core_1.UserUtils.isBlocked(scUserContext.user)) {
enqueueSnackbar((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.common.userBlocked", defaultMessage: "ui.common.userBlocked" }), {
variant: 'warning',
autoHideDuration: 3000
});
}
else {
scFollowedManager
.follow(scUser)
.then(() => {
onFollow && onFollow(scUser, !followed);
})
.catch((e) => {
utils_1.Logger.error(Errors_1.SCOPE_SC_UI, e);
if ((0, errors_1.catchUnauthorizedActionByBlockedUser)(e, scUserContext.managers.blockedUsers.isBlocked(scUser), enqueueSnackbar)) {
setDisabled(true);
}
else {
enqueueSnackbar((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.common.actionToUserDeleted", defaultMessage: "ui.common.actionToUserDeleted" }), {
variant: 'warning',
autoHideDuration: 3000
});
}
});
}
};
const handleFollowAction = () => {
if (!scUserContext.user) {
scContext.settings.handleAnonymousAction();
}
else {
followUser();
}
};
//same user
if (scUserContext.user && scUserContext.user.id === scUser.id) {
return null;
}
return ((0, jsx_runtime_1.jsx)(FollowButton, Object.assign({ size: "small", variant: "outlined", onClick: handleFollowAction, loading: scUserContext.user ? followed === null || scFollowedManager.isLoading(scUser) : null, disabled: disabled, className: (0, classnames_1.default)(classes.root, className) }, rest, { children: scUserContext.user && followed ? ((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { defaultMessage: "ui.followUserButton.unfollow", id: "ui.followUserButton.unfollow" })) : ((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { defaultMessage: "ui.followUserButton.follow", id: "ui.followUserButton.follow" })) })));
}
exports.default = FollowUserButton;