fix: fixed api calling issues for admin (#691)

* 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>
This commit is contained in:
sundasnoreen12
2024-04-05 16:48:36 +05:00
committed by GitHub
parent c0873df575
commit d7fcc86847
5 changed files with 33 additions and 14 deletions

View File

@@ -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';
@@ -32,6 +34,7 @@ import {
selectIsCourseAdmin,
selectIsCourseStaff,
selectIsPostingEnabled,
selectIsUserLearner,
selectPostThreadCount,
selectUserHasModerationPrivileges,
selectUserIsGroupTa,
@@ -72,22 +75,34 @@ export const useSidebarVisible = () => {
return !hideSidebar;
};
export function useCourseDiscussionData(courseId, isEnrolled) {
export function useCourseDiscussionData(courseId) {
const dispatch = useDispatch();
const { authenticatedUser } = useContext(AppContext);
useEffect(() => {
async function fetchBaseData() {
if (isEnrolled) {
await dispatch(fetchCourseConfig(courseId));
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 (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) {

View File

@@ -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,
@@ -86,7 +86,7 @@ export const selectIsUserLearner = createSelector(
&& !userIsStaff
&& !userIsCourseAdmin
&& !userIsCourseStaff
&& courseStatus === LOADED
&& isCourseStatusValid(courseStatus)
) || false
),
);

View File

@@ -12,12 +12,11 @@ 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';
@@ -25,6 +24,7 @@ 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'));
@@ -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.
@@ -120,7 +121,7 @@ const DiscussionsHome = () => {
</Routes>
</Suspense>
)}
{(courseStatus === LOADED) && (
{isCourseStatusValid(courseStatus) && (
!isEnrolled && isUserLearner ? (
<Suspense fallback={(<Spinner />)}>
<Routes>

View File

@@ -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`);

View File

@@ -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';
@@ -313,3 +314,5 @@ export function getAuthorLabel(intl, authorLabel) {
return authorLabelMappings[authorLabel] || {};
}
export const isCourseStatusValid = (courseStatus) => [DENIED, LOADED].includes(courseStatus);