fix: refactor notification display logic and add format function
This commit is contained in:
@@ -3,6 +3,7 @@ import { Badge, Popover, Button, List, Typography, Empty } from 'antd';
|
|||||||
import { BellOutlined } from '@ant-design/icons';
|
import { BellOutlined } from '@ant-design/icons';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
import api from '../api';
|
import api from '../api';
|
||||||
|
import { formatNotificationText, notificationTypeLabels } from '../utils/notification-display';
|
||||||
|
|
||||||
interface NotificationItem {
|
interface NotificationItem {
|
||||||
id: number;
|
id: number;
|
||||||
@@ -14,18 +15,6 @@ interface NotificationItem {
|
|||||||
createdAt: string;
|
createdAt: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const typeLabels: Record<string, string> = {
|
|
||||||
bill_generated: '账单',
|
|
||||||
bill_paid: '账单',
|
|
||||||
check_in: '入住',
|
|
||||||
check_out: '退宿',
|
|
||||||
deposit_due: '押金',
|
|
||||||
deposit_refunded: '押金',
|
|
||||||
class_change: '班级',
|
|
||||||
schedule_conflict: '排课',
|
|
||||||
announcement: '公告',
|
|
||||||
};
|
|
||||||
|
|
||||||
function timeAgo(dateStr: string): string {
|
function timeAgo(dateStr: string): string {
|
||||||
const diff = Date.now() - new Date(dateStr).getTime();
|
const diff = Date.now() - new Date(dateStr).getTime();
|
||||||
const mins = Math.floor(diff / 60000);
|
const mins = Math.floor(diff / 60000);
|
||||||
@@ -163,7 +152,7 @@ const NotificationBell: React.FC = () => {
|
|||||||
strong={!item.isRead}
|
strong={!item.isRead}
|
||||||
style={{ fontSize: 14 }}
|
style={{ fontSize: 14 }}
|
||||||
>
|
>
|
||||||
[{typeLabels[item.type] || item.type}] {item.title}
|
[{notificationTypeLabels[item.type] || item.type}] {formatNotificationText(item.title)}
|
||||||
</Typography.Text>
|
</Typography.Text>
|
||||||
}
|
}
|
||||||
description={
|
description={
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import {
|
|||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
import api from '../../api';
|
import api from '../../api';
|
||||||
import { message } from '../../ui/app-message';
|
import { message } from '../../ui/app-message';
|
||||||
|
import { formatNotificationText } from '../../utils/notification-display';
|
||||||
|
|
||||||
const { Sider, Content } = Layout;
|
const { Sider, Content } = Layout;
|
||||||
|
|
||||||
@@ -169,7 +170,7 @@ const NotificationsPage: React.FC = () => {
|
|||||||
strong={!item.isRead}
|
strong={!item.isRead}
|
||||||
style={{ fontSize: 15 }}
|
style={{ fontSize: 15 }}
|
||||||
>
|
>
|
||||||
{item.title}
|
{formatNotificationText(item.title)}
|
||||||
</Typography.Text>
|
</Typography.Text>
|
||||||
<Typography.Text type="secondary" style={{ fontSize: 12 }}>
|
<Typography.Text type="secondary" style={{ fontSize: 12 }}>
|
||||||
{timeAgo(item.createdAt)}
|
{timeAgo(item.createdAt)}
|
||||||
@@ -183,7 +184,7 @@ const NotificationsPage: React.FC = () => {
|
|||||||
ellipsis={{ rows: 1 }}
|
ellipsis={{ rows: 1 }}
|
||||||
style={{ marginBottom: 0 }}
|
style={{ marginBottom: 0 }}
|
||||||
>
|
>
|
||||||
{item.content}
|
{formatNotificationText(item.content)}
|
||||||
</Typography.Paragraph>
|
</Typography.Paragraph>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
25
apps/admin/src/utils/notification-display.ts
Normal file
25
apps/admin/src/utils/notification-display.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
export const notificationTypeLabels: Record<string, string> = {
|
||||||
|
bill_generated: '账单',
|
||||||
|
bill_paid: '账单',
|
||||||
|
check_in: '入住',
|
||||||
|
check_out: '退宿',
|
||||||
|
deposit_due: '押金',
|
||||||
|
deposit_refunded: '押金',
|
||||||
|
class_change: '班级',
|
||||||
|
schedule_conflict: '排课',
|
||||||
|
announcement: '公告',
|
||||||
|
};
|
||||||
|
|
||||||
|
const teacherRoleLabels: Record<string, string> = {
|
||||||
|
subject_teacher: '任课老师',
|
||||||
|
head_teacher: '班主任',
|
||||||
|
life_teacher: '生活老师',
|
||||||
|
academic_teacher: '学服老师',
|
||||||
|
};
|
||||||
|
|
||||||
|
export function formatNotificationText(text: string): string {
|
||||||
|
return Object.entries(teacherRoleLabels).reduce(
|
||||||
|
(result, [roleType, label]) => result.replaceAll(roleType, label),
|
||||||
|
text,
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -29,6 +29,7 @@ import { extractRequestInfo } from '../common/request-utils';
|
|||||||
import { RequirePermission } from '../auth/decorators/permission.decorator';
|
import { RequirePermission } from '../auth/decorators/permission.decorator';
|
||||||
import { NotificationsService } from '../notifications/notifications.service';
|
import { NotificationsService } from '../notifications/notifications.service';
|
||||||
import { NotificationType } from '../entities/notification.entity';
|
import { NotificationType } from '../entities/notification.entity';
|
||||||
|
import { TeacherRoleType } from '../entities';
|
||||||
import * as ExcelJS from 'exceljs';
|
import * as ExcelJS from 'exceljs';
|
||||||
import { AuthorizationService, CaslAction, SubjectName, AuthenticatedUser } from '../authorization';
|
import { AuthorizationService, CaslAction, SubjectName, AuthenticatedUser } from '../authorization';
|
||||||
|
|
||||||
@@ -36,6 +37,13 @@ interface AuthenticatedRequest {
|
|||||||
user: AuthenticatedUser;
|
user: AuthenticatedUser;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const teacherRoleLabels: Record<string, string> = {
|
||||||
|
[TeacherRoleType.SUBJECT_TEACHER]: '任课老师',
|
||||||
|
[TeacherRoleType.HEAD_TEACHER]: '班主任',
|
||||||
|
[TeacherRoleType.LIFE_TEACHER]: '生活老师',
|
||||||
|
[TeacherRoleType.ACADEMIC_TEACHER]: '学服老师',
|
||||||
|
};
|
||||||
|
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Controller('classes')
|
@Controller('classes')
|
||||||
export class ClassesController {
|
export class ClassesController {
|
||||||
@@ -302,7 +310,7 @@ export class ClassesController {
|
|||||||
recipientIds: [dto.userId],
|
recipientIds: [dto.userId],
|
||||||
type: NotificationType.CLASS_CHANGE,
|
type: NotificationType.CLASS_CHANGE,
|
||||||
title: '班级分配',
|
title: '班级分配',
|
||||||
content: `您已被分配到班级担任${dto.roleType}角色`,
|
content: `您已被分配到班级担任${teacherRoleLabels[dto.roleType] ?? dto.roleType}角色`,
|
||||||
});
|
});
|
||||||
} catch {}
|
} catch {}
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
Reference in New Issue
Block a user