@selfcommunity/react-ui
Version:
React UI Components to integrate a Community created with SelfCommunity Platform.
59 lines (58 loc) • 5 kB
JavaScript
import { __rest } from "tslib";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { useState } from 'react';
import { styled } from '@mui/material/styles';
import { Avatar, Stack, Typography } from '@mui/material';
import { Link, SCRoutes, useSCRouting } from '@selfcommunity/react-core';
import { defineMessages, FormattedMessage, useIntl } from 'react-intl';
import DateTimeAgo from '../../../shared/DateTimeAgo';
import { getContribution, getContributionType, getContributionSnippet, getRouteData, getContributionRouteName } from '../../../utils/contribution';
import classNames from 'classnames';
import { SCNotificationObjectTemplateType } from '../../../types/notification';
import NotificationItem from '../../../shared/NotificationItem';
import UserDeletedSnackBar from '../../../shared/UserDeletedSnackBar';
import UserAvatar from '../../../shared/UserAvatar';
import { PREFIX } from '../constants';
const messages = defineMessages({
appreciated: {
id: 'ui.notification.voteUp.appreciated',
defaultMessage: 'ui.notification.voteUp.appreciated'
}
});
const classes = {
root: `${PREFIX}-vote-up-root`,
avatar: `${PREFIX}-avatar`,
username: `${PREFIX}-username`,
voteUpText: `${PREFIX}-vote-up-text`,
activeAt: `${PREFIX}-active-at`,
contributionText: `${PREFIX}-contribution-text`
};
const Root = styled(NotificationItem, {
name: PREFIX,
slot: 'VoteUpRoot'
})(() => ({}));
/**
* This component render the content of the notification of type vote up
* @constructor
* @param props
*/
export default function VoteUpNotification(props) {
// PROPS
const { notificationObject, id = `n_${props.notificationObject['sid']}`, className, template = SCNotificationObjectTemplateType.DETAIL, index, onVote, loadingVote } = props, rest = __rest(props, ["notificationObject", "id", "className", "template", "index", "onVote", "loadingVote"]);
// CONTEXT
const scRoutingContext = useSCRouting();
// CONST
const contribution = getContribution(notificationObject);
const contributionType = getContributionType(notificationObject);
// STATE
const [openAlert, setOpenAlert] = useState(false);
// INTL
const intl = useIntl();
/**
* Renders root object
*/
return (_jsxs(_Fragment, { children: [_jsx(Root, Object.assign({ id: id, className: classNames(classes.root, className, `${PREFIX}-${template}`), template: template, isNew: notificationObject.is_new, disableTypography: true, image: _jsx(Link, Object.assign({}, (!notificationObject.user.deleted && { to: scRoutingContext.url(SCRoutes.USER_PROFILE_ROUTE_NAME, notificationObject.user) }), { onClick: notificationObject.user.deleted ? () => setOpenAlert(true) : null }, { children: _jsx(UserAvatar, Object.assign({ hide: !notificationObject.user.community_badge, smaller: true }, { children: _jsx(Avatar, { alt: notificationObject.user.username, variant: "circular", src: notificationObject.user.avatar, classes: { root: classes.avatar } }) })) })), primary: _jsxs(_Fragment, { children: [_jsx(Link, Object.assign({}, (!notificationObject.user.deleted && { to: scRoutingContext.url(SCRoutes.USER_PROFILE_ROUTE_NAME, notificationObject.user) }), { onClick: notificationObject.user.deleted ? () => setOpenAlert(true) : null, className: classes.username }, { children: notificationObject.user.username })), ' ', intl.formatMessage(messages.appreciated, {
username: notificationObject.user.username,
b: (...chunks) => _jsx("strong", { children: chunks })
})] }), secondary: _jsxs(_Fragment, { children: [_jsx(Link, Object.assign({ to: scRoutingContext.url(SCRoutes[`${contributionType.toUpperCase()}_ROUTE_NAME`], getRouteData(notificationObject[contributionType])) }, { children: _jsx(Typography, Object.assign({ variant: "body2", className: classes.contributionText, component: 'div' }, { children: getContributionSnippet(notificationObject[contributionType]) })) })), (template === SCNotificationObjectTemplateType.DETAIL || template === SCNotificationObjectTemplateType.SNIPPET) && (_jsx(DateTimeAgo, { date: notificationObject.active_at, className: classes.activeAt }))] }), footer: template === SCNotificationObjectTemplateType.TOAST && (_jsxs(Stack, Object.assign({ direction: "row", justifyContent: "space-between", alignItems: "center", spacing: 2 }, { children: [_jsx(DateTimeAgo, { date: notificationObject.active_at }), _jsx(Typography, Object.assign({ color: "primary", component: 'div' }, { children: _jsx(Link, Object.assign({ to: scRoutingContext.url(getContributionRouteName(contribution), getRouteData(contribution)) }, { children: _jsx(FormattedMessage, { id: "ui.userToastNotifications.viewContribution", defaultMessage: 'ui.userToastNotifications.viewContribution' }) })) }))] }))) }, rest)), openAlert && _jsx(UserDeletedSnackBar, { open: openAlert, handleClose: () => setOpenAlert(false) })] }));
}