UNPKG

@selfcommunity/react-ui

Version:

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

78 lines (73 loc) 3.71 kB
import { __rest } from "tslib"; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { Button, Icon, styled } from '@mui/material'; 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 CourseFormDialog from '../CourseFormDialog'; const PREFIX = 'SCCreateCourseButton'; 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 {CreateCourseButton} from '@selfcommunity/react-ui'; ``` #### Component Name The name `SCCreateCourseButton` can be used when providing style overrides in the theme. #### CSS |Rule Name|Global class|Description| |---|---|---| |root|.SCCreateCourseButton-root|Styles applied to the root element.| * @param inProps */ export default function CreateCourseButton(inProps) { var _a; //PROPS const props = useThemeProps({ props: inProps, name: PREFIX }); const { className, CourseFormDialogComponentProps = { CourseFormComponentProps: { hidePaywalls: true } }, children } = props, rest = __rest(props, ["className", "CourseFormDialogComponentProps", "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 coursesEnabled = useMemo(() => preferences && features && features.includes(SCFeatureName.COURSE) && SCPreferences.CONFIGURATIONS_COURSES_ENABLED in preferences && preferences[SCPreferences.CONFIGURATIONS_COURSES_ENABLED].value, [preferences, features]); const onlyStaffEnabled = useMemo(() => { var _a; return (_a = preferences[SCPreferences.CONFIGURATIONS_COURSES_ONLY_STAFF_ENABLED]) === null || _a === void 0 ? void 0 : _a.value; }, [preferences]); const canCreateCourse = 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_course; }, [(_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 (!coursesEnabled || (!canCreateCourse && 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: "primary", startIcon: _jsx(Icon, Object.assign({ fontSize: "small" }, { children: "add" })) }, rest, { children: children !== null && children !== void 0 ? children : _jsx(FormattedMessage, { id: "ui.createCourseButton", defaultMessage: "ui.createCourseButton" }) })), open && _jsx(CourseFormDialog, Object.assign({}, CourseFormDialogComponentProps, { open: true, onClose: handleClick }))] })); }