From 7db4fde2527601479f9abcb7032d98c6732eed99 Mon Sep 17 00:00:00 2001
From: sundasnoreen12 <72802712+sundasnoreen12@users.noreply.github.com>
Date: Thu, 4 Apr 2024 12:42:37 +0500
Subject: [PATCH] feat: restricted unnecessary api calls (#683)
* feat: restricted unnecessary api calls
* fix: fixed content unavailable issue for user admin
* refactor: refactor code for course status
---
src/discussions/data/hooks.js | 13 ++++++++-----
src/discussions/data/selectors.js | 5 +++++
.../discussions-home/DiscussionsHome.jsx | 4 ++--
3 files changed, 15 insertions(+), 7 deletions(-)
diff --git a/src/discussions/data/hooks.js b/src/discussions/data/hooks.js
index 1fe96a280..1b86eb4d5 100644
--- a/src/discussions/data/hooks.js
+++ b/src/discussions/data/hooks.js
@@ -72,19 +72,22 @@ export const useSidebarVisible = () => {
return !hideSidebar;
};
-export function useCourseDiscussionData(courseId) {
+export function useCourseDiscussionData(courseId, isEnrolled) {
const dispatch = useDispatch();
const { authenticatedUser } = useContext(AppContext);
useEffect(() => {
async function fetchBaseData() {
- await dispatch(fetchCourseConfig(courseId));
- await dispatch(fetchCourseBlocks(courseId, authenticatedUser.username));
- await dispatch(fetchTab(courseId));
+ if (isEnrolled) {
+ await dispatch(fetchCourseConfig(courseId));
+ await dispatch(fetchCourseBlocks(courseId, authenticatedUser.username));
+ } else {
+ await dispatch(fetchTab(courseId));
+ }
}
fetchBaseData();
- }, [courseId]);
+ }, [courseId, isEnrolled]);
}
export function useRedirectToThread(courseId, enableInContextSidebar) {
diff --git a/src/discussions/data/selectors.js b/src/discussions/data/selectors.js
index 86f3fd9de..cd6da322e 100644
--- a/src/discussions/data/selectors.js
+++ b/src/discussions/data/selectors.js
@@ -1,5 +1,7 @@
import { createSelector } from '@reduxjs/toolkit';
+import selectCourseTabs from '../../components/NavigationBar/data/selectors';
+import { LOADED } from '../../components/NavigationBar/data/slice';
import { PostsStatusFilter, ThreadType } from '../../data/constants';
export const selectAnonymousPostingConfig = state => ({
@@ -69,12 +71,14 @@ export const selectIsUserLearner = createSelector(
selectUserIsStaff,
selectIsCourseAdmin,
selectIsCourseStaff,
+ selectCourseTabs,
(
userHasModerationPrivileges,
userIsGroupTa,
userIsStaff,
userIsCourseAdmin,
userIsCourseStaff,
+ { courseStatus },
) => (
(
!userHasModerationPrivileges
@@ -82,6 +86,7 @@ export const selectIsUserLearner = createSelector(
&& !userIsStaff
&& !userIsCourseAdmin
&& !userIsCourseStaff
+ && courseStatus === LOADED
) || false
),
);
diff --git a/src/discussions/discussions-home/DiscussionsHome.jsx b/src/discussions/discussions-home/DiscussionsHome.jsx
index 23b602dd8..06a302b69 100644
--- a/src/discussions/discussions-home/DiscussionsHome.jsx
+++ b/src/discussions/discussions-home/DiscussionsHome.jsx
@@ -58,7 +58,7 @@ const DiscussionsHome = () => {
courseId, postId, topicId, category, learnerUsername,
} = params;
- useCourseDiscussionData(courseId);
+ useCourseDiscussionData(courseId, isEnrolled);
useRedirectToThread(courseId, enableInContextSidebar);
useFeedbackWrapper();
/* Display the content area if we are currently viewing/editing a post or creating one.
@@ -172,7 +172,7 @@ const DiscussionsHome = () => {
)
)}
- {!enableInContextSidebar && ()}
+ {!enableInContextSidebar && isEnrolled && ()}
{!enableInContextSidebar && }