Update so as not to break existing links to comments

This commit is contained in:
SleeplessOne1917 2023-08-04 15:18:30 -04:00
parent 61acd39be1
commit e1cf2b8e02
4 changed files with 58 additions and 27 deletions

View file

@ -1194,7 +1194,7 @@ export class CommentNode extends Component<CommentNodeProps, CommentNodeState> {
<> <>
<Link <Link
className={classnames} className={classnames}
to={`/post/${cv.comment.post_id}/comment/${parentCommentId}`} to={`/comment/${parentCommentId}`}
title={title} title={title}
> >
<Icon icon="link" classes="icon-inline" /> <Icon icon="link" classes="icon-inline" />

View file

@ -334,7 +334,7 @@ function renderModlogType({ type_, view }: ModlogType) {
const mrc = view as ModRemoveCommentView; const mrc = view as ModRemoveCommentView;
const { const {
mod_remove_comment: { reason, removed }, mod_remove_comment: { reason, removed },
comment: { id, content, post_id }, comment: { id, content },
commenter, commenter,
} = mrc; } = mrc;
@ -342,7 +342,7 @@ function renderModlogType({ type_, view }: ModlogType) {
<> <>
<span>{removed ? "Removed " : "Restored "}</span> <span>{removed ? "Removed " : "Restored "}</span>
<span> <span>
Comment <Link to={`/post/${post_id}/comment/${id}`}>{content}</Link> Comment <Link to={`/comment/${id}`}>{content}</Link>
</span> </span>
<span> <span>
{" "} {" "}

View file

@ -198,22 +198,44 @@ export class Post extends Component<any, PostState> {
const auth = myAuth(); const auth = myAuth();
const { post_id, comment_id } = this.props.match.params as PostParams; const { post_id, comment_id } = this.props.match.params as PostParams;
const postForm: GetPost = {
auth,
};
const commentsForm: GetComments = {
max_depth: commentTreeMaxDepth,
sort: this.state.commentSort,
type_: "All",
saved_only: false,
auth,
};
if (post_id) {
postForm.id = getIdFromString(post_id);
commentsForm.post_id = getIdFromString(post_id);
} else {
const commentRes = await HttpService.client.getComment({
auth,
id: getIdFromString(comment_id)!,
});
if (commentRes.state === "success") {
const postId = commentRes.data.comment_view.comment.post_id;
postForm.id = postId;
postForm.comment_id = getIdFromString(comment_id);
commentsForm.post_id = postId;
commentsForm.parent_id = getIdFromString(comment_id);
}
}
const [postRes, commentsRes] = await Promise.all([
HttpService.client.getPost(postForm),
HttpService.client.getComments(commentsForm),
]);
this.setState({ this.setState({
postRes: await HttpService.client.getPost({ postRes,
id: getIdFromString(post_id), commentsRes,
comment_id: getIdFromString(comment_id),
auth,
}),
commentsRes: await HttpService.client.getComments({
post_id: getIdFromString(post_id),
parent_id: getIdFromString(comment_id),
max_depth: commentTreeMaxDepth,
sort: this.state.commentSort,
type_: "All",
saved_only: false,
auth,
}),
}); });
setupTippy(); setupTippy();
@ -234,13 +256,11 @@ export class Post extends Component<any, PostState> {
}: InitialFetchRequest): Promise<PostData> { }: InitialFetchRequest): Promise<PostData> {
const pathSplit = path.split("/"); const pathSplit = path.split("/");
const id = getIdFromString(pathSplit.at(2)); const pathType = pathSplit.at(1);
const comment_id = getIdFromString(pathSplit.at(4)); const id = getIdFromString(pathSplit.at(2))!;
const postForm: GetPost = { const postForm: GetPost = {
auth, auth,
id,
comment_id,
}; };
const commentsForm: GetComments = { const commentsForm: GetComments = {
@ -249,10 +269,23 @@ export class Post extends Component<any, PostState> {
type_: "All", type_: "All",
saved_only: false, saved_only: false,
auth, auth,
post_id: id,
parent_id: comment_id,
}; };
if (pathType === "post") {
postForm.id = id;
commentsForm.post_id = id;
} else {
const commentRes = await client.getComment({ id, auth });
if (commentRes.state === "success") {
const postId = commentRes.data.comment_view.comment.post_id;
postForm.id = postId;
postForm.comment_id = id;
commentsForm.post_id = postId;
commentsForm.parent_id = id;
}
}
return { return {
postRes: await client.getPost(postForm), postRes: await client.getPost(postForm),
commentsRes: await client.getComments(commentsForm), commentsRes: await client.getComments(commentsForm),
@ -704,9 +737,7 @@ export class Post extends Component<any, PostState> {
const comment = i.state.commentsRes.data.comments.at(0)?.comment; const comment = i.state.commentsRes.data.comments.at(0)?.comment;
const parentId = getCommentParentId(comment); const parentId = getCommentParentId(comment);
if (parentId) { if (parentId) {
i.context.router.history.push( i.context.router.history.push(`/comment/${parentId}`);
`/post/${comment?.post_id}/comment/${parentId}`,
);
} }
} }
} }

View file

@ -67,7 +67,7 @@ export const routes: IRoutePropsWithFetch<Record<string, any>>[] = [
fetchInitialData: Communities.fetchInitialData, fetchInitialData: Communities.fetchInitialData,
}, },
{ {
path: `/post/:post_id/comment/:comment_id`, path: `/comment/:comment_id`,
component: Post, component: Post,
fetchInitialData: Post.fetchInitialData, fetchInitialData: Post.fetchInitialData,
}, },