UNPKG

@selfcommunity/react-ui

Version:

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

201 lines (192 loc) • 11.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = require("react"); const styles_1 = require("@mui/material/styles"); const material_1 = require("@mui/material"); const api_services_1 = require("@selfcommunity/api-services"); const utils_1 = require("@selfcommunity/utils"); const react_core_1 = require("@selfcommunity/react-core"); const Skeleton_1 = tslib_1.__importDefault(require("./Skeleton")); const react_intl_1 = require("react-intl"); const classnames_1 = tslib_1.__importDefault(require("classnames")); const Errors_1 = require("../../constants/Errors"); const system_1 = require("@mui/system"); const HiddenPlaceholder_1 = tslib_1.__importDefault(require("../../shared/HiddenPlaceholder")); const constants_1 = require("./constants"); const Group_1 = tslib_1.__importStar(require("../Group")); const Pagination_1 = require("../../constants/Pagination"); const InfiniteScroll_1 = tslib_1.__importDefault(require("../../shared/InfiniteScroll")); const pubsub_js_1 = tslib_1.__importDefault(require("pubsub-js")); const PubSub_1 = require("../../constants/PubSub"); const classes = { root: `${constants_1.PREFIX}-root`, filters: `${constants_1.PREFIX}-filter`, groups: `${constants_1.PREFIX}-groups`, item: `${constants_1.PREFIX}-item`, noResults: `${constants_1.PREFIX}-no-results`, showMore: `${constants_1.PREFIX}-show-more`, endMessage: `${constants_1.PREFIX}-end-message` }; const Root = (0, styles_1.styled)(material_1.Box, { name: constants_1.PREFIX, slot: 'Root' })(() => ({})); /** * > API documentation for the Community-JS Groups component. Learn about the available props and the CSS API. * * * This component renders the list of the follows of the given group. * Take a look at our <strong>demo</strong> component [here](/docs/sdk/community-js/react-ui/Components/Groups) #### Import ```jsx import {Groups} from '@selfcommunity/react-ui'; ``` #### Component Name The name `SCGroups` can be used when providing style overrides in the theme. #### CSS |Rule Name|Global class|Description| |---|---|---| |root|.SCGroups-root|Styles applied to the root element.| |title|.SCGroups-title|Styles applied to the title element.| |noResults|.SCGroups-no-results|Styles applied to no results section.| |showMore|.SCGroups-show-more|Styles applied to show more button element.| |dialogRoot|.SCGroups-dialog-root|Styles applied to the dialog root element.| |endMessage|.SCGroups-end-message|Styles applied to the end message element.| * @param inProps */ function Groups(inProps) { // PROPS const props = (0, system_1.useThemeProps)({ props: inProps, name: constants_1.PREFIX }); const { endpointQueryParams = { limit: 20, offset: Pagination_1.DEFAULT_PAGINATION_OFFSET }, className, GroupComponentProps = { variant: 'outlined', ButtonBaseProps: { disableRipple: true, component: material_1.Box } }, showFilters = true, filters, general = true } = props, rest = tslib_1.__rest(props, ["endpointQueryParams", "className", "GroupComponentProps", "showFilters", "filters", "general"]); // STATE const [groups, setGroups] = (0, react_1.useState)([]); const [loading, setLoading] = (0, react_1.useState)(true); const [next, setNext] = (0, react_1.useState)(null); const [search, setSearch] = (0, react_1.useState)(''); const theme = (0, material_1.useTheme)(); const isMobile = (0, material_1.useMediaQuery)(theme.breakpoints.down('md')); // CONTEXT const scUserContext = (0, react_core_1.useSCUser)(); const scPreferencesContext = (0, react_core_1.useSCPreferences)(); const onlyStaffEnabled = (0, react_1.useMemo)(() => scPreferencesContext.preferences[react_core_1.SCPreferences.CONFIGURATIONS_GROUPS_ONLY_STAFF_ENABLED].value, [scPreferencesContext.preferences]); // MEMO const contentAvailability = (0, react_1.useMemo)(() => react_core_1.SCPreferences.CONFIGURATIONS_CONTENT_AVAILABILITY in scPreferencesContext.preferences && scPreferencesContext.preferences[react_core_1.SCPreferences.CONFIGURATIONS_CONTENT_AVAILABILITY].value, [scPreferencesContext.preferences]); // CONST const authUserId = scUserContext.user ? scUserContext.user.id : null; // REFS const updatesSubscription = (0, react_1.useRef)(null); // HANDLERS const handleScrollUp = () => { window.scrollTo({ left: 0, top: 0, behavior: 'smooth' }); }; /** * Fetches groups list */ const fetchGroups = () => { let groupService; if (general) { groupService = api_services_1.GroupService.searchGroups(Object.assign(Object.assign({}, endpointQueryParams), (search !== '' && { search: search }))); } else { groupService = api_services_1.GroupService.getUserGroups(Object.assign(Object.assign({}, endpointQueryParams), (search !== '' && { search: search }))); } groupService .then((res) => { setGroups(res.results); setNext(res.next); setLoading(false); }) .catch((error) => { utils_1.Logger.error(Errors_1.SCOPE_SC_UI, error); }); }; /** * On mount, fetches groups list */ (0, react_1.useEffect)(() => { if (!contentAvailability && !authUserId) { return; } else { fetchGroups(); } }, [contentAvailability, authUserId, search]); /** * Subscriber for pubsub callback */ const onDeleteGroupHandler = (0, react_1.useCallback)((_msg, deleted) => { setGroups((prev) => { if (prev.some((e) => e.id === deleted)) { return prev.filter((e) => e.id !== deleted); } return prev; }); }, [groups]); /** * On mount, subscribe to receive event updates (only delete) */ (0, react_1.useEffect)(() => { if (groups) { updatesSubscription.current = pubsub_js_1.default.subscribe(`${PubSub_1.SCTopicType.GROUP}.${PubSub_1.SCGroupEventType.DELETE}`, onDeleteGroupHandler); } return () => { updatesSubscription.current && pubsub_js_1.default.unsubscribe(updatesSubscription.current); }; }, [groups]); const handleNext = (0, react_1.useMemo)(() => () => { if (!next) { return; } return api_services_1.http .request({ url: next, method: general ? api_services_1.Endpoints.SearchGroups.method : api_services_1.Endpoints.GetUserGroups.method }) .then((res) => { setGroups([...groups, ...res.data.results]); setNext(res.data.next); }) .catch((error) => console.log(error)) .then(() => setLoading(false)); }, [next]); /** * Get groups filtered */ const getFilteredGroups = () => { if (search) { return groups.filter((g) => g.name.toLowerCase().includes(search.toLowerCase())); } return groups; }; /** * Handle change filter name * @param event */ const handleOnChangeFilterName = (event) => { setSearch(event.target.value); }; /** * Renders groups list */ const filteredGroups = (0, utils_1.sortByAttr)(getFilteredGroups(), 'order'); const content = ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [showFilters && groups.length !== 0 && ((0, jsx_runtime_1.jsx)(material_1.Grid, Object.assign({ container: true, direction: "row", justifyContent: "center", alignItems: "center", className: classes.filters }, { children: filters ? (filters) : ((0, jsx_runtime_1.jsx)(material_1.Grid, Object.assign({ item: true, xs: 12, md: 6 }, { children: (0, jsx_runtime_1.jsx)(material_1.TextField, { fullWidth: true, value: search, label: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.groups.filterByName", defaultMessage: "ui.groups.filterByName" }), variant: "outlined", onChange: handleOnChangeFilterName, disabled: loading }) }))) }))), (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: !groups.length ? ((0, jsx_runtime_1.jsx)(material_1.Box, Object.assign({ className: classes.noResults }, { children: !onlyStaffEnabled ? ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ variant: "h4" }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.groups.noGroups.title", defaultMessage: "ui.groups.noGroups.title" }) })), (0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ variant: "body1" }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.groups.noGroups.subtitle", defaultMessage: "ui.groups.noGroups.subtitle" }) }))] })) : ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ variant: "h4" }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.groups.noGroups.title.onlyStaff", defaultMessage: "ui.groups.noGroups.title.onlyStaff" }) })), (0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ variant: "body1" }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.groups.noGroups.subtitle.onlyStaff", defaultMessage: "ui.groups.noGroups.subtitle.onlyStaff" }) }))] })) }))) : ((0, jsx_runtime_1.jsx)(InfiniteScroll_1.default, Object.assign({ dataLength: groups.length, next: handleNext, hasMoreNext: Boolean(next), loaderNext: isMobile ? (0, jsx_runtime_1.jsx)(Group_1.GroupSkeleton, {}) : (0, jsx_runtime_1.jsx)(Skeleton_1.default, { groupsNumber: 2 }), endMessage: (0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ component: "div", className: classes.endMessage }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.groups.endMessage", defaultMessage: "ui.groups.endMessage", values: { // eslint-disable-next-line @typescript-eslint/ban-ts-ignore // @ts-ignore button: (chunk) => ((0, jsx_runtime_1.jsx)(material_1.Button, Object.assign({ color: "secondary", variant: "text", onClick: handleScrollUp }, { children: chunk }))) } }) })) }, { children: (0, jsx_runtime_1.jsx)(material_1.Grid, Object.assign({ container: true, spacing: { xs: 2 }, className: classes.groups }, { children: filteredGroups.map((group) => ((0, jsx_runtime_1.jsx)(material_1.Grid, Object.assign({ item: true, xs: 12, sm: 8, md: 6, className: classes.item }, { children: (0, jsx_runtime_1.jsx)(Group_1.default, Object.assign({ group: group, groupId: group.id, actionRedirect: true }, GroupComponentProps)) }), group.id))) })) }))) })] })); // RENDER if (!contentAvailability && !scUserContext.user) { return (0, jsx_runtime_1.jsx)(HiddenPlaceholder_1.default, {}); } if (loading) { return (0, jsx_runtime_1.jsx)(Skeleton_1.default, {}); } return ((0, jsx_runtime_1.jsx)(Root, Object.assign({ className: (0, classnames_1.default)(classes.root, className) }, rest, { children: content }))); } exports.default = Groups;