DREW | ae420b2 | 2025-06-02 14:07:20 +0800 | [diff] [blame^] | 1 | import React, { useEffect, useState } from 'react';
|
| 2 | import { useNavigate, useLocation, useParams } from 'react-router-dom';
|
| 3 | import { getAnnouncementDetail } from '../api/announcement';
|
| 4 | import { message } from 'antd';
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 5 | import './AnnouncementDetail.css';
|
| 6 |
|
| 7 | const AnnouncementDetail = () => {
|
| 8 | const navigate = useNavigate();
|
DREW | ae420b2 | 2025-06-02 14:07:20 +0800 | [diff] [blame^] | 9 | const { id } = useParams();
|
| 10 | const [announcement, setAnnouncement] = useState(null);
|
| 11 | const [loading, setLoading] = useState(true);
|
| 12 | const [error, setError] = useState('');
|
| 13 | const location = useLocation();
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 14 | const { state } = useLocation();
|
DREW | ae420b2 | 2025-06-02 14:07:20 +0800 | [diff] [blame^] | 15 |
|
| 16 |
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 17 | const handleBack = () => {
|
| 18 | const fromTab = location.state?.fromTab; // 从跳转时传递的 state 中获取
|
| 19 | if (fromTab) {
|
| 20 | navigate(`/dashboard/${fromTab}`); // 明确返回对应标签页
|
| 21 | } else {
|
| 22 | navigate(-1); // 保底策略
|
| 23 | }
|
| 24 | }
|
DREW | ae420b2 | 2025-06-02 14:07:20 +0800 | [diff] [blame^] | 25 | useEffect(() => {
|
| 26 | const fetchAnnouncement = async () => {
|
| 27 | try {
|
| 28 | setLoading(true);
|
| 29 | const response = await getAnnouncementDetail(id);
|
| 30 | if (response.data.code === 200) {
|
| 31 | setAnnouncement(response.data.data.announcement);
|
| 32 | } else {
|
| 33 | setError(response.data.message || '获取公告详情失败');
|
| 34 | }
|
| 35 | } catch (err) {
|
| 36 | setError('获取公告详情失败');
|
| 37 | message.error('获取公告详情失败');
|
| 38 | } finally {
|
| 39 | setLoading(false);
|
| 40 | }
|
| 41 | };
|
| 42 |
|
| 43 | fetchAnnouncement();
|
| 44 | }, [id]);
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 45 |
|
| 46 |
|
| 47 |
|
DREW | ae420b2 | 2025-06-02 14:07:20 +0800 | [diff] [blame^] | 48 | if (error || !announcement) {
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 49 | return (
|
| 50 | <div className="announcement-container">
|
DREW | ae420b2 | 2025-06-02 14:07:20 +0800 | [diff] [blame^] | 51 | <button className="back-button" onClick={handleBack}>
|
| 52 | ← 返回
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 53 | </button>
|
DREW | ae420b2 | 2025-06-02 14:07:20 +0800 | [diff] [blame^] | 54 | <div className="error-message">{error || '公告不存在'}</div>
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 55 | </div>
|
| 56 | );
|
| 57 | }
|
| 58 |
|
| 59 | return (
|
| 60 | <div className="announcement-container">
|
| 61 | <button className="back-button" onClick={handleBack}>
|
| 62 | ← 返回
|
| 63 | </button>
|
| 64 |
|
| 65 | <div className="announcement-detail">
|
| 66 | <div className="announcement-header">
|
| 67 | <h1>{announcement.title}</h1>
|
| 68 | <div className="announcement-meta">
|
DREW | ae420b2 | 2025-06-02 14:07:20 +0800 | [diff] [blame^] | 69 | <span>发布日期:{announcement.createTime}</span>
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 70 | </div>
|
| 71 | </div>
|
| 72 |
|
| 73 | <div className="announcement-content">
|
| 74 | {announcement.content.split('\n').map((paragraph, i) => (
|
| 75 | <p key={i}>{paragraph}</p>
|
| 76 | ))}
|
| 77 | </div>
|
| 78 | </div>
|
| 79 | </div>
|
| 80 | );
|
| 81 | };
|
| 82 |
|
| 83 | export default AnnouncementDetail;
|
| 84 |
|
| 85 |
|
| 86 | |