@selfcommunity/react-ui
Version:
React UI Components to integrate a Community created with SelfCommunity Platform.
206 lines (198 loc) • 13.2 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = tslib_1.__importStar(require("react"));
const styles_1 = require("@mui/material/styles");
const List_1 = tslib_1.__importDefault(require("@mui/material/List"));
const material_1 = require("@mui/material");
const Errors_1 = require("../../constants/Errors");
const react_core_1 = require("@selfcommunity/react-core");
const FeedObject_1 = tslib_1.__importStar(require("../FeedObject"));
const react_intl_1 = require("react-intl");
const feedObject_1 = require("../../types/feedObject");
const CustomAdv_1 = tslib_1.__importDefault(require("../CustomAdv"));
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const BaseDialog_1 = tslib_1.__importDefault(require("../../shared/BaseDialog"));
const InfiniteScroll_1 = tslib_1.__importDefault(require("../../shared/InfiniteScroll"));
const Widget_1 = tslib_1.__importDefault(require("../Widget"));
const system_1 = require("@mui/system");
const api_services_1 = require("@selfcommunity/api-services");
const utils_1 = require("@selfcommunity/utils");
const types_1 = require("@selfcommunity/types");
const HiddenPlaceholder_1 = tslib_1.__importDefault(require("../../shared/HiddenPlaceholder"));
const widget_1 = require("../../utils/widget");
const Skeleton_1 = tslib_1.__importDefault(require("./Skeleton"));
const constants_1 = require("./constants");
const classes = {
root: `${constants_1.PREFIX}-root`,
title: `${constants_1.PREFIX}-title`,
noResults: `${constants_1.PREFIX}-no-results`,
showMore: `${constants_1.PREFIX}-show-more`,
dialogRoot: `${constants_1.PREFIX}-dialog-root`,
endMessage: `${constants_1.PREFIX}-end-message`
};
const Root = (0, styles_1.styled)(Widget_1.default, {
name: constants_1.PREFIX,
slot: 'Root'
})(() => ({}));
const DialogRoot = (0, styles_1.styled)(BaseDialog_1.default, {
name: constants_1.PREFIX,
slot: 'DialogRoot'
})(() => ({}));
const PREFERENCES = [
react_core_1.SCPreferences.ADVERTISING_CUSTOM_ADV_ENABLED,
react_core_1.SCPreferences.ADVERTISING_CUSTOM_ADV_ONLY_FOR_ANONYMOUS_USERS_ENABLED,
react_core_1.SCPreferences.CONFIGURATIONS_CONTENT_AVAILABILITY
];
/**
* > API documentation for the Community-JS Related FeedObjects component. Learn about the available props and the CSS API.
*
*
* This component renders a list of related feed objects (posts, discussions or statuses).
* Take a look at our <strong>demo</strong> component [here](/docs/sdk/community-js/react-ui/Components/RelatedFeedObjects)
#### Import
```jsx
import {RelatedFeedObjectsWidget} from '@selfcommunity/react-ui';
```
#### Component Name
The name `SCRelatedFeedObjectsWidget` can be used when providing style overrides in the theme.
#### CSS
|Rule Name|Global class|Description|
|---|---|---|
|root|.SCRelatedFeedObjectsWidget-root|Styles applied to the root element.|
|title|.SCRelatedFeedObjectsWidget-title|Styles applied to the title element.|
|noResults|.SCRelatedFeedObjectsWidget-no-results|Styles applied to no results section.|
|showMore|.SCRelatedFeedObjectsWidget-show-more|Styles applied to show more button element.|
|dialogRoot|.SCRelatedFeedObjectsWidget-dialog-root|Styles applied to the dialog root element.|
|endMessage|.SCRelatedFeedObjectsWidget-end-message|Styles applied to the end message element.|
*
* @param inProps
*/
function RelatedFeedObjectWidget(inProps) {
// PROPS
const props = (0, system_1.useThemeProps)({
props: inProps,
name: constants_1.PREFIX
});
const { feedObject, feedObjectId, feedObjectType, template = feedObject_1.SCFeedObjectTemplateType.SNIPPET, className = null, autoHide = null, limit = 5, FeedObjectProps = {
template: feedObject_1.SCFeedObjectTemplateType.SNIPPET
}, cacheStrategy = utils_1.CacheStrategies.CACHE_FIRST, onHeightChange, onStateChange, DialogProps = {} } = props, rest = tslib_1.__rest(props, ["feedObject", "feedObjectId", "feedObjectType", "template", "className", "autoHide", "limit", "FeedObjectProps", "cacheStrategy", "onHeightChange", "onStateChange", "DialogProps"]);
// STATE
const [state, dispatch] = (0, react_1.useReducer)(widget_1.dataWidgetReducer, {
isLoadingNext: false,
next: null,
cacheKey: react_core_1.SCCache.getWidgetStateCacheKey(react_core_1.SCCache.RELATED_FEED_TOOLS_STATE_CACHE_PREFIX_KEY, feedObjectId),
cacheStrategy,
visibleItems: limit
}, widget_1.stateWidgetInitializer);
const [openDialog, setOpenDialog] = (0, react_1.useState)(false);
// CONTEXT
const scUserContext = (0, react_core_1.useSCUser)();
const scPreferences = (0, react_core_1.useSCPreferences)();
// HOOKS
const { obj } = (0, react_core_1.useSCFetchFeedObject)({ id: feedObjectId, feedObject, feedObjectType });
const theme = (0, material_1.useTheme)();
const isMobile = (0, material_1.useMediaQuery)(theme.breakpoints.down('md'));
const objId = obj ? obj.id : null;
// MEMO
const preferences = (0, react_1.useMemo)(() => {
const _preferences = {};
PREFERENCES.map((p) => (_preferences[p] = p in scPreferences.preferences ? scPreferences.preferences[p].value : null));
return _preferences;
}, [scPreferences.preferences]);
/**
* Initialize component
* Fetch data only if the component is not initialized and it is not loading data
*/
const _initComponent = (0, react_1.useMemo)(() => () => {
if (!state.initialized && !state.isLoadingNext) {
dispatch({ type: widget_1.actionWidgetTypes.LOADING_NEXT });
api_services_1.FeedObjectService.relatedFeedObjects(obj.type, obj.id, { limit })
.then((payload) => {
dispatch({ type: widget_1.actionWidgetTypes.LOAD_NEXT_SUCCESS, payload: Object.assign(Object.assign({}, payload), { initialized: true }) });
})
.catch((error) => {
dispatch({ type: widget_1.actionWidgetTypes.LOAD_NEXT_FAILURE, payload: { errorLoadNext: error } });
utils_1.Logger.error(Errors_1.SCOPE_SC_UI, error);
});
}
}, [objId, state.initialized, state.isLoadingNext, limit, dispatch]);
// EFFECTS
(0, react_1.useEffect)(() => {
var _a;
let _t;
if (scUserContext.user !== undefined &&
objId &&
preferences &&
(preferences[react_core_1.SCPreferences.CONFIGURATIONS_CONTENT_AVAILABILITY] ||
(!preferences[react_core_1.SCPreferences.CONFIGURATIONS_CONTENT_AVAILABILITY] && ((_a = scUserContext.user) === null || _a === void 0 ? void 0 : _a.id)))) {
_t = setTimeout(_initComponent);
return () => {
_t && clearTimeout(_t);
};
}
}, [objId, scUserContext.user, preferences]);
// Add 10 - limit items to initial dialog page
(0, react_1.useEffect)(() => {
if (openDialog && state.next && state.results.length === limit && state.initialized) {
dispatch({ type: widget_1.actionWidgetTypes.LOADING_NEXT });
api_services_1.FeedObjectService.relatedFeedObjects(obj.type, obj.id, { offset: limit, limit: 10 })
.then((payload) => {
dispatch({ type: widget_1.actionWidgetTypes.LOAD_NEXT_SUCCESS, payload: payload });
})
.catch((error) => {
dispatch({ type: widget_1.actionWidgetTypes.LOAD_NEXT_FAILURE, payload: { errorLoadNext: error } });
utils_1.Logger.error(Errors_1.SCOPE_SC_UI, error);
});
}
}, [openDialog, limit, state.next, state.initialized, state.results.length, obj]);
/**
* Virtual feed update
*/
(0, react_1.useEffect)(() => {
onHeightChange && onHeightChange();
}, [state.results]);
(0, react_1.useEffect)(() => {
if (!preferences[react_core_1.SCPreferences.CONFIGURATIONS_CONTENT_AVAILABILITY] && !scUserContext.user) {
return;
}
else if ((obj === null || obj === void 0 ? void 0 : obj.id) !== null && cacheStrategy === utils_1.CacheStrategies.NETWORK_ONLY) {
onStateChange && onStateChange({ cacheStrategy: utils_1.CacheStrategies.CACHE_FIRST });
}
}, [obj === null || obj === void 0 ? void 0 : obj.id, preferences, cacheStrategy, scUserContext.user]);
// HANDLERS
const handleNext = (0, react_1.useMemo)(() => () => {
dispatch({ type: widget_1.actionWidgetTypes.LOADING_NEXT });
api_services_1.http
.request({
url: state.next,
method: api_services_1.Endpoints.RelatedFeedObjects.method
})
.then((res) => {
dispatch({ type: widget_1.actionWidgetTypes.LOAD_NEXT_SUCCESS, payload: res.data });
});
}, [dispatch, state.next]);
const handleToggleDialogOpen = () => {
setOpenDialog((prev) => !prev);
};
// RENDER
if ((!preferences[react_core_1.SCPreferences.CONFIGURATIONS_CONTENT_AVAILABILITY] && !scUserContext.user) || (autoHide && !state.count && state.initialized)) {
return (0, jsx_runtime_1.jsx)(HiddenPlaceholder_1.default, {});
}
if (!state.initialized) {
return (0, jsx_runtime_1.jsx)(Skeleton_1.default, {});
}
const renderAdvertising = () => {
if (preferences[react_core_1.SCPreferences.ADVERTISING_CUSTOM_ADV_ENABLED] &&
((preferences[react_core_1.SCPreferences.ADVERTISING_CUSTOM_ADV_ONLY_FOR_ANONYMOUS_USERS_ENABLED] && scUserContext.user === null) ||
!preferences[react_core_1.SCPreferences.ADVERTISING_CUSTOM_ADV_ONLY_FOR_ANONYMOUS_USERS_ENABLED])) {
return ((0, jsx_runtime_1.jsx)(material_1.ListItem, { children: (0, jsx_runtime_1.jsx)(CustomAdv_1.default, Object.assign({ position: types_1.SCCustomAdvPosition.POSITION_RELATED_POSTS_COLUMN }, (obj.categories.length && { categoriesId: obj.categories.map((c) => c.id) }))) }));
}
return null;
};
const advPosition = Math.floor(Math.random() * (Math.min(state.count, 5) - 1 + 1) + 1);
const content = ((0, jsx_runtime_1.jsxs)(material_1.CardContent, { children: [(0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ className: classes.title, variant: "h5" }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.relatedFeedObjectsWidget.title", defaultMessage: "ui.relatedFeedObjectsWidget.title" }) })), !state.count ? ((0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ className: classes.noResults, variant: "body2" }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.relatedFeedObjectsWidget.noResults", defaultMessage: "ui.relatedFeedObjectsWidget.noResults" }) }))) : ((0, jsx_runtime_1.jsxs)(react_1.default.Fragment, { children: [(0, jsx_runtime_1.jsx)(List_1.default, { children: state.results.slice(0, state.visibleItems).map((obj, index) => ((0, jsx_runtime_1.jsxs)(react_1.default.Fragment, { children: [(0, jsx_runtime_1.jsx)(material_1.ListItem, { children: (0, jsx_runtime_1.jsx)(FeedObject_1.default, Object.assign({ elevation: 0, feedObject: obj, template: template }, FeedObjectProps)) }, obj.id), advPosition === index && renderAdvertising()] }, index))) }), state.count > state.visibleItems && ((0, jsx_runtime_1.jsx)(material_1.Button, Object.assign({ className: classes.showMore, onClick: handleToggleDialogOpen }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.categoryTrendingFeedWidget.button.showAll", defaultMessage: "ui.categoryTrendingFeedWidget.button.showAll" }) })))] })), openDialog && ((0, jsx_runtime_1.jsx)(DialogRoot, Object.assign({ className: classes.dialogRoot, title: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { defaultMessage: "ui.categoryTrendingFeedWidget.title", id: "ui.categoryTrendingFeedWidget.title", values: { total: state.count } }), onClose: handleToggleDialogOpen, open: openDialog }, DialogProps, { children: (0, jsx_runtime_1.jsx)(InfiniteScroll_1.default, Object.assign({ dataLength: state.results.length, next: handleNext, hasMoreNext: Boolean(state.next), loaderNext: (0, jsx_runtime_1.jsx)(FeedObject_1.FeedObjectSkeleton, Object.assign({ elevation: 0 }, FeedObjectProps)), height: isMobile ? '100%' : 400, endMessage: (0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ className: classes.endMessage }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.categoryTrendingFeedWidget.noMoreResults", defaultMessage: "ui.categoryTrendingFeedWidget.noMoreResults" }) })) }, { children: (0, jsx_runtime_1.jsx)(List_1.default, { children: state.results.map((obj) => ((0, jsx_runtime_1.jsx)(material_1.ListItem, { children: (0, jsx_runtime_1.jsx)(FeedObject_1.default, Object.assign({ elevation: 0, feedObject: obj }, FeedObjectProps)) }, obj.id))) }) })) })))] }));
return ((0, jsx_runtime_1.jsx)(Root, Object.assign({ className: (0, classnames_1.default)(classes.root, className) }, rest, { children: content })));
}
exports.default = RelatedFeedObjectWidget;