UNPKG

@selfcommunity/react-ui

Version:

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

86 lines (85 loc) 8.74 kB
import { __rest } from "tslib"; import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; import { useState } from 'react'; import { Avatar, Box, Stack, Typography, styled } from '@mui/material'; import { Link, SCRoutes, useSCRouting } from '@selfcommunity/react-core'; import { SCNotificationTypologyType } from '@selfcommunity/types'; import { FormattedMessage } from 'react-intl'; import DateTimeAgo from '../../../shared/DateTimeAgo'; import classNames from 'classnames'; import { SCCourseTemplateType, SCNotificationObjectTemplateType } from '../../../types'; import NotificationItem from '../../../shared/NotificationItem'; import { LoadingButton } from '@mui/lab'; import UserDeletedSnackBar from '../../../shared/UserDeletedSnackBar'; import UserAvatar from '../../../shared/UserAvatar'; import { PREFIX } from '../constants'; import { default as CourseItem } from '../../Course'; import HiddenPlaceholder from '../../../shared/HiddenPlaceholder'; import { formatLessonUrl, getRouteName, getRouteParams } from './utils'; const classes = { root: `${PREFIX}-course-root`, avatar: `${PREFIX}-avatar`, actions: `${PREFIX}-actions`, seeButton: `${PREFIX}see-button`, activeAt: `${PREFIX}-active-at`, snippetTime: `${PREFIX}-snippet-time`, username: `${PREFIX}-username` }; const Root = styled(NotificationItem, { name: PREFIX, slot: 'CourseRoot' })(() => ({})); /** * This component render the content of the notification of type course * @constructor * @param props */ export default function CourseNotification(props) { // PROPS const { notificationObject, 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); // CONST const isSnippetTemplate = template === SCNotificationObjectTemplateType.SNIPPET; const isToastTemplate = template === SCNotificationObjectTemplateType.TOAST; if (!notificationObject.course) { return _jsx(HiddenPlaceholder, {}); } // RENDER if (isSnippetTemplate || isToastTemplate) { return (_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(Box, { 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 })), ' ', _jsx(FormattedMessage, { id: `ui.notification.course.${notificationObject.type}`, defaultMessage: `ui.notification.course.${notificationObject.type}`, values: { // eslint-disable-next-line @typescript-eslint/ban-ts-ignore // @ts-ignore name: notificationObject.type === SCNotificationTypologyType.USER_COMMENTED_A_COURSE_LESSON ? notificationObject.comment.lesson_name : notificationObject.course.name, link: (...chunks) => (_jsx(Link, Object.assign({ to: scRoutingContext.url(notificationObject.type === SCNotificationTypologyType.USER_COMMENTED_A_COURSE_LESSON ? SCRoutes.COURSE_LESSON_ROUTE_NAME : SCRoutes.COURSE_ROUTE_NAME, notificationObject.type === SCNotificationTypologyType.USER_COMMENTED_A_COURSE_LESSON ? formatLessonUrl(notificationObject) : notificationObject.course) }, { children: chunks }))) } })] }), footer: isToastTemplate ? (_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" }, { children: _jsx(Link, Object.assign({ to: scRoutingContext.url(getRouteName(notificationObject), getRouteParams(notificationObject)) }, { children: _jsx(FormattedMessage, { id: "ui.notification.course.button.see", defaultMessage: "ui.notification.course.button.see" }) })) }))] }))) : (_jsx(DateTimeAgo, { date: notificationObject.active_at, className: classes.snippetTime })) }, rest))); } 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, { className: classes.avatar, alt: notificationObject.user.username, variant: "circular", src: notificationObject.user.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 })), ' ', _jsx(FormattedMessage, { id: `ui.notification.course.${notificationObject.type}`, defaultMessage: `ui.notification.course.${notificationObject.type}`, values: { // eslint-disable-next-line @typescript-eslint/ban-ts-ignore // @ts-ignore name: notificationObject.type === SCNotificationTypologyType.USER_COMMENTED_A_COURSE_LESSON ? notificationObject.comment.lesson_name : notificationObject.course.name, link: (...chunks) => (_jsx(Link, Object.assign({ to: scRoutingContext.url(notificationObject.type === SCNotificationTypologyType.USER_COMMENTED_A_COURSE_LESSON ? SCRoutes.COURSE_LESSON_ROUTE_NAME : SCRoutes.COURSE_ROUTE_NAME, notificationObject.type === SCNotificationTypologyType.USER_COMMENTED_A_COURSE_LESSON ? formatLessonUrl(notificationObject) : notificationObject.course) }, { children: chunks }))) } }), notificationObject.type === SCNotificationTypologyType.USER_COMMENTED_A_COURSE_LESSON ? (_jsx(Typography, { variant: "body2", dangerouslySetInnerHTML: { __html: notificationObject.comment.html } })) : (_jsx(CourseItem, { course: notificationObject.course, actions: _jsx(_Fragment, {}), template: SCCourseTemplateType.SNIPPET, elevation: 0 }))] }), actions: _jsxs(Stack, Object.assign({ direction: "row", justifyContent: "space-between", alignItems: "center", spacing: 2 }, { children: [_jsx(DateTimeAgo, { date: notificationObject.active_at, className: classes.activeAt }), _jsx(LoadingButton, Object.assign({ color: 'primary', variant: "outlined", size: "small", classes: { root: classes.seeButton }, component: Link, to: scRoutingContext.url(getRouteName(notificationObject), getRouteParams(notificationObject)) }, { children: _jsx(FormattedMessage, { id: "ui.notification.course.button.see", defaultMessage: "ui.notification.course.button.see" }) }))] })) }, rest)), openAlert && _jsx(UserDeletedSnackBar, { open: openAlert, handleClose: () => setOpenAlert(false) })] })); }