@selfcommunity/react-ui
Version:
React UI Components to integrate a Community created with SelfCommunity Platform.
76 lines (71 loc) • 3.32 kB
JavaScript
import { __rest } from "tslib";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import React, { useContext, useMemo } from 'react';
import { useThemeProps } from '@mui/system';
import { styled } from '@mui/material/styles';
import { Button, Icon } from '@mui/material';
import { FormattedMessage } from 'react-intl';
import { SCPreferences, SCUserContext, useSCPreferences } from '@selfcommunity/react-core';
import classNames from 'classnames';
import GroupForm from '../GroupForm';
import HiddenPlaceholder from '../../shared/HiddenPlaceholder';
const PREFIX = 'SCCreateGroupButton';
const classes = {
root: `${PREFIX}-root`
};
const Root = styled(Button, {
name: PREFIX,
slot: 'Root',
overridesResolver: (props, styles) => styles.root
})(({ theme }) => ({}));
/**
*> API documentation for the Community-JS Create Group Button component. Learn about the available props and the CSS API.
*
#### Import
```jsx
import {CreateGroupButton} from '@selfcommunity/react-ui';
```
#### Component Name
The name `SCCreateGroupButton` can be used when providing style overrides in the theme.
#### CSS
|Rule Name|Global class|Description|
|---|---|---|
|root|.SCCreateGroupButton-root|Styles applied to the root element.|
* @param inProps
*/
export default function CreateGroupButton(inProps) {
var _a;
//PROPS
const props = useThemeProps({
props: inProps,
name: PREFIX
});
const { className, GroupFormProps = {}, children } = props, rest = __rest(props, ["className", "GroupFormProps", "children"]);
// CONTEXT
const scUserContext = useContext(SCUserContext);
// STATE
const [open, setOpen] = React.useState(false);
// CONST
const authUserId = scUserContext.user ? scUserContext.user.id : null;
const preferences = useSCPreferences();
const onlyStaffEnabled = useMemo(() => preferences.preferences[SCPreferences.CONFIGURATIONS_GROUPS_ONLY_STAFF_ENABLED].value, [preferences.preferences]);
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
const canCreateGroup = useMemo(() => { var _a, _b; return (_b = (_a = scUserContext === null || scUserContext === void 0 ? void 0 : scUserContext.user) === null || _a === void 0 ? void 0 : _a.permission) === null || _b === void 0 ? void 0 : _b.create_group; }, [(_a = scUserContext === null || scUserContext === void 0 ? void 0 : scUserContext.user) === null || _a === void 0 ? void 0 : _a.permission]);
/**
* Handle click on button
*/
const handleClick = () => {
setOpen((o) => !o);
};
/**
* If there's no authUserId, component is hidden.
*/
if ((!canCreateGroup && onlyStaffEnabled) || !authUserId) {
return _jsx(HiddenPlaceholder, {});
}
/**
* Renders root object
*/
return (_jsxs(React.Fragment, { children: [_jsx(Root, Object.assign({ className: classNames(classes.root, className), onClick: handleClick, variant: "contained", startIcon: _jsx(Icon, { children: "add" }) }, rest, { children: children !== null && children !== void 0 ? children : _jsx(FormattedMessage, { id: "ui.createGroupButton", defaultMessage: "ui.createGroupButton" }) })), open && _jsx(GroupForm, Object.assign({}, GroupFormProps, { open: true, onClose: handleClick }))] }));
}