Skip to content

Commit

Permalink
fix: fixed api calling issues for admin (openedx#691)
Browse files Browse the repository at this point in the history
* fix: fixed api calling issues for admin

* test: fixed test case

* refactor: fixed review issue

---------

Co-authored-by: Awais Ansari <79941147+awais-ansari@users.noreply.github.com>
  • Loading branch information
sundasnoreen12 and awais-ansari committed Apr 5, 2024
1 parent c0873df commit d7fcc86
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 13 deletions.
27 changes: 21 additions & 6 deletions src/discussions/data/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { getAuthenticatedUser } from '@edx/frontend-platform/auth';
import { useIntl } from '@edx/frontend-platform/i18n';
import { AppContext } from '@edx/frontend-platform/react';

import selectCourseTabs from '../../components/NavigationBar/data/selectors';
import { LOADED } from '../../components/NavigationBar/data/slice';
import fetchTab from '../../components/NavigationBar/data/thunks';
import { RequestStatus, Routes } from '../../data/constants';
import { selectTopicsUnderCategory } from '../../data/selectors';
Expand All @@ -32,6 +34,7 @@ import {
selectIsCourseAdmin,
selectIsCourseStaff,
selectIsPostingEnabled,
selectIsUserLearner,
selectPostThreadCount,
selectUserHasModerationPrivileges,
selectUserIsGroupTa,
Expand Down Expand Up @@ -72,22 +75,34 @@ export const useSidebarVisible = () => {
return !hideSidebar;
};

export function useCourseDiscussionData(courseId, isEnrolled) {
export function useCourseDiscussionData(courseId) {
const dispatch = useDispatch();

useEffect(() => {
async function fetchBaseData() {
await dispatch(fetchCourseConfig(courseId));
await dispatch(fetchTab(courseId));
}

fetchBaseData();
}, [courseId]);
}

export function useCourseBlockData(courseId) {
const dispatch = useDispatch();
const { authenticatedUser } = useContext(AppContext);
const { isEnrolled, courseStatus } = useSelector(selectCourseTabs);
const isUserLearner = useSelector(selectIsUserLearner);

useEffect(() => {
async function fetchBaseData() {
if (isEnrolled) {
await dispatch(fetchCourseConfig(courseId));
if (courseStatus === LOADED && (!isUserLearner || isEnrolled)) {
await dispatch(fetchCourseBlocks(courseId, authenticatedUser.username));
} else {
await dispatch(fetchTab(courseId));
}
}

fetchBaseData();
}, [courseId, isEnrolled]);
}, [courseId, isEnrolled, courseStatus, isUserLearner]);
}

export function useRedirectToThread(courseId, enableInContextSidebar) {
Expand Down
4 changes: 2 additions & 2 deletions src/discussions/data/selectors.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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';
import { isCourseStatusValid } from '../utils';

export const selectAnonymousPostingConfig = state => ({
allowAnonymous: state.config.allowAnonymous,
Expand Down Expand Up @@ -86,7 +86,7 @@ export const selectIsUserLearner = createSelector(
&& !userIsStaff
&& !userIsCourseAdmin
&& !userIsCourseStaff
&& courseStatus === LOADED
&& isCourseStatusValid(courseStatus)
) || false
),
);
9 changes: 5 additions & 4 deletions src/discussions/discussions-home/DiscussionsHome.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ import { LearningHeader as Header } from '@edx/frontend-component-header';

import { Spinner } from '../../components';
import selectCourseTabs from '../../components/NavigationBar/data/selectors';
import { LOADED } from '../../components/NavigationBar/data/slice';
import { ALL_ROUTES, DiscussionProvider, Routes as ROUTES } from '../../data/constants';
import DiscussionContext from '../common/context';
import ContentUnavailable from '../content-unavailable/ContentUnavailable';
import {
useCourseDiscussionData, useIsOnDesktop, useRedirectToThread, useSidebarVisible,
useCourseBlockData, useCourseDiscussionData, useIsOnDesktop, useRedirectToThread, useSidebarVisible,
} from '../data/hooks';
import { selectDiscussionProvider, selectEnableInContext, selectIsUserLearner } from '../data/selectors';
import { EmptyLearners, EmptyTopics } from '../empty-posts';
import EmptyPosts from '../empty-posts/EmptyPosts';
import { EmptyTopic as InContextEmptyTopics } from '../in-context-topics/components';
import messages from '../messages';
import { selectPostEditorVisible } from '../posts/data/selectors';
import { isCourseStatusValid } from '../utils';
import useFeedbackWrapper from './FeedbackWrapper';

const Footer = lazy(() => import('@edx/frontend-component-footer'));
Expand Down Expand Up @@ -58,8 +58,9 @@ const DiscussionsHome = () => {
courseId, postId, topicId, category, learnerUsername,
} = params;

useCourseDiscussionData(courseId, isEnrolled);
useCourseDiscussionData(courseId);
useRedirectToThread(courseId, enableInContextSidebar);
useCourseBlockData(courseId);
useFeedbackWrapper();
/* Display the content area if we are currently viewing/editing a post or creating one.
If the window is larger than a particular size, show the sidebar for navigating between posts/topics.
Expand Down Expand Up @@ -120,7 +121,7 @@ const DiscussionsHome = () => {
</Routes>
</Suspense>
)}
{(courseStatus === LOADED) && (
{isCourseStatusValid(courseStatus) && (
!isEnrolled && isUserLearner ? (
<Suspense fallback={(<Spinner />)}>
<Routes>
Expand Down
2 changes: 1 addition & 1 deletion src/discussions/discussions-home/DiscussionsHome.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ describe('DiscussionsHome', () => {

it('should display post editor form when click on add a post button in legacy topics view', async () => {
axiosMock.onGet(getDiscussionsConfigUrl(courseId)).reply(200, {
enable_in_context: false,
enable_in_context: false, hasModerationPrivileges: true,
});
await executeThunk(fetchCourseConfig(courseId), store.dispatch, store.getState);
await renderComponent(`/${courseId}/topics`);
Expand Down
3 changes: 3 additions & 0 deletions src/discussions/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {

import { getConfig } from '@edx/frontend-platform';

import { DENIED, LOADED } from '../components/NavigationBar/data/slice';
import {
ContentActions, Routes, ThreadType,
} from '../data/constants';
Expand Down Expand Up @@ -313,3 +314,5 @@ export function getAuthorLabel(intl, authorLabel) {

return authorLabelMappings[authorLabel] || {};
}

export const isCourseStatusValid = (courseStatus) => [DENIED, LOADED].includes(courseStatus);

0 comments on commit d7fcc86

Please sign in to comment.