@selfcommunity/react-ui
Version:
React UI Components to integrate a Community created with SelfCommunity Platform.
59 lines (58 loc) • 4.84 kB
JavaScript
import { __rest } from "tslib";
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useState } from 'react';
import { Avatar, Stack, Typography, styled } from '@mui/material';
import { defineMessages, FormattedMessage, useIntl } from 'react-intl';
import DateTimeAgo from '../../../shared/DateTimeAgo';
import { SCNotificationTypologyType } from '@selfcommunity/types';
import { Link, SCRoutes, useSCRouting } from '@selfcommunity/react-core';
import classNames from 'classnames';
import { SCNotificationObjectTemplateType } from '../../../types';
import NotificationItem from '../../../shared/NotificationItem';
import UserDeletedSnackBar from '../../../shared/UserDeletedSnackBar';
import UserAvatar from '../../../shared/UserAvatar';
import { PREFIX } from '../constants';
const messages = defineMessages({
requestConnection: {
id: 'ui.notification.userConnection.requestConnection',
defaultMessage: 'ui.notification.userConnection.requestConnection'
},
acceptConnection: {
id: 'ui.notification.userConnection.acceptConnection',
defaultMessage: 'ui.notification.userConnection.acceptConnection'
}
});
const classes = {
root: `${PREFIX}-user-connection-root`,
avatar: `${PREFIX}-avatar`,
username: `${PREFIX}-username`,
connectionText: `${PREFIX}-connection-text`,
activeAt: `${PREFIX}-active-at`
};
const Root = styled(NotificationItem, {
name: PREFIX,
slot: 'UserConnectionRoot'
})(() => ({}));
/**
* This component render the content of the notification of connection accept/request
* @constructor
* @param props
*/
export default function UserConnectionNotification(props) {
// PROPS
const { notificationObject = null, id = `n_${props.notificationObject['sid']}`, className, template = SCNotificationObjectTemplateType.DETAIL } = props, rest = __rest(props, ["notificationObject", "id", "className", "template"]);
// CONTEXT
const scRoutingContext = useSCRouting();
// STATE
const [openAlert, setOpenAlert] = useState(false);
// INTL
const intl = useIntl();
// CONST
const userConnection = notificationObject.type === SCNotificationTypologyType.CONNECTION_REQUEST ? notificationObject.request_user : notificationObject.accept_user;
/**
* 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({}, (!userConnection.deleted && { to: scRoutingContext.url(SCRoutes.USER_PROFILE_ROUTE_NAME, userConnection) }), { onClick: userConnection.deleted ? () => setOpenAlert(true) : null }, { children: _jsx(UserAvatar, Object.assign({ hide: !userConnection.community_badge, smaller: true }, { children: _jsx(Avatar, { alt: userConnection.username, variant: "circular", src: userConnection.avatar, classes: { root: classes.avatar } }) })) })), primary: _jsxs(Typography, Object.assign({ component: "div", color: "inherit", className: classes.connectionText }, { children: [_jsx(Link, Object.assign({}, (!userConnection.deleted && { to: scRoutingContext.url(SCRoutes.USER_PROFILE_ROUTE_NAME, userConnection) }), { onClick: userConnection.deleted ? () => setOpenAlert(true) : null, className: classes.username }, { children: userConnection.username })), ' ', notificationObject.type === SCNotificationTypologyType.CONNECTION_REQUEST
? intl.formatMessage(messages.requestConnection, { b: (...chunks) => _jsx("strong", { children: chunks }) })
: intl.formatMessage(messages.acceptConnection, { b: (...chunks) => _jsx("strong", { children: chunks }) })] })), secondary: (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({}, (!userConnection.deleted && { to: scRoutingContext.url(SCRoutes.USER_PROFILE_ROUTE_NAME, userConnection) }), { onClick: userConnection.deleted ? () => setOpenAlert(true) : null }, { children: _jsx(FormattedMessage, { id: "ui.userToastNotifications.goToProfile", defaultMessage: 'ui.userToastNotifications.goToProfile' }) })) }))] }))) }, rest)), openAlert && _jsx(UserDeletedSnackBar, { open: openAlert, handleClose: () => setOpenAlert(false) })] }));
}