UNPKG

@selfcommunity/react-ui

Version:

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

116 lines (108 loc) 5.15 kB
import { __rest } from "tslib"; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { useContext, useEffect, useMemo, useState } from 'react'; import { styled } from '@mui/material/styles'; import CardContent from '@mui/material/CardContent'; import { Button, CardActions, Chip, Typography } from '@mui/material'; import { http, Endpoints } from '@selfcommunity/api-services'; import { SCPreferences, SCUserContext, useSCPreferences } from '@selfcommunity/react-core'; import { FormattedMessage } from 'react-intl'; import { useSCRouting, Link, SCRoutes } from '@selfcommunity/react-core'; import classNames from 'classnames'; import Widget from '../Widget'; import { useThemeProps } from '@mui/system'; import HiddenPlaceholder from '../../shared/HiddenPlaceholder'; import Skeleton from './Skeleton'; import { SCOPE_SC_UI } from '../../constants/Errors'; import { Logger } from '@selfcommunity/utils'; import { PREFIX } from './constants'; const classes = { root: `${PREFIX}-root`, title: `${PREFIX}-title`, actions: `${PREFIX}-actions`, points: `${PREFIX}-points`, discoverMore: `${PREFIX}-discover-more` }; const Root = styled(Widget, { name: PREFIX, slot: 'Root' })(() => ({})); /** * > API documentation for the Community-JS Loyalty Program Widget component. Learn about the available props and the CSS API. * * * This component renders a widget containing the points earn by the user with the loyalty program. * Take a look at our <strong>demo</strong> component [here](/docs/sdk/community-js/react-ui/Components/LoyaltyProgram) #### Import ```jsx import {LoyaltyProgramWidget} from '@selfcommunity/react-ui'; ``` #### Component Name The name `SCLoyaltyProgramWidget` can be used when providing style overrides in the theme. #### CSS |Rule Name|Global class|Description| |---|---|---| |root|.SCLoyaltyProgramWidget-root|Styles applied to the root element.| |title.SCLoyaltyProgramWidget-title|Styles applied to the title element.| |actions|.SCLoyaltyProgramWidget-actions|Styles applied to the actions section.| |points|.SCLoyaltyProgramWidget-points|Styles applied to the points section.| |discoverMore|.SCLoyaltyProgramWidget-discover-more|Styles applied to discover more button element.| * * @param inProps */ export default function LoyaltyProgramWidget(inProps) { const props = useThemeProps({ props: inProps, name: PREFIX }); // PROPS const { className, userId = null } = props, rest = __rest(props, ["className", "userId"]); // CONTEXT const scUserContext = useContext(SCUserContext); const scPreferencesContext = useSCPreferences(); const scRoutingContext = useSCRouting(); // STATE const [points, setPoints] = useState(null); const [loading, setLoading] = useState(true); const authUserId = scUserContext.user ? scUserContext.user.id : null; //MEMO const loyaltyEnabled = useMemo(() => SCPreferences.ADDONS_LOYALTY_POINTS_COLLECTION in scPreferencesContext.preferences && scPreferencesContext.preferences[SCPreferences.ADDONS_LOYALTY_POINTS_COLLECTION].value, [scPreferencesContext.preferences]); /** * Fetches user loyalty points */ const fetchLP = () => { http .request({ url: Endpoints.GetUserLoyaltyPoints.url({ id: scUserContext.user['id'] }), method: Endpoints.GetUserLoyaltyPoints.method }) .then((res) => { setPoints(res.data.points); setLoading(false); }) .catch((error) => { setLoading(false); Logger.error(SCOPE_SC_UI, error); console.log(error); }); }; /** * On mount, fetches user loyalty points */ useEffect(() => { if (loyaltyEnabled && authUserId) { fetchLP(); } }, [authUserId, loyaltyEnabled]); /** * Rendering */ if (!loyaltyEnabled || !scUserContext.user || (userId && userId !== scUserContext.user.id)) { return _jsx(HiddenPlaceholder, {}); } if (loading) { return _jsx(Skeleton, {}); } return (_jsxs(Root, Object.assign({ className: classNames(classes.root, className) }, rest, { children: [_jsx(CardContent, { children: _jsx(Typography, Object.assign({ className: classes.title }, { children: _jsx(FormattedMessage, { id: "ui.loyaltyProgramWidget.title", defaultMessage: "ui.loyaltyProgramWidget.title" }) })) }), _jsxs(CardActions, Object.assign({ className: classes.actions }, { children: [_jsxs(Typography, Object.assign({ className: classes.points }, { children: [_jsx(Chip, { size: 'medium', component: "span", label: points }), _jsx(FormattedMessage, { id: "ui.loyaltyProgramWidget.points", defaultMessage: "ui.loyaltyProgramWidget.points" })] })), _jsx(Button, Object.assign({ size: "small", variant: "outlined", className: classes.discoverMore, component: Link, to: scRoutingContext.url(SCRoutes.LOYALTY_ROUTE_NAME, {}) }, { children: _jsx(FormattedMessage, { id: "ui.loyaltyProgramWidget.discover", defaultMessage: "ui.loyaltyProgramWidget.discover" }) }))] }))] }))); }