Skip to content

Commit 7db4fde

Browse files
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
1 parent 4914f51 commit 7db4fde

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

src/discussions/data/hooks.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,22 @@ export const useSidebarVisible = () => {
7272
return !hideSidebar;
7373
};
7474

75-
export function useCourseDiscussionData(courseId) {
75+
export function useCourseDiscussionData(courseId, isEnrolled) {
7676
const dispatch = useDispatch();
7777
const { authenticatedUser } = useContext(AppContext);
7878

7979
useEffect(() => {
8080
async function fetchBaseData() {
81-
await dispatch(fetchCourseConfig(courseId));
82-
await dispatch(fetchCourseBlocks(courseId, authenticatedUser.username));
83-
await dispatch(fetchTab(courseId));
81+
if (isEnrolled) {
82+
await dispatch(fetchCourseConfig(courseId));
83+
await dispatch(fetchCourseBlocks(courseId, authenticatedUser.username));
84+
} else {
85+
await dispatch(fetchTab(courseId));
86+
}
8487
}
8588

8689
fetchBaseData();
87-
}, [courseId]);
90+
}, [courseId, isEnrolled]);
8891
}
8992

9093
export function useRedirectToThread(courseId, enableInContextSidebar) {

src/discussions/data/selectors.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { createSelector } from '@reduxjs/toolkit';
22

3+
import selectCourseTabs from '../../components/NavigationBar/data/selectors';
4+
import { LOADED } from '../../components/NavigationBar/data/slice';
35
import { PostsStatusFilter, ThreadType } from '../../data/constants';
46

57
export const selectAnonymousPostingConfig = state => ({
@@ -69,19 +71,22 @@ export const selectIsUserLearner = createSelector(
6971
selectUserIsStaff,
7072
selectIsCourseAdmin,
7173
selectIsCourseStaff,
74+
selectCourseTabs,
7275
(
7376
userHasModerationPrivileges,
7477
userIsGroupTa,
7578
userIsStaff,
7679
userIsCourseAdmin,
7780
userIsCourseStaff,
81+
{ courseStatus },
7882
) => (
7983
(
8084
!userHasModerationPrivileges
8185
&& !userIsGroupTa
8286
&& !userIsStaff
8387
&& !userIsCourseAdmin
8488
&& !userIsCourseStaff
89+
&& courseStatus === LOADED
8590
) || false
8691
),
8792
);

src/discussions/discussions-home/DiscussionsHome.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const DiscussionsHome = () => {
5858
courseId, postId, topicId, category, learnerUsername,
5959
} = params;
6060

61-
useCourseDiscussionData(courseId);
61+
useCourseDiscussionData(courseId, isEnrolled);
6262
useRedirectToThread(courseId, enableInContextSidebar);
6363
useFeedbackWrapper();
6464
/* Display the content area if we are currently viewing/editing a post or creating one.
@@ -172,7 +172,7 @@ const DiscussionsHome = () => {
172172
</div>
173173
)
174174
)}
175-
{!enableInContextSidebar && (<DiscussionsProductTour />)}
175+
{!enableInContextSidebar && isEnrolled && (<DiscussionsProductTour />)}
176176
</main>
177177
{!enableInContextSidebar && <Footer />}
178178
</DiscussionContext.Provider>

0 commit comments

Comments
 (0)