Files
frontend-app-learning/src/shared/access.js
Michael Terry b4bedfe3f0 fix: re-enable access error redirects for course home (#570)
These redirects are already in place for the courseware, and will
redirect to the outline page, or the dashboard, or wherever, based
on the type of access error (unenrolled, access expired, survey
needed, etc).

This commit stops the course home tabs from paying attention to the
401 error messages coming from the backend - course_access in the
metadata API handles that now.

This commit also changes our useModel hook to more gracefully handle
not being able to find the model - it is a valid case we want to
allow (still will cause problems if you actually try to use the data,
but will at least provide an object you can inspect).
2021-07-30 14:20:42 -04:00

42 lines
1.8 KiB
JavaScript

/* eslint-disable import/prefer-default-export */
import { getLocale } from '@edx/frontend-platform/i18n';
// This function inspects an access denied error and provides a redirect url (looks like a /redirect/... path),
// which then renders a nice little message while the browser loads the next page.
// This is basically a frontend version of check_course_access_with_redirect in the backend.
export function getAccessDeniedRedirectUrl(courseId, activeTabSlug, courseAccess, start, unitId) {
let url = null;
switch (courseAccess.errorCode) {
case 'audit_expired':
url = `/redirect/dashboard?access_response_error=${courseAccess.additionalContextUserMessage}`;
break;
case 'course_not_started':
// eslint-disable-next-line no-case-declarations
const startDate = (new Intl.DateTimeFormat(getLocale())).format(new Date(start));
url = `/redirect/dashboard?notlive=${startDate}`;
break;
case 'survey_required':
url = `/redirect/survey/${courseId}`;
break;
case 'unfulfilled_milestones':
url = '/redirect/dashboard';
break;
case 'microfrontend_disabled':
// This code path is only used by the courseware right now. The course home tabs each have their own check for
// this in the tab-specific API calls. In those cases, the API will return an http status code if the MFE version
// of those tabs are disabled, rather than an access error like this. We could try to unify these approaches, but
// hopefully the legacy code isn't around long enough for that to be worth it.
if (unitId) {
url = `/redirect/courseware/${courseId}/unit/${unitId}`;
}
break;
case 'authentication_required':
case 'enrollment_required':
default:
if (activeTabSlug !== 'outline') {
url = `/redirect/course-home/${courseId}`;
}
}
return url;
}