Skip to content

Commit 0ffba19

Browse files
committed
Fix: posts loading error
1 parent 7d20838 commit 0ffba19

1 file changed

Lines changed: 29 additions & 18 deletions

File tree

src/Components/Post/PostContent/PostContent.jsx

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,23 @@ import React, {useState} from "react";
1818
import {BoldTypography, useSmallPage} from "../../common";
1919
import {isAuthApplicant} from "../../../Data/ApplicantData";
2020
import "./PostContent.css"
21-
import {getAvatar, getMetaData} from "../../../Data/UserData";
21+
import {getMetaData, getAvatar} from "../../../Data/UserData";
2222
import Grid2 from "@mui/material/Unstable_Grid2";
2323

2424
export async function loader({params}) {
2525
const postId = params.postId;
2626
try {
2727
const postObj = await getPostObject(postId);
28-
const editable = await isAuthApplicant(postObj?.Author);
29-
const metaData = postObj?.Author ? await getMetaData(postObj?.Author.split("@")[0]) : {};
30-
const avatar = await getAvatar(metaData?.Avatar, postObj?.Author);
31-
return {postObj, editable, avatar};
28+
29+
const authorId = postObj?.author;
30+
const editable = authorId ? await isAuthApplicant(authorId) : false;
31+
32+
const metaData = authorId ? await getMetaData(authorId.split("@")[0]) : {};
33+
const avatar = await getAvatar(metaData?.Avatar, authorId);
34+
const latestYear = metaData?.latestYear;
35+
return {postObj, editable, avatar, latestYear, urlPostId: postId};
3236
} catch (e) {
37+
console.error("[PostContent Loader] Error:", e);
3338
throw e;
3439
}
3540
}
@@ -40,17 +45,23 @@ export async function action({request, params}) {
4045
const author = formData.get('Author');
4146
const ActionType = formData.get('ActionType');
4247
if (ActionType === 'Refresh') {
43-
return await getPostObject(postId, true);
48+
await getPostObject(postId, true);
49+
return {urlPostId: postId};
4450
} else if (ActionType === 'Delete') {
4551
await removePost(postId, author);
4652
return redirect("/posts");
4753
}
54+
return {urlPostId: postId};
4855
}
4956

5057
export default function PostContent() {
51-
const {postObj, editable, avatar} = useLoaderData();
58+
const {postObj, editable, avatar, latestYear, urlPostId} = useLoaderData();
5259
const smallPage = useSmallPage();
5360
const [open, setOpen] = useState(false);
61+
62+
if (!postObj) {
63+
return <Typography>帖子加载失败或不存在。</Typography>;
64+
}
5465
const handleClose = () => {
5566
setOpen(false);
5667
};
@@ -62,31 +73,31 @@ export default function PostContent() {
6273
<Box className="PostContentHeader" sx={{pb: "0.5rem"}}>
6374
<Box sx={{pb: "0.5rem", display: 'flex', gap: '1rem'}}>
6475
<Avatar src={avatar} sx={{height: (smallPage ? "5rem" : "4rem"), width: (smallPage ? "5rem" : "4rem"), cursor: 'pointer'}} component={Link}
65-
to={`/datapoints/applicant/${postObj.Author}`}/>
76+
to={`/datapoints/applicant/${postObj.author}${latestYear ? '@' + latestYear : ''}`}/>
6677
<Grid2 container>
6778
<Grid2 xs={12}>
68-
<BoldTypography variant="h6" component={Link} to={`/datapoints/applicant/${postObj.Author}`}
79+
<BoldTypography variant="h6" component={Link} to={`/datapoints/applicant/${postObj.author}${latestYear ? '@' + latestYear : ''}`}
6980
sx={{
7081
cursor: 'pointer',
7182
textDecoration: "none",
7283
}}
7384
>
74-
{postObj.Author}
85+
{postObj.author}
7586
</BoldTypography>
7687
</Grid2>
7788
<Grid2 component={Typography} xs={12} md={5}>
78-
创建于: {new Date(postObj.created).toISOString().split('T')[0]}
89+
创建于: {postObj.created_at}
7990
</Grid2>
8091
<Grid2 component={Typography} xs={12} md={7}>
81-
最后修改于: {new Date(postObj.modified).toISOString().split('T')[0]}
92+
最后修改于: {postObj.updated_at}
8293
</Grid2>
8394
</Grid2>
8495
</Box>
8596
<Box className='ReviseRefreshButtonGroup'>
8697
{editable ?
8798
<>
8899
<Tooltip title="编辑内容" arrow>
89-
<IconButton component={Link} to={`edit${window.location.search}`}>
100+
<IconButton component={Link} to={`/posts/${urlPostId}/edit${window.location.search}`}>
90101
<Edit/>
91102
</IconButton>
92103
</Tooltip>
@@ -97,12 +108,12 @@ export default function PostContent() {
97108
</Tooltip>
98109
<Dialog open={open} onClose={handleClose}>
99110
<DialogTitle>
100-
是否要删除《{postObj.Title}》这篇文章?
111+
是否要删除《{postObj.title}》这篇文章?
101112
</DialogTitle>
102113
<DialogActions>
103114
<Button onClick={handleClose}>取消</Button>
104115
<Form method="post">
105-
<Input type="hidden" name="Author" value={postObj.Author}/>
116+
<Input type="hidden" name="Author" value={postObj.author}/>
106117
<Button color='error' type="submit"
107118
name="ActionType" value="Delete"
108119
onClick={handleClose}
@@ -124,12 +135,12 @@ export default function PostContent() {
124135
</Box>
125136
<Paper sx={{display: 'flex', flexDirection: 'column', p: '1rem', height: '100%', overflowY: 'auto'}}>
126137
<Typography variant={'h4'} sx={{display: 'flex', position: 'relative', mb: '1rem'}}>
127-
{postObj.Title}
138+
{postObj.title}
128139
</Typography>
129140
<ReactMarkdown>
130-
{postObj.Content}
141+
{postObj.content}
131142
</ReactMarkdown>
132143
</Paper>
133144
</>
134145
)
135-
}
146+
}

0 commit comments

Comments
 (0)