Files
frontend-app-learning/src/generic/path-fixes/PathFixesProvider.jsx
Syed Ali Abbas Zaidi b788b969c3 feat: upgrade react router to v6 (#1128)
* feat: upgrade react router to v6

* fix: all test cases affected by react router upgrade

* refactor: fix navigations

* fix: test cases affectewd due to lib-special-exams

* refactor: improve code coverage
2023-10-04 17:34:53 -04:00

43 lines
1.4 KiB
JavaScript

import { Navigate, useLocation } from 'react-router-dom';
import PropTypes from 'prop-types';
import { sendTrackEvent } from '@edx/frontend-platform/analytics';
/**
* We have seen evidence of learners hitting MFE pages with spaces instead of plus signs (which are used commonly
* in our course keys). It's possible something out there is un-escaping our paths before sending learners to them.
*
* So this provider fixes those paths up and logs it so that we can try to fix the source.
*
* This might be temporary, based on how much we can fix the sources of these urls-with-spaces.
*/
const PathFixesProvider = ({ children }) => {
const location = useLocation();
// We only check for spaces. That's not the only kind of character that is escaped in URLs, but it would always be
// present for our cases, and I believe it's the only one we use normally.
if (location.pathname.includes(' ') || location.pathname.includes('%20')) {
const newLocation = {
...location,
pathname: (location.pathname.replaceAll(' ', '+')).replaceAll('%20', '+'),
};
sendTrackEvent('edx.ui.lms.path_fixed', {
new_path: newLocation.pathname,
old_path: location.pathname,
referrer: document.referrer,
search: location.search,
});
return (<Navigate to={newLocation} replace />);
}
return children; // pass through
};
PathFixesProvider.propTypes = {
children: PropTypes.node.isRequired,
};
export default PathFixesProvider;