@selfcommunity/react-templates
Version:
React Templates Components to integrate a Community created with SelfCommunity.
123 lines (116 loc) • 4.24 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import { useContext } from 'react';
import { styled } from '@mui/material';
import { BroadcastMessages, CategoriesSuggestionWidget, Feed, SCFeedObjectTemplateType, FeedUpdatesWidget, LoyaltyProgramWidget, Notification, NotificationSkeleton, UserSuggestionWidget, PlatformWidget, getUnseenNotificationCounter } from '@selfcommunity/react-ui';
import { Endpoints } from '@selfcommunity/api-services';
import { SCUserContext } from '@selfcommunity/react-core';
import { SCNotificationTopicType } from '@selfcommunity/types';
import { useThemeProps } from '@mui/system';
import classNames from 'classnames';
import { PREFIX } from './constants';
const classes = {
root: `${PREFIX}-root`
};
const Root = styled(Feed, {
name: PREFIX,
slot: 'Root'
})(() => ({}));
// Widgets for feed
const WIDGETS = [
{
type: 'widget',
component: FeedUpdatesWidget,
componentProps: { subscriptionChannel: SCNotificationTopicType.INTERACTION, publicationChannel: 'notifications_feed' },
column: 'left',
position: 0,
publishEvents: true
},
{
type: 'widget',
component: BroadcastMessages,
componentProps: { subscriptionChannel: 'notification_feed' },
column: 'left',
position: 0
},
{
type: 'widget',
component: PlatformWidget,
componentProps: {},
column: 'right',
position: 0
},
{
type: 'widget',
component: LoyaltyProgramWidget,
componentProps: {},
column: 'right',
position: 1
},
{
type: 'widget',
component: CategoriesSuggestionWidget,
componentProps: {},
column: 'right',
position: 2
},
{
type: 'widget',
component: UserSuggestionWidget,
componentProps: {},
column: 'right',
position: 3
}
];
/**
* > API documentation for the Community-JS Notification Feed Template. Learn about the available props and the CSS API.
*
*
* This component renders the template for the notification feed.
* Take a look at our <strong>demo</strong> component [here](/docs/sdk/community-js/react-templates/Components/NotificationFeed)
#### Import
```jsx
import {NotificationFeed} from '@selfcommunity/react-templates';
```
#### Component Name
The name `SCNotificationFeedTemplate` can be used when providing style overrides in the theme.
#### CSS
|Rule Name|Global class|Description|
|---|---|---|
|root|.SCNotificationFeedTemplate-root|Styles applied to the root element.|
*
* @param inProps
*/
export default function NotificationFeed(inProps) {
// PROPS
const props = useThemeProps({
props: inProps,
name: PREFIX
});
const { id = 'notification_feed', className, widgets = WIDGETS, NotificationProps = {}, FeedSidebarProps = null, FeedProps = {} } = props;
//CONTEXT
const scUserContext = useContext(SCUserContext);
// Ckeck user is authenticated
if (!scUserContext.user) {
return null;
}
/**
* Update user unseen notification counter
* @param page
* @param offset
* @param total
* @param data
*/
const handleFetchData = (page, offset, total, data) => {
let _unviewed = getUnseenNotificationCounter(data);
_unviewed > 0 && scUserContext.setUnseenInteractionsCounter(scUserContext.user.unseen_interactions_counter - _unviewed);
if (!_unviewed) {
// Sync counters
void scUserContext.refreshCounters();
}
};
return (_jsx(Root, Object.assign({ id: id, className: classNames(classes.root, className), endpoint: Endpoints.UserNotificationList, widgets: widgets, ItemComponent: Notification, itemPropsGenerator: (scUser, item) => ({
notificationObject: item
}), itemIdGenerator: (item) => item.sid, ItemProps: NotificationProps, ItemSkeleton: NotificationSkeleton, ItemSkeletonProps: {
template: SCFeedObjectTemplateType.PREVIEW
}, FeedSidebarProps: FeedSidebarProps, requireAuthentication: true, disablePaginationLinks: true, onNextData: handleFetchData, onPreviousData: handleFetchData }, FeedProps)));
}