From c57dfc1fc57a8aa859f1d64b03649372837adba7 Mon Sep 17 00:00:00 2001 From: Kshitij Sobti Date: Wed, 20 Apr 2022 17:56:03 +0530 Subject: [PATCH] fix: Fix constant reloading on topics page and posts links on category page [BD-38] [TNL-9868] [TNL-9846] (#128) This fixes two issues. The first is that the topics page can cause constant requests to threads, and the second, that clicking on a post link when browsing a category can cause the application to crash. --- src/data/constants.js | 8 +- src/discussions/common/context.js | 7 +- .../discussions-home/DiscussionSidebar.jsx | 5 +- .../navigation-bar/NavigationBar.jsx | 10 +- src/discussions/posts/PostsView.jsx | 98 +++++++++---------- src/discussions/posts/PostsView.test.jsx | 10 +- src/discussions/posts/post/PostLink.jsx | 2 + 7 files changed, 77 insertions(+), 63 deletions(-) diff --git a/src/data/constants.js b/src/data/constants.js index 26e7f566..e3646279 100644 --- a/src/data/constants.js +++ b/src/data/constants.js @@ -179,6 +179,7 @@ export const Routes = { `${BASE_PATH}`, ], EDIT_POST: [ + `${BASE_PATH}/category/:category/posts/:postId/edit`, `${BASE_PATH}/topics/:topicId/posts/:postId/edit`, `${BASE_PATH}/posts/:postId/edit`, `${BASE_PATH}/my-posts/:postId/edit`, @@ -186,12 +187,14 @@ export const Routes = { }, COMMENTS: { PATH: [ + `${BASE_PATH}/category/:category/posts/:postId`, `${BASE_PATH}/topics/:topicId/posts/:postId`, `${BASE_PATH}/posts/:postId`, `${BASE_PATH}/my-posts/:postId`, ], PAGE: `${BASE_PATH}/:page`, PAGES: { + category: `${BASE_PATH}/category/:category/posts/:postId`, topics: `${BASE_PATH}/topics/:topicId/posts/:postId`, posts: `${BASE_PATH}/posts/:postId`, 'my-posts': `${BASE_PATH}/my-posts/:postId`, @@ -200,15 +203,18 @@ export const Routes = { TOPICS: { PATH: [ `${BASE_PATH}/topics/:topicId?`, + `${BASE_PATH}/category/:category`, + `${BASE_PATH}/topics`, ], ALL: `${BASE_PATH}/topics`, CATEGORY: `${BASE_PATH}/category/:category`, + CATEGORY_POST: `${BASE_PATH}/category/:category/posts/:postId`, TOPIC: `${BASE_PATH}/topics/:topicId`, }, }; export const ALL_ROUTES = [] - .concat([Routes.TOPICS.CATEGORY]) + .concat([Routes.TOPICS.CATEGORY_POST, Routes.TOPICS.CATEGORY]) .concat(Routes.COMMENTS.PATH) .concat(Routes.TOPICS.PATH) .concat([Routes.POSTS.ALL_POSTS, Routes.POSTS.MY_POSTS]) diff --git a/src/discussions/common/context.js b/src/discussions/common/context.js index 19003713..df63df36 100644 --- a/src/discussions/common/context.js +++ b/src/discussions/common/context.js @@ -2,10 +2,11 @@ import React from 'react'; export const DiscussionContext = React.createContext({ + page: null, courseId: null, postId: null, - category: null, - commentId: null, - learnerUsername: null, + topicId: null, inContext: false, + category: null, + learnerUsername: null, }); diff --git a/src/discussions/discussions-home/DiscussionSidebar.jsx b/src/discussions/discussions-home/DiscussionSidebar.jsx index c1825047..40e4bcd0 100644 --- a/src/discussions/discussions-home/DiscussionSidebar.jsx +++ b/src/discussions/discussions-home/DiscussionSidebar.jsx @@ -24,11 +24,8 @@ export default function DiscussionSidebar({ displaySidebar }) { data-testid="sidebar" > - - - diff --git a/src/discussions/navigation/navigation-bar/NavigationBar.jsx b/src/discussions/navigation/navigation-bar/NavigationBar.jsx index c45effc3..5ec94178 100644 --- a/src/discussions/navigation/navigation-bar/NavigationBar.jsx +++ b/src/discussions/navigation/navigation-bar/NavigationBar.jsx @@ -1,7 +1,7 @@ import React from 'react'; import { useSelector } from 'react-redux'; -import { useParams } from 'react-router'; +import { matchPath, useParams } from 'react-router'; import { NavLink } from 'react-router-dom'; import { injectIntl, intlShape } from '@edx/frontend-platform/i18n'; @@ -27,6 +27,7 @@ function NavigationBar({ intl }) { }, { route: Routes.TOPICS.ALL, + isActive: (match, location) => Boolean(matchPath(location.pathname, { path: Routes.TOPICS.PATH })), labelMessage: messages.allTopics, }, ]; @@ -41,7 +42,12 @@ function NavigationBar({ intl }) {