@selfcommunity/react-ui
Version:
React UI Components to integrate a Community created with SelfCommunity Platform.
126 lines (118 loc) • 8.57 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 = require("react");
const styles_1 = require("@mui/material/styles");
const lab_1 = require("@mui/lab");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const system_1 = require("@mui/system");
const Icon_1 = tslib_1.__importDefault(require("@mui/material/Icon"));
const material_1 = require("@mui/material");
const react_intl_1 = require("react-intl");
const react_core_1 = require("@selfcommunity/react-core");
const BaseDialog_1 = tslib_1.__importDefault(require("../../shared/BaseDialog"));
const CentralProgress_1 = tslib_1.__importDefault(require("../../shared/CentralProgress"));
const InfiniteScroll_1 = tslib_1.__importDefault(require("../../shared/InfiniteScroll"));
const User_1 = tslib_1.__importDefault(require("../User"));
const PREFIX = 'SCVoteAudienceButton';
const classes = {
root: `${PREFIX}-root`,
reactionList: `${PREFIX}-reaction-list`,
dialogRoot: `${PREFIX}-dialog-root`,
dialogTabs: `${PREFIX}-dialog-tabs`,
dialogVoteBadge: `${PREFIX}-dialog-vote-badge`
};
const Root = (0, styles_1.styled)(lab_1.LoadingButton, {
name: PREFIX,
slot: 'Root',
overridesResolver: (props, styles) => [styles.root, styles.voted]
})(({ theme }) => ({}));
const DialogRoot = (0, styles_1.styled)(BaseDialog_1.default, {
name: PREFIX,
slot: 'Root',
overridesResolver: (props, styles) => styles.dialogRoot
})(({ theme }) => ({}));
/**
* > API documentation for the Community-JS Vote Audience Button component. Learn about the available props and the CSS API.
#### Import
```jsx
import {VoteAudienceButton} from '@selfcommunity/react-ui';
```
#### Component Name
The name `SCVoteAudienceButton` can be used when providing style overrides in the theme.
#### CSS
|Rule Name|Global class|Description|
|---|---|---|
|root|.SCVoteAudienceButton-root|Styles applied to the root element.|
|reactionList|.SCVoteAudienceButton-reactions|Styles applied to the reactionList list element.|
|dialogRoot|.SCVoteAudienceButton-dialog-root|Styles applied to the dialog root element.|
|dialogTabs|.SCVoteAudienceButton-dialog-tabs|Styles applied to the tabs element.|
|dialogVoteBadge|.SCVoteAudienceButton-dialog-vote-badge|Styles applied to the vote badge element.|
* @param inProps
*/
function VoteAudienceButton(inProps) {
// PROPS
const props = (0, system_1.useThemeProps)({
props: inProps,
name: PREFIX
});
const { className, contributionId, contributionType, contribution = null } = props, rest = tslib_1.__rest(props, ["className", "contributionId", "contributionType", "contribution"]);
// STATE
const [open, setOpen] = (0, react_1.useState)(false);
const [tab, setTab] = (0, react_1.useState)(0);
// CONTEXT
const scUserContext = (0, react_core_1.useSCUser)();
// HANDLERS
const handleOpen = (event) => {
setOpen(true);
};
const handleClose = (event) => {
setOpen(false);
};
const handleChangeTab = (event, newValue) => {
setTab(newValue);
};
// HOOKS
const { isLoading, contributionVoted, contributionVoteCount, contributionReactionsCount, reactions, error, handleFetchVoteList, voteList, isLoadingVoteList, voteListHasNext } = (0, react_core_1.useSCFetchVote)({
id: contributionId,
contributionType,
contribution
});
// EFFECTS
(0, react_1.useEffect)(() => {
if (!isLoadingVoteList && open) {
handleFetchVoteList({ reset: true });
}
}, [open]);
(0, react_1.useEffect)(() => {
if (isLoadingVoteList || !open) {
return;
}
if (tab === 0) {
handleFetchVoteList({ reset: true });
return;
}
const { reaction } = contributionReactionsCount[tab - 1];
handleFetchVoteList({ reaction, reset: true });
}, [tab]);
// RENDER
const audienceIcon = (0, react_1.useMemo)(() => {
if (reactions.default && !isLoading && !error) {
return ((0, jsx_runtime_1.jsx)(material_1.Box, Object.assign({ className: classes.reactionList }, { children: contributionReactionsCount &&
contributionReactionsCount.slice(0, 3).map((count, i) => ((0, jsx_runtime_1.jsx)(Icon_1.default, { children: (0, jsx_runtime_1.jsx)("img", { alt: count.reaction.label, src: count.reaction.image, width: "100%", height: "100%" }) }, count.reaction.id))) })));
}
return scUserContext.user && contributionVoted ? (0, jsx_runtime_1.jsx)(Icon_1.default, { children: "thumb_up" }) : (0, jsx_runtime_1.jsx)(Icon_1.default, { children: "thumb_up_off_alt" });
}, [reactions.default, isLoading, error, contributionReactionsCount, scUserContext.user, contributionVoted]);
const dialogTitle = (0, react_1.useMemo)(() => {
if (reactions.default && !isLoading) {
return ((0, jsx_runtime_1.jsxs)(material_1.Tabs, Object.assign({ className: classes.dialogTabs, value: tab, onChange: handleChangeTab, "aria-label": "reactions" }, { children: [(0, jsx_runtime_1.jsx)(material_1.Tab, { label: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { defaultMessage: "ui.voteAudienceButton.dialog.tab.all", id: "ui.voteAudienceButton.dialog.tab.all" }) }), contributionReactionsCount &&
contributionReactionsCount.map((count) => ((0, jsx_runtime_1.jsx)(material_1.Tab, { label: (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(Icon_1.default, { children: (0, jsx_runtime_1.jsx)("img", { alt: count.reaction.label, src: count.reaction.image, width: "100%", height: "100%" }) }), count.count] }) }, count.reaction.id)))] })));
}
else {
return (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { defaultMessage: "ui.voteAudienceButton.dialog.title", id: "ui.voteAudienceButton.dialog.title" });
}
}, [tab, reactions, contributionReactionsCount]);
return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)(Root, Object.assign({ onClick: handleOpen, disabled: isLoading || Boolean(error) || contributionVoteCount === 0, loading: isLoading, className: (0, classnames_1.default)(classes.root, className) }, rest, { children: [audienceIcon, "\u00A0", scUserContext.user && contributionVoted ? (contributionVoteCount === 1 ? ((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.voteAudienceButton.votedOnlyByMe", defaultMessage: "ui.voteAudienceButton.votedOnlyByMe" })) : ((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.voteAudienceButton.votedByMe", defaultMessage: "ui.voteAudienceButton.votedByMe", values: { total: contributionVoteCount - 1 } }))) : ((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.voteAudienceButton.votes", defaultMessage: "ui.voteAudienceButton.votes", values: { total: contributionVoteCount } }))] })), open && ((0, jsx_runtime_1.jsx)(DialogRoot, Object.assign({ title: dialogTitle, onClose: handleClose, open: open, DialogContentProps: {} }, { children: isLoadingVoteList && !voteList ? ((0, jsx_runtime_1.jsx)(CentralProgress_1.default, { size: 50 })) : ((0, jsx_runtime_1.jsx)(InfiniteScroll_1.default, Object.assign({ dataLength: voteList.length, next: () => handleFetchVoteList({}), hasMoreNext: voteListHasNext, loaderNext: (0, jsx_runtime_1.jsx)(CentralProgress_1.default, { size: 30 }), height: 400, endMessage: (0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ variant: "body2", align: "center", fontWeight: "bold" }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.voteAudienceButton.dialog.end", defaultMessage: "ui.voteAudienceButton.dialog.end" }) })) }, { children: (0, jsx_runtime_1.jsx)(material_1.List, { children: voteList.map((vote) => ((0, jsx_runtime_1.jsx)(material_1.ListItem, { children: (0, jsx_runtime_1.jsx)(User_1.default, { elevation: 0, user: vote.user, badgeContent: (0, jsx_runtime_1.jsx)(material_1.Avatar, Object.assign({ className: classes.dialogVoteBadge }, { children: vote.reaction ? ((0, jsx_runtime_1.jsx)(Icon_1.default, { children: (0, jsx_runtime_1.jsx)("img", { alt: vote.reaction.label, src: vote.reaction.image, width: "100%", height: "100%" }) })) : ((0, jsx_runtime_1.jsx)(Icon_1.default, { children: "thumb_up" })) })), onClick: handleClose }) }, vote.user.id))) }) }))) })))] }));
}
exports.default = VoteAudienceButton;