/* global gettext */ import React from 'react'; import FocusLock, { AutoFocusInside } from 'react-focus-lock'; import StringUtils from 'edx-ui-toolkit/js/utils/string-utils'; class EnterpriseLearnerPortalModal extends React.Component { constructor(props) { super(props); this.state = { isModalOpen: false, }; this.openModal = this.openModal.bind(this); this.closeModal = this.closeModal.bind(this); this.handleModalBackdropClick = this.handleModalBackdropClick.bind(this); this.handleEsc = this.handleEsc.bind(this); this.handleLearnerPortalDashboardClick = this.handleLearnerPortalDashboardClick.bind(this); } componentDidMount() { // eslint-disable-next-line react/prop-types const storageKey = `enterprise_learner_portal_modal__${this.props.enterpriseCustomerUUID}`; const hasViewedModal = window.sessionStorage.getItem(storageKey); if (!hasViewedModal) { this.openModal(); document.addEventListener('mousedown', this.handleModalBackdropClick, false); window.sessionStorage.setItem(storageKey, true); document.addEventListener('keydown', this.handleEsc, false); } } componentDidUpdate(prevProps, prevState) { if (this.state.isModalOpen !== prevState.isModalOpen) { if (this.state.isModalOpen) { // add a class here to prevent scrolling on anything that is not the modal document.body.classList.add('modal-open'); } else { // remove the class to allow the dashboard content to scroll document.body.classList.remove('modal-open'); } } } componentWillUnmount() { // remove the class to allow the dashboard content to scroll document.body.classList.remove('modal-open'); document.removeEventListener('mousedown', this.handleModalBackdropClick, false); document.removeEventListener('keydown', this.handleEsc, false); } handleModalBackdropClick(e) { if (this.modalRef && this.modalRef.contains(e.target)) { // click is inside modal, don't close it return; } this.closeModal(); } handleEsc(e) { const { key } = e; if (key === 'Escape') { window.analytics.track('edx.ui.enterprise.lms.dashboard.learner_portal_modal.closed', { // eslint-disable-next-line react/prop-types enterpriseUUID: this.props.enterpriseCustomerUUID, source: 'Escape', }); this.closeModal(); } } // eslint-disable-next-line react/sort-comp closeModal() { this.setState({ isModalOpen: false, }); } openModal() { window.analytics.track('edx.ui.enterprise.lms.dashboard.learner_portal_modal.opened', { // eslint-disable-next-line react/prop-types enterpriseUUID: this.props.enterpriseCustomerUUID, }); this.setState({ isModalOpen: true, }); } getLearnerPortalUrl() { // eslint-disable-next-line react/prop-types const baseUrlWithSlug = `${this.props.enterpriseLearnerPortalBaseUrl}/${this.props.enterpriseCustomerSlug}`; return `${baseUrlWithSlug}?utm_source=lms_dashboard_modal`; } handleLearnerPortalDashboardClick(e) { e.preventDefault(); window.analytics.track('edx.ui.enterprise.lms.dashboard.learner_portal_modal.dashboard_cta.clicked', { // eslint-disable-next-line react/prop-types enterpriseUUID: this.props.enterpriseCustomerUUID, }); setTimeout(() => { window.location.href = this.getLearnerPortalUrl(); }, 300); } render() { if (!this.state.isModalOpen) { return null; } return (
{ this.modalRef = node; }} >
{StringUtils.interpolate( gettext('You have access to the {enterpriseName} dashboard'), { // eslint-disable-next-line react/prop-types enterpriseName: this.props.enterpriseCustomerName, }, )}

{StringUtils.interpolate( gettext('To access the courses available to you through {enterpriseName}, visit the {enterpriseName} dashboard.'), { // eslint-disable-next-line react/prop-types enterpriseName: this.props.enterpriseCustomerName, }, )}

{/* eslint-disable-next-line react/button-has-type */} {gettext('Go to dashboard')}
); } } // eslint-disable-next-line import/prefer-default-export export { EnterpriseLearnerPortalModal };