docbrown/app/javascript/packs/onboardingRedirectCheck.jsx
Julianna Tetreault 00324c38e6
Pulls Creator Onboarding Redirect Links Into a Creator Onboarding-Specific Function (#15654)
* Pulls Creator Onboarding redirect links out into own function

* Adds all pathnames to redirectableCreatorOnbboardingLocation()

* Uses Array.proptypes.includes() in onboardingRedirectCheck.jsx

* Adds back removed pathname from redirectableLocation

* Refactor redirectableCreatorOnboardingLocation()

* Adds back code changes that were reverted at some point :/
2021-12-07 06:47:42 -07:00

85 lines
2.6 KiB
JavaScript

import { getUserDataAndCsrfToken } from '@utilities/getUserDataAndCsrfToken';
HTMLDocument.prototype.ready = new Promise((resolve) => {
if (document.readyState !== 'loading') {
return resolve();
}
document.addEventListener('DOMContentLoaded', () => resolve());
return null;
});
// If localStorage.getItem('shouldRedirectToOnboarding') is not set, i.e. null, that means we should redirect.
function redirectableLocation() {
return ![
'/onboarding',
'/signout_confirm',
'/privacy',
'/admin/creator_settings/new',
].includes(window.location.pathname);
}
function redirectableCreatorOnboardingLocation() {
return (
redirectableLocation() &&
!['/code-of-conduct', '/terms'].includes(window.location.pathname)
);
}
function onboardingSkippable(currentUser) {
return (
currentUser.saw_onboarding &&
currentUser.checked_code_of_conduct &&
currentUser.checked_terms_and_conditions
);
}
function onboardCreator(currentUser) {
return (
document.body.dataset.creator === 'true' &&
document.body.dataset.creatorOnboarding === 'true' &&
!currentUser.saw_onboarding
);
}
document.ready.then(
getUserDataAndCsrfToken()
.then(({ currentUser, csrfToken }) => {
window.currentUser = currentUser;
window.csrfToken = csrfToken;
if (onboardCreator(currentUser)) {
if (redirectableCreatorOnboardingLocation()) {
window.location = `${window.location.origin}/admin/creator_settings/new?referrer=${window.location}`;
}
} else if (redirectableLocation() && !onboardingSkippable(currentUser)) {
window.location = `${window.location.origin}/onboarding?referrer=${window.location}`;
}
})
.catch((error) => {
// eslint-disable-next-line no-console
console.error('Error getting user and CSRF Token', error);
}),
);
window.InstantClick.on('change', () => {
getUserDataAndCsrfToken()
.then(({ currentUser }) => {
if (
redirectableCreatorOnboardingLocation() &&
localStorage.getItem('shouldRedirectToOnboarding') === null &&
onboardCreator(currentUser)
) {
window.location = `${window.location.origin}/admin/creator_settings/new?referrer=${window.location}`;
} else if (
redirectableLocation() &&
localStorage.getItem('shouldRedirectToOnboarding') === null &&
!onboardingSkippable(currentUser)
) {
window.location = `${window.location.origin}/onboarding?referrer=${window.location}`;
}
})
.catch((error) => {
// eslint-disable-next-line no-console
console.error('Error getting user and CSRF Token', error);
});
});