@selfcommunity/react-ui
Version:
React UI Components to integrate a Community created with SelfCommunity Platform.
81 lines (76 loc) • 3.82 kB
JavaScript
import { __rest } from "tslib";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Button, Icon } from '@mui/material';
import { styled } from '@mui/material/styles';
import { useThemeProps } from '@mui/system';
import { SCPreferences, SCUserContext, useSCPreferences } from '@selfcommunity/react-core';
import { SCFeatureName } from '@selfcommunity/types';
import classNames from 'classnames';
import React, { useContext, useMemo } from 'react';
import { FormattedMessage } from 'react-intl';
import EventFormDialog from '../EventFormDialog';
const PREFIX = 'SCCreateEventButton';
const classes = {
root: `${PREFIX}-root`
};
const Root = styled(Button, {
name: PREFIX,
slot: 'Root',
overridesResolver: (_props, styles) => styles.root
})(() => ({}));
/**
*> API documentation for the Community-JS Create Group Button component. Learn about the available props and the CSS API.
*
#### Import
```jsx
import {CreateEventButton} from '@selfcommunity/react-ui';
```
#### Component Name
The name `SCCreateEventButton` can be used when providing style overrides in the theme.
#### CSS
|Rule Name|Global class|Description|
|---|---|---|
|root|.SCCreateEventButton-root|Styles applied to the root element.|
* @param inProps
*/
export default function CreateEventButton(inProps) {
var _a, _b;
//PROPS
const props = useThemeProps({
props: inProps,
name: PREFIX
});
const { className, EventFormDialogComponentProps = {}, children } = props, rest = __rest(props, ["className", "EventFormDialogComponentProps", "children"]);
// CONTEXT
const scUserContext = useContext(SCUserContext);
// STATE
const [open, setOpen] = React.useState(false);
// CONST
const authUserId = scUserContext.user ? scUserContext.user.id : null;
const { preferences, features } = useSCPreferences();
const eventsEnabled = useMemo(() => preferences &&
features &&
features.includes(SCFeatureName.TAGGING) &&
SCPreferences.CONFIGURATIONS_EVENTS_ENABLED in preferences &&
preferences[SCPreferences.CONFIGURATIONS_EVENTS_ENABLED].value, [preferences, features]);
const onlyStaffEnabled = useMemo(() => preferences[SCPreferences.CONFIGURATIONS_EVENTS_ONLY_STAFF_ENABLED].value, [preferences]);
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
const canCreateEvent = 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_event; }, [(_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 (!eventsEnabled || (!canCreateEvent && onlyStaffEnabled) || !authUserId) {
return null;
}
/**
* Renders root object
*/
return (_jsxs(React.Fragment, { children: [_jsx(Root, Object.assign({ className: classNames(classes.root, className), onClick: handleClick, variant: "contained", color: "secondary", startIcon: !((_b = EventFormDialogComponentProps.EventFormComponentProps) === null || _b === void 0 ? void 0 : _b.event) && _jsx(Icon, Object.assign({ fontSize: "small" }, { children: "add" })) }, rest, { children: children !== null && children !== void 0 ? children : _jsx(FormattedMessage, { id: "ui.createEventButton", defaultMessage: "ui.createEventButton" }) })), open && _jsx(EventFormDialog, Object.assign({}, EventFormDialogComponentProps, { open: true, onClose: handleClick }))] }));
}