UNPKG

@selfcommunity/react-ui

Version:

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

147 lines (143 loc) • 7.66 kB
import { __rest } from "tslib"; import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; import { Avatar, AvatarGroup, Button, Icon, List, ListItem, Typography, styled } from '@mui/material'; import { useThemeProps } from '@mui/system'; import { CourseService, Endpoints, http } from '@selfcommunity/api-services'; import { useSCFetchCourse } from '@selfcommunity/react-core'; import { SCCourseJoinStatusType } from '@selfcommunity/types'; import { Logger } from '@selfcommunity/utils'; import classNames from 'classnames'; import { useCallback, useEffect, useMemo, useState } from 'react'; import { FormattedMessage } from 'react-intl'; import { useDeepCompareEffectNoCheck } from 'use-deep-compare-effect'; import { SCOPE_SC_UI } from '../../constants/Errors'; import BaseDialog from '../../shared/BaseDialog'; import InfiniteScroll from '../../shared/InfiniteScroll'; import { numberFormatter } from '../../utils/buttonCounters'; import AvatarGroupSkeleton from '../Skeleton/AvatarGroupSkeleton'; import User, { UserSkeleton } from '../User'; import HiddenPlaceholder from '../../shared/HiddenPlaceholder'; const PREFIX = 'SCCourseParticipantsButton'; const classes = { root: `${PREFIX}-root`, dialogRoot: `${PREFIX}-dialog-root`, endMessage: `${PREFIX}-end-message`, infiniteScroll: `${PREFIX}-infinite-scroll`, participants: `${PREFIX}-participants` }; const Root = styled(Button, { name: PREFIX, slot: 'Root', overridesResolver: (_props, styles) => styles.root, shouldForwardProp: (prop) => prop !== 'enrolled' })(() => ({})); const DialogRoot = styled(BaseDialog, { name: PREFIX, slot: 'DialogRoot', overridesResolver: (_props, styles) => styles.dialogRoot })(() => ({})); /** *> API documentation for the Community-JS Course Participants Button component. Learn about the available props and the CSS API. * #### Import ```jsx import {CourseParticipantsButton} from '@selfcommunity/react-ui'; ``` #### Component Name The name `SCCourseParticipantsButton` can be used when providing style overrides in the theme. * #### CSS * |Rule Name|Global class|Description| |---|---|---| |root|.SCCourseParticipantsButton-root|Styles applied to the root element.| |dialogRoot|.SCCourseParticipantsButton-dialog-root|Styles applied to the dialog root element.| |endMessage|.SCCourseParticipantsButton-end-message|Styles applied to the end message element.| |infiniteScroll|.SCCourseParticipantsButton-infinite-scroll|Styles applied to the infinite scroll element.| |participants|.SCCourseParticipantsButton-participants|Styles applied to the participants section.| * @param inProps */ export default function CourseParticipantsButton(inProps) { // PROPS const props = useThemeProps({ props: inProps, name: PREFIX }); const { className, courseId, course, hideCaption = false, DialogProps = {} } = props, rest = __rest(props, ["className", "courseId", "course", "hideCaption", "DialogProps"]); // STATE const [loading, setLoading] = useState(true); const [count, setCount] = useState(0); const [next, setNext] = useState(null); const [offset, setOffset] = useState(null); const [enrolled, setEnrolled] = useState([]); const [open, setOpen] = useState(false); // HOOKS const { scCourse } = useSCFetchCourse({ id: courseId, course }); const participantsAvailable = useMemo(() => (scCourse === null || scCourse === void 0 ? void 0 : scCourse.join_status) === SCCourseJoinStatusType.CREATOR || (scCourse === null || scCourse === void 0 ? void 0 : scCourse.join_status) === SCCourseJoinStatusType.MANAGER, [scCourse]); useDeepCompareEffectNoCheck(() => { setEnrolled([]); setLoading(true); }, [scCourse]); // FETCH FIRST ENROLLED useDeepCompareEffectNoCheck(() => { if (!scCourse) { return; } if (!count && participantsAvailable) { CourseService.getCourseJoinedUsers(scCourse.id, { limit: 3 }).then((res) => { setEnrolled([...res.results]); setOffset(4); setCount(res.count); setLoading(false); }); } else { setOffset(0); } }, [scCourse, participantsAvailable, count, setEnrolled, setOffset, setLoading]); useEffect(() => { if (open && offset !== null) { setLoading(true); CourseService.getCourseJoinedUsers(scCourse.id, { offset, limit: 20 }).then((res) => { setEnrolled([...(offset === 0 ? [] : enrolled), ...res.results]); setNext(res.next); setLoading(false); setOffset(null); }); } }, [open, enrolled, offset, setLoading, setNext]); /** * Memoized fetchEnrolledUsers */ const fetchEnrolledUsers = useCallback(() => { if (!next) { return; } http .request({ url: next, method: Endpoints.GetUsersGoingToCourse.method }) .then((res) => { setEnrolled([...enrolled, ...res.data.results]); setNext(res.data.next); }) .catch((error) => Logger.error(SCOPE_SC_UI, error)) .then(() => setLoading(false)); }, [enrolled, next, setLoading]); const renderSurplus = useCallback(() => numberFormatter(count), [count]); /** * Opens participants dialog */ const handleToggleDialogOpen = useCallback(() => { setOpen((prev) => !prev); }, [setOpen]); /** * Rendering */ if (!participantsAvailable) { return _jsx(HiddenPlaceholder, {}); } return (_jsxs(_Fragment, { children: [_jsxs(Root, Object.assign({ className: classNames(classes.root, className), onClick: handleToggleDialogOpen, disabled: loading || !scCourse || count === 0, // @ts-expect-error this is needed to use enrolled into SCCourseParticipantsButton enrolled: enrolled }, rest, { children: [!hideCaption && (_jsxs(Typography, Object.assign({ className: classes.participants }, { children: [_jsx(Icon, { children: "people_alt" }), _jsx(FormattedMessage, { defaultMessage: "ui.courseParticipantsButton.participants", id: "ui.courseParticipantsButton.participants", values: { total: count } })] }))), !count && (loading || !scCourse) ? (_jsx(AvatarGroupSkeleton, Object.assign({}, rest, (!participantsAvailable && { skeletonsAnimation: false }), { count: 4 }))) : (_jsx(AvatarGroup, Object.assign({ total: count, renderSurplus: renderSurplus }, { children: enrolled.map((c) => (_jsx(Avatar, { alt: c.username, src: c.avatar }, c.id))) })))] })), open && (_jsx(DialogRoot, Object.assign({ className: classes.dialogRoot, title: _jsx(FormattedMessage, { defaultMessage: "ui.courseParticipantsButton.dialogTitle", id: "ui.courseParticipantsButton.dialogTitle", values: { total: count } }), onClose: handleToggleDialogOpen, open: true }, DialogProps, { children: _jsx(InfiniteScroll, Object.assign({ dataLength: count, next: fetchEnrolledUsers, hasMoreNext: next !== null || loading, loaderNext: _jsx(UserSkeleton, { elevation: 0 }), className: classes.infiniteScroll, endMessage: _jsx(Typography, Object.assign({ className: classes.endMessage }, { children: _jsx(FormattedMessage, { id: "ui.courseParticipantsButton.noOtherParticipants", defaultMessage: "ui.courseParticipantsButton.noOtherParticipants" }) })) }, { children: _jsx(List, { children: enrolled.map((e) => (_jsx(ListItem, { children: _jsx(User, { elevation: 0, user: e }) }, e.id))) }) })) })))] })); }