@selfcommunity/react-ui
Version:
React UI Components to integrate a Community created with SelfCommunity Platform.
138 lines (134 loc) • 7.43 kB
JavaScript
import { __rest } from "tslib";
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { Avatar, AvatarGroup, Button, List, ListItem, Typography } from '@mui/material';
import { styled } from '@mui/material/styles';
import { useThemeProps } from '@mui/system';
import { Endpoints, EventService, http } from '@selfcommunity/api-services';
import { useSCFetchEvent } from '@selfcommunity/react-core';
import { SCEventPrivacyType, SCEventSubscriptionStatusType } 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';
const PREFIX = 'SCEventParticipantsButton';
const classes = {
root: `${PREFIX}-root`,
participants: `${PREFIX}-participants`,
dialogRoot: `${PREFIX}-dialog-root`,
infiniteScroll: `${PREFIX}-infinite-scroll`,
endMessage: `${PREFIX}-end-message`
};
const Root = styled(Button, {
name: PREFIX,
slot: 'Root',
overridesResolver: (_props, styles) => styles.root,
shouldForwardProp: (prop) => prop !== 'followers'
})(() => ({}));
const DialogRoot = styled(BaseDialog, {
name: PREFIX,
slot: 'DialogRoot',
overridesResolver: (_props, styles) => styles.dialogRoot
})(() => ({}));
/**
*> API documentation for the Community-JS Event Participants Button component. Learn about the available props and the CSS API.
*
#### Import
```jsx
import {EventParticipantsButton} from '@selfcommunity/react-ui';
```
#### Component Name
The name `SCEventParticipantsButton` can be used when providing style overrides in the theme.
* #### CSS
*
|Rule Name|Global class|Description|
|---|---|---|
|root|.SCEventParticipantsButton-root|Styles applied to the root element.|
|dialogRoot|.SCEventParticipantsButton-dialog-root|Styles applied to the root element.|
|endMessage|.SCEventParticipantsButton-end-message|Styles applied to the end message element.|
* @param inProps
*/
export default function EventParticipantsButton(inProps) {
// PROPS
const props = useThemeProps({
props: inProps,
name: PREFIX
});
const { className, eventId, event, hideCaption = false, DialogProps = {} } = props, rest = __rest(props, ["className", "eventId", "event", "hideCaption", "DialogProps"]);
// STATE
const [loading, setLoading] = useState(true);
const [next, setNext] = useState(null);
const [offset, setOffset] = useState(null);
const [followers, setFollowers] = useState([]);
const [openDialog, setOpenDialog] = useState(false);
// HOOKS
const { scEvent } = useSCFetchEvent({ id: eventId, event, autoSubscribe: false });
const participantsAvailable = useMemo(() => (scEvent === null || scEvent === void 0 ? void 0 : scEvent.privacy) === SCEventPrivacyType.PUBLIC ||
[SCEventSubscriptionStatusType.SUBSCRIBED, SCEventSubscriptionStatusType.GOING, SCEventSubscriptionStatusType.NOT_GOING].indexOf(scEvent === null || scEvent === void 0 ? void 0 : scEvent.subscription_status) > -1, [scEvent]);
useDeepCompareEffectNoCheck(() => {
setFollowers([]);
setLoading(true);
}, [scEvent]);
// FETCH FIRST FOLLOWERS
useDeepCompareEffectNoCheck(() => {
if (!scEvent || !participantsAvailable) {
return;
}
if (!followers.length && participantsAvailable) {
EventService.getUsersGoingToEvent(scEvent.id, { limit: 3 }).then((res) => {
setFollowers([...res.results]);
setOffset(4);
setLoading(false);
});
}
else {
setOffset(0);
}
}, [scEvent, participantsAvailable, followers]);
useEffect(() => {
if (open && offset !== null) {
setLoading(true);
EventService.getUsersGoingToEvent(scEvent.id, { offset, limit: 20 }).then((res) => {
setFollowers([...(offset === 0 ? [] : followers), ...res.results]);
setNext(res.next);
setLoading(false);
setOffset(null);
});
}
}, [open, followers, offset]);
/**
* Memoized fetchFollowers
*/
const fetchFollowers = useCallback(() => {
if (!next) {
return;
}
http
.request({
url: next,
method: Endpoints.GetUsersGoingToEvent.method
})
.then((res) => {
setFollowers([...followers, ...res.data.results]);
setNext(res.data.next);
})
.catch((error) => Logger.error(SCOPE_SC_UI, error))
.then(() => setLoading(false));
}, [followers, scEvent, next]);
const renderSurplus = useCallback(() => numberFormatter(followers.length), [followers]);
/**
* Opens dialog votes
*/
const handleToggleDialogOpen = useCallback(() => {
setOpenDialog((prev) => !prev);
}, [setOpenDialog]);
return (_jsxs(_Fragment, { children: [_jsxs(Root, Object.assign({ className: classNames(classes.root, className), onClick: handleToggleDialogOpen, disabled: loading || !scEvent || scEvent.goings_counter === 0,
// @ts-expect-error this is needed to use followers into SCEventParticipantsButton
followers: followers }, rest, { children: [!hideCaption && (_jsx(Typography, Object.assign({ className: classes.participants, variant: "caption" }, { children: _jsx(FormattedMessage, { defaultMessage: "ui.eventParticipantsButton.participants", id: "ui.eventParticipantsButton.participants" }) }))), !followers.length && (loading || !scEvent) ? (_jsx(AvatarGroupSkeleton, Object.assign({}, rest, (!participantsAvailable && { skeletonsAnimation: false })))) : (_jsx(AvatarGroup, Object.assign({ total: followers.length, renderSurplus: renderSurplus }, { children: followers.map((c) => (_jsx(Avatar, { alt: c.username, src: c.avatar }, c.id))) })))] })), openDialog && (_jsx(DialogRoot, Object.assign({ className: classes.dialogRoot, title: _jsx(FormattedMessage, { defaultMessage: "ui.eventParticipantsButton.dialogTitle", id: "ui.eventParticipantsButton.dialogTitle", values: { total: (scEvent === null || scEvent === void 0 ? void 0 : scEvent.goings_counter) || 0 } }), onClose: handleToggleDialogOpen, open: true }, DialogProps, { children: _jsx(InfiniteScroll, Object.assign({ dataLength: followers.length, next: fetchFollowers, 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.eventParticipantsButton.noOtherParticipants", defaultMessage: "ui.eventParticipantsButton.noOtherParticipants" }) })) }, { children: _jsx(List, { children: followers.map((follower) => (_jsx(ListItem, { children: _jsx(User, { elevation: 0, user: follower }) }, follower.id))) }) })) })))] }));
}