UNPKG

@selfcommunity/react-templates

Version:

React Templates Components to integrate a Community created with SelfCommunity.

164 lines (157 loc) • 7.87 kB
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime"; import { useEffect, useMemo, useRef, useState } from 'react'; import { styled, Box } from '@mui/material'; import { ContributionUtils, EventInfoWidget, EventLocationWidget, EventMediaWidget, EventMembersWidget, Feed, FeedObject, FeedObjectSkeleton, InlineComposerWidget, RelatedEventsWidget, SCFeedObjectTemplateType } from '@selfcommunity/react-ui'; import { Endpoints } from '@selfcommunity/api-services'; import { Link, SCRoutes, useSCFetchEvent, useSCRouting, useSCUser } from '@selfcommunity/react-core'; import { SCCustomAdvPosition, SCEventPrivacyType, SCEventSubscriptionStatusType, SCFeedTypologyType } from '@selfcommunity/types'; import { useThemeProps } from '@mui/system'; import classNames from 'classnames'; import { FormattedMessage } from 'react-intl'; import { useSnackbar } from 'notistack'; import { PREFIX } from './constants'; import EventFeedSkeleton from './Skeleton'; const classes = { root: `${PREFIX}-root` }; const Root = styled(Feed, { name: PREFIX, slot: 'Root' })(() => ({})); // Widgets for feed const WIDGETS = [ { type: 'widget', component: EventLocationWidget, componentProps: {}, column: 'right', position: 0 }, { type: 'widget', component: EventMembersWidget, componentProps: {}, column: 'right', position: 1 }, { type: 'widget', component: EventMediaWidget, componentProps: {}, column: 'right', position: 2 }, { type: 'widget', component: RelatedEventsWidget, componentProps: {}, column: 'right', position: 3 } ]; /** * > API documentation for the Community-JS Group Feed Template. Learn about the available props and the CSS API. * * * This component renders a specific event's feed. #### Import ```jsx import {EventFeed} from '@selfcommunity/react-templates'; ``` #### Component Name The name `SCEventFeedTemplate` can be used when providing style overrides in the theme. #### CSS |Rule Name|Global class|Description| |---|---|---| |root|.SCEventFeedTemplate-root|Styles applied to the root element.| * * @param inProps */ export default function EventFeed(inProps) { // PROPS const props = useThemeProps({ props: inProps, name: PREFIX }); const { id = 'event_feed', className, event, eventId, widgets = WIDGETS, FeedObjectProps = {}, FeedSidebarProps = null, FeedProps = {} } = props; // STATUS const [status, setStatus] = useState(undefined); // CONTEXT const scRoutingContext = useSCRouting(); const scUserContext = useSCUser(); const scEventsManager = scUserContext.managers.events; const { enqueueSnackbar } = useSnackbar(); const { scEvent } = useSCFetchEvent({ id: eventId, event }); // REF const feedRef = useRef(null); // CONST const authUserId = scUserContext.user ? scUserContext.user.id : null; useEffect(() => { var _a; /** * Call scEventsManager.subscriptionStatus inside an effect * to avoid warning rendering child during update parent state */ if (authUserId) { setStatus((_a = scEventsManager === null || scEventsManager === void 0 ? void 0 : scEventsManager.subscriptionStatus) === null || _a === void 0 ? void 0 : _a.call(scEventsManager, scEvent)); } }, [authUserId, scEventsManager === null || scEventsManager === void 0 ? void 0 : scEventsManager.subscriptionStatus, scEvent]); // HANDLERS const handleComposerSuccess = (feedObject) => { var _a; const messageId = feedObject.scheduled_at ? 'ui.composer.scheduled.success' : 'ui.composerIconButton.composer.success'; enqueueSnackbar(_jsx(FormattedMessage, { id: messageId, defaultMessage: messageId }), { action: () => (_jsx(Link, Object.assign({ to: scRoutingContext.url(SCRoutes[`${feedObject.type.toUpperCase()}_ROUTE_NAME`], ContributionUtils.getRouteData(feedObject)) }, { children: _jsx(FormattedMessage, { id: "ui.composerIconButton.composer.viewContribute", defaultMessage: "ui.composerIconButton.composer.viewContribute" }) }))), variant: 'success', autoHideDuration: 7000 }); if (((_a = feedObject.event) === null || _a === void 0 ? void 0 : _a.id) === scEvent.id) { // Hydrate feedUnit const feedUnit = { type: feedObject.type, [feedObject.type]: feedObject, seen: false, has_boost: false }; !feedObject.draft && feedRef && feedRef.current && feedRef.current.addFeedData(feedUnit, true); } }; // WIDGETS const _widgets = useMemo(() => widgets.map((w) => { if (scEvent) { return Object.assign(Object.assign({}, w), { componentProps: Object.assign(Object.assign({}, w.componentProps), { event: scEvent }) }); } return w; }), [widgets, scEvent]); if (scUserContext.user === undefined || (scUserContext.user && status === undefined) || !scEvent || (scUserContext.user && scEvent.privacy === SCEventPrivacyType.PUBLIC && !status) || (scEvent && ((eventId !== undefined && scEvent.id !== eventId) || (event && scEvent.id !== event.id)))) { return _jsx(EventFeedSkeleton, {}); } else if (scEvent.privacy === SCEventPrivacyType.PRIVATE && (status === SCEventSubscriptionStatusType.INVITED || (status !== SCEventSubscriptionStatusType.SUBSCRIBED && status !== SCEventSubscriptionStatusType.GOING && status !== SCEventSubscriptionStatusType.NOT_GOING))) { return (_jsx(Box, Object.assign({ mt: 2 }, { children: _jsx(EventInfoWidget, { className: classes.root, event: scEvent }) }))); } return (_jsx(Root, Object.assign({ className: classNames(classes.root, className), id: id, ref: feedRef, endpoint: Object.assign(Object.assign({}, Endpoints.GetEventFeed), { url: () => Endpoints.GetEventFeed.url({ id: scEvent.id }) }), widgets: _widgets, ItemComponent: FeedObject, itemPropsGenerator: (scUser, item) => ({ feedObject: item[item.type], feedObjectType: item.type, feedObjectActivities: item.activities ? item.activities : null, markRead: scUser ? !(item === null || item === void 0 ? void 0 : item.seen) : null }), itemIdGenerator: (item) => item[item.type].id, ItemProps: FeedObjectProps, ItemSkeleton: FeedObjectSkeleton, ItemSkeletonProps: { template: SCFeedObjectTemplateType.PREVIEW }, FeedSidebarProps: FeedSidebarProps, HeaderComponent: _jsxs(_Fragment, { children: [_jsx(EventInfoWidget, { className: classes.root, event: scEvent }), Boolean(scEvent && ((!scUserContext.user && scEvent.privacy === SCEventPrivacyType.PUBLIC) || (scUserContext.user && (status === SCEventSubscriptionStatusType.SUBSCRIBED || status === SCEventSubscriptionStatusType.GOING || status === SCEventSubscriptionStatusType.NOT_GOING)))) && (_jsx(InlineComposerWidget, { onSuccess: handleComposerSuccess, defaultValue: { event: scEvent }, label: _jsx(FormattedMessage, { id: "templates.eventFeed.composer.label", defaultMessage: "templates.eventFeed.composer.label" }), feedType: SCFeedTypologyType.EVENT }))] }), CustomAdvProps: { position: SCCustomAdvPosition.POSITION_FEED, groupsId: [scEvent.id] }, enabledCustomAdvPositions: [ SCCustomAdvPosition.POSITION_FEED_SIDEBAR, SCCustomAdvPosition.POSITION_FEED, SCCustomAdvPosition.POSITION_BELOW_TOPBAR ] }, FeedProps))); }