Skip to content

Commit

Permalink
feat: restricted unnecessary api calls (#683)
Browse files Browse the repository at this point in the history
* feat: restricted unnecessary api calls

* fix: fixed content unavailable issue for user admin

* refactor: refactor code for course status
  • Loading branch information
sundasnoreen12 authored Apr 4, 2024
1 parent 4914f51 commit 7db4fde
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
13 changes: 8 additions & 5 deletions src/discussions/data/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions src/discussions/data/selectors.js
Original file line number Diff line number Diff line change
@@ -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 => ({
Expand Down Expand Up @@ -69,19 +71,22 @@ export const selectIsUserLearner = createSelector(
selectUserIsStaff,
selectIsCourseAdmin,
selectIsCourseStaff,
selectCourseTabs,
(
userHasModerationPrivileges,
userIsGroupTa,
userIsStaff,
userIsCourseAdmin,
userIsCourseStaff,
{ courseStatus },
) => (
(
!userHasModerationPrivileges
&& !userIsGroupTa
&& !userIsStaff
&& !userIsCourseAdmin
&& !userIsCourseStaff
&& courseStatus === LOADED
) || false
),
);
4 changes: 2 additions & 2 deletions src/discussions/discussions-home/DiscussionsHome.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -172,7 +172,7 @@ const DiscussionsHome = () => {
</div>
)
)}
{!enableInContextSidebar && (<DiscussionsProductTour />)}
{!enableInContextSidebar && isEnrolled && (<DiscussionsProductTour />)}
</main>
{!enableInContextSidebar && <Footer />}
</DiscussionContext.Provider>
Expand Down

0 comments on commit 7db4fde

Please sign in to comment.