Skip to content

Commit

Permalink
Merge pull request #1543 from entando/ENG-5080_fix_tour_error
Browse files Browse the repository at this point in the history
ENG-5080 only expand the my_homepage if the tour has started
  • Loading branch information
ichalagashvili authored Sep 22, 2023
2 parents 3a44baa + 30cda00 commit 2b40179
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 15 deletions.
6 changes: 6 additions & 0 deletions src/state/app-tour/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
CLEAR_APP_TOUR_PROGRESS, SET_TOUR_CREATED_PAGE,
SET_PUBLISH_STATUS, SET_WIZARD_ENABLED,
SET_EXISTING_PAGES,
SET_WIZARD_CAN_BE_SHOWN,
} from 'state/app-tour/types';
import { getWizardEnabled } from 'state/app-tour/selectors';
import { getUserPreferences } from 'api/userPreferences';
Expand Down Expand Up @@ -43,6 +44,11 @@ export const setWizardEnabled = enabled => ({
payload: enabled,
});

export const setWizardCanBeShown = canBeShown => ({
type: SET_WIZARD_CAN_BE_SHOWN,
payload: canBeShown,
});

export const fetchWizardEnabled = username => (dispatch, getState) => (
new Promise((resolve) => {
const wizardEnabled = getWizardEnabled(getState());
Expand Down
11 changes: 10 additions & 1 deletion src/state/app-tour/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
CLEAR_APP_TOUR_PROGRESS, SET_TOUR_CREATED_PAGE,
SET_PUBLISH_STATUS, SET_WIZARD_ENABLED,
SET_EXISTING_PAGES,
SET_WIZARD_CAN_BE_SHOWN,
} from 'state/app-tour/types';
import { APP_TOUR_STARTED } from './const';

Expand Down Expand Up @@ -45,8 +46,16 @@ const appTour = (state = {}, action = {}) => {
existingPages: action.payload,
};
}
case SET_WIZARD_CAN_BE_SHOWN: {
return {
...state,
wizardCanBeShown: action.payload,
};
}
case CLEAR_APP_TOUR_PROGRESS: {
return {};
return {
wizardCanBeShown: state.wizardCanBeShown,
};
}
default: return state;
}
Expand Down
1 change: 1 addition & 0 deletions src/state/app-tour/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export const getTourCreatedPage = state => state.appTour.page;
export const getPublishStatus = state => state.appTour.status;
export const getWizardEnabled = state => state.appTour.wizardEnabled;
export const getExistingPages = state => state.appTour.existingPages;
export const getWizardCanBeShown = state => state.appTour.wizardCanBeShown;
1 change: 1 addition & 0 deletions src/state/app-tour/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export const SET_TOUR_CREATED_PAGE = 'app-tour/set-tour-created-page';
export const SET_PUBLISH_STATUS = 'app-tour/set-publish-status';
export const SET_WIZARD_ENABLED = 'app-tour/set-wizard-enabled';
export const SET_EXISTING_PAGES = 'app-tour/set-existing-pages';
export const SET_WIZARD_CAN_BE_SHOWN = 'app-tour/set-wizard-can-be-shown';
10 changes: 9 additions & 1 deletion src/state/pages/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { toggleLoading } from 'state/loading/actions';
import { getDefaultLanguage } from 'state/languages/selectors';

import { APP_TOUR_CANCELLED, APP_TOUR_STARTED, APP_TOUR_HOMEPAGE_CODEREF } from 'state/app-tour/const';
import { setExistingPages } from 'state/app-tour/actions';
import { setExistingPages, setWizardCanBeShown } from 'state/app-tour/actions';
import { getAppTourProgress } from 'state/app-tour/selectors';

const INVALID_PAGE_POSITION_ERROR = { id: 'page.invalidPositionError' };
Expand Down Expand Up @@ -194,6 +194,11 @@ export const fetchPage = wrapApiCall(getPage);
export const fetchPageInfo = wrapApiCall(SEO_ENABLED ? getPageSEO : getPage);
export const fetchPageChildren = wrapApiCall(getPageChildren);

export const fetchIfPageExists = pageCode => new Promise((resolve) => {
getPage(pageCode).then(response => resolve(response.ok)).catch(() => resolve(false));
});


export const fetchViewPages = () => dispatch => new Promise((resolve) => {
getViewPages().then((response) => {
response.json().then((json) => {
Expand All @@ -220,6 +225,9 @@ export const sendDeletePage = (page, successRedirect = true) => async (dispatch)
},
}, TOAST_SUCCESS));
dispatch(removePage(page));
if (page.code === APP_TOUR_HOMEPAGE_CODEREF) {
dispatch(setWizardCanBeShown(false));
}
if (page.tourProgress === APP_TOUR_CANCELLED) return;
if ((page.tourProgress !== APP_TOUR_STARTED && successRedirect) || page.redirectToPageTree) {
history.push(ROUTE_PAGE_TREE);
Expand Down
9 changes: 7 additions & 2 deletions src/ui/app-tour/AppTour.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class AppTour extends React.Component {
componentDidMount() {
window.addEventListener(LISTEN_TUTORIAL_START, this.listenTutorialStart);
window.addEventListener(LISTEN_TUTORIAL_NEXT_STEP, this.listenTutorialNextStep);
this.props.checkIfWizardCanBeShown(this.props.isSuperuser);
}

componentDidUpdate(prevProps) {
Expand Down Expand Up @@ -464,12 +465,13 @@ class AppTour extends React.Component {
render() {
const {
wizardEnabled, appTourLastStep, appTourProgress, lockBodyScroll, customOffset, isDismissed,
isSuperuser,
isSuperuser, wizardCanBeShown,
} = this.props;
// sessionStorage is persistent between rerenders
// and is cleared out when the current tab is closed,
// if the wizard is completed or close on a session live, users won't see it anymore.
if (!wizardEnabled || appTourProgress === APP_TOUR_CANCELLED || isDismissed || !isSuperuser) {
if (!wizardEnabled || appTourProgress === APP_TOUR_CANCELLED || isDismissed || !isSuperuser ||
!wizardCanBeShown) {
return null;
}
const maskName = [1, 12, 14, 15].includes(appTourLastStep) ? 'Mask' : '';
Expand Down Expand Up @@ -534,6 +536,8 @@ AppTour.propTypes = {
onAppTourFinish: PropTypes.func.isRequired,
isDismissed: PropTypes.bool,
isSuperuser: PropTypes.bool.isRequired,
wizardCanBeShown: PropTypes.oneOfType([PropTypes.bool, undefined]),
checkIfWizardCanBeShown: PropTypes.func.isRequired,
};

AppTour.defaultProps = {
Expand All @@ -551,6 +555,7 @@ AppTour.defaultProps = {
customOffset: 0,
publishStatus: false,
isDismissed: false,
wizardCanBeShown: undefined,
};

export default withPermissionValues(AppTour);
20 changes: 17 additions & 3 deletions src/ui/app-tour/AppTourContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { withRouter } from 'react-router-dom';
import { getUsername } from '@entando/apimanager';

import AppTour from 'ui/app-tour/AppTour';
import { getAppTourlastStep, getAppTourProgress, getPublishStatus, getTourCreatedPage, getWizardEnabled } from 'state/app-tour/selectors';
import { getAppTourlastStep, getAppTourProgress, getPublishStatus, getTourCreatedPage, getWizardCanBeShown, getWizardEnabled } from 'state/app-tour/selectors';
import { getActiveLanguages } from 'state/languages/selectors';
import { setAppTourLastStep, setAppTourProgress, setPublishStatus } from 'state/app-tour/actions';
import { setAppTourLastStep, setAppTourProgress, setPublishStatus, setWizardCanBeShown } from 'state/app-tour/actions';
import { updateConfiguredPageWidget } from 'state/widget-config/actions';

import { APP_TOUR_CANCELLED, APP_TOUR_STARTED, APP_TOUR_HOMEPAGE_CODEREF } from 'state/app-tour/const';
import { sendDeletePage, unpublishSelectedPage } from 'state/pages/actions';
import { fetchIfPageExists, sendDeletePage, unpublishSelectedPage } from 'state/pages/actions';
import { ROUTE_DASHBOARD, ROUTE_PAGE_ADD, ROUTE_PAGE_TREE } from 'app-init/router';
import { initConfigPage, configOrUpdatePageWidget } from 'state/page-config/actions';
import { updateUserPreferences } from 'state/user-preferences/actions';
Expand Down Expand Up @@ -42,9 +42,23 @@ export const mapStateToProps = (state, { lockBodyScroll = true }) => {
tourCreatedPageCode: pageCode,
publishStatus: getPublishStatus(state),
isDismissed,
wizardCanBeShown: getWizardCanBeShown(state),
};
};
export const mapDispatchToProps = (dispatch, { history }) => ({
checkIfWizardCanBeShown: (isSuperuser) => {
if (isSuperuser) {
fetchIfPageExists(APP_TOUR_HOMEPAGE_CODEREF).then((response) => {
if (response) {
dispatch(setWizardCanBeShown(true));
} else {
dispatch(setWizardCanBeShown(false));
}
}).catch(() => {
dispatch(setWizardCanBeShown(false));
});
}
},
onToggleDontShow: (disableWizard, username) => {
dispatch(updateUserPreferences(username, { wizard: !disableWizard, showToast: false }));
},
Expand Down
6 changes: 4 additions & 2 deletions src/ui/internal-page/InfoMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ import React, { useCallback } from 'react';
import PropTypes from 'prop-types';
import { FormattedMessage } from 'react-intl';
import { LinkMenuItem } from '@entando/menu';
import { useDispatch } from 'react-redux';
import { useDispatch, useSelector } from 'react-redux';

import InfoDropdown from 'ui/internal-page/InfoDropdown';
import { setVisibleModal } from 'state/modal/actions';
import { MODAL_ID as ABOUT_MODAL_ID } from 'ui/about/AboutInfoModal';
import { MODAL_ID as LICENSE_MODAL_ID } from 'ui/license/LicenseInfoModal';
import { withPermissionValues } from 'ui/auth/withPermissions';
import { getWizardCanBeShown } from 'state/app-tour/selectors';

const InfoMenu = ({ onStartTutorial, isSuperuser }) => {
const dispatch = useDispatch();
const wizardCanBeShown = useSelector(getWizardCanBeShown);

const handleAboutClick = useCallback(
() => dispatch(setVisibleModal(ABOUT_MODAL_ID)),
Expand Down Expand Up @@ -52,7 +54,7 @@ const InfoMenu = ({ onStartTutorial, isSuperuser }) => {
</a>
</li>
{
isSuperuser ? (
isSuperuser && wizardCanBeShown ? (
<LinkMenuItem
id="info-menu-start-tutorial"
to="#"
Expand Down
4 changes: 2 additions & 2 deletions src/ui/pages/list/PageTreePageContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { getLoading } from 'state/loading/selectors';
import withPermissions from 'ui/auth/withPermissions';
import { MANAGE_PAGES_PERMISSION } from 'state/permissions/const';
import { setAppTourLastStep } from 'state/app-tour/actions';
import { APP_TOUR_HOMEPAGE_CODEREF, APP_TOUR_CANCELLED } from 'state/app-tour/const';
import { APP_TOUR_HOMEPAGE_CODEREF, APP_TOUR_STARTED } from 'state/app-tour/const';
import { getAppTourProgress } from 'state/app-tour/selectors';
import { fetchLanguages } from 'state/languages/actions';

Expand All @@ -32,7 +32,7 @@ export const mapDispatchToProps = dispatch => ({
dispatch(toggleLoading('pageTree'));
dispatch(handleExpandPage())
.then(() => {
if (appTourProgress !== APP_TOUR_CANCELLED && isSuperuser) {
if (appTourProgress === APP_TOUR_STARTED && isSuperuser) {
dispatch(handleExpandPage(APP_TOUR_HOMEPAGE_CODEREF)).finally(() => dispatch(toggleLoading('pageTree')));
} else {
dispatch(toggleLoading('pageTree'));
Expand Down
12 changes: 10 additions & 2 deletions test/state/app-tour/actions.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
setAppTourProgress, setAppTourLastStep, setWizardEnabled,
setTourCreatedPage, clearAppTourProgress, setPublishStatus,
setExistingPages,
setExistingPages, setWizardCanBeShown,
} from 'state/app-tour/actions';

import {
SET_APP_TOUR_PROGRESS, SET_APP_TOUR_LAST_STEP, SET_WIZARD_ENABLED,
CLEAR_APP_TOUR_PROGRESS, SET_TOUR_CREATED_PAGE, SET_PUBLISH_STATUS,
SET_EXISTING_PAGES,
SET_EXISTING_PAGES, SET_WIZARD_CAN_BE_SHOWN,
} from 'state/app-tour/types';

describe('state/app-tour/actions', () => {
Expand Down Expand Up @@ -68,4 +68,12 @@ describe('state/app-tour/actions', () => {
expect(action.payload).toEqual(pages);
});
});

describe('setWizardCanBeShown', () => {
it('test setWizardCanBeShown action sets the correct type', () => {
const action = setWizardCanBeShown(true);
expect(action.type).toEqual(SET_WIZARD_CAN_BE_SHOWN);
expect(action.payload).toEqual(true);
});
});
});
14 changes: 13 additions & 1 deletion test/state/app-tour/reducer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import reducer from 'state/app-tour/reducer';
import {
setAppTourProgress, setAppTourLastStep, setWizardEnabled,
setTourCreatedPage, clearAppTourProgress, setPublishStatus,
setExistingPages,
setExistingPages, setWizardCanBeShown,
} from 'state/app-tour/actions';
import {
getAppTourProgress, getAppTourlastStep, getExistingPages,
getTourCreatedPage, getPublishStatus, getWizardEnabled,
getWizardCanBeShown,
} from 'state/app-tour/selectors';

describe('state/permssions/reducer', () => {
Expand Down Expand Up @@ -98,4 +99,15 @@ describe('state/permssions/reducer', () => {
expect(getAppTourlastStep({ appTour: newState })).toEqual(undefined);
});
});

describe('after action setWizardCanBeShown', () => {
let newState;
beforeEach(() => {
newState = reducer(state, setWizardCanBeShown(true));
});

it('should define the correct wizard can be shown payload', () => {
expect(getWizardCanBeShown({ appTour: newState })).toEqual(true);
});
});
});
7 changes: 7 additions & 0 deletions test/state/app-tour/selectors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
getAppTourProgress, getAppTourlastStep,
getTourCreatedPage, getPublishStatus,
getWizardEnabled, getExistingPages,
getWizardCanBeShown,
} from 'state/app-tour/selectors';

const TEST_STATE = {
Expand All @@ -12,6 +13,7 @@ const TEST_STATE = {
status: 'published',
wizardEnabled: true,
existingPages: [{ code: '1' }],
wizardCanBeShown: true,
},
};

Expand Down Expand Up @@ -45,4 +47,9 @@ describe('state/app-tour/selectors', () => {
expect(getExistingPages(TEST_STATE))
.toEqual(TEST_STATE.appTour.existingPages);
});

it('verify getWizardCanBeShown selector', () => {
expect(getWizardCanBeShown(TEST_STATE))
.toEqual(TEST_STATE.appTour.wizardCanBeShown);
});
});
19 changes: 18 additions & 1 deletion test/state/pages/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
fetchPageForm, sendPutPage, setFreePages, fetchFreePages, fetchPageSettings, publishSelectedPage,
unpublishSelectedPage, loadSelectedPage, removePage, sendDeletePage, clearSearchPage, clearSearch,
setReferenceSelectedPage, clonePage, clearTree, sendPutPageSettings, sendPatchPage,
fetchPageTreeAll, setBatchExpanded, fetchDashboardPages, setVirtualRoot,
fetchPageTreeAll, setBatchExpanded, fetchDashboardPages, setVirtualRoot, fetchIfPageExists,
} from 'state/pages/actions';

import {
Expand Down Expand Up @@ -47,6 +47,7 @@ jest.mock('state/page-config/selectors', () => ({
}));

jest.mock('state/pages/selectors', () => ({
getPage: jest.fn(),
getStatusMap: jest.fn(),
getPagesMap: jest.fn(),
getChildrenMap: jest.fn(),
Expand Down Expand Up @@ -937,3 +938,19 @@ describe('fetchDashboardPages', () => {
});
});
});

describe('fetchIfPageExists', () => {
it('fetchIfPageExists promise should resolve true if page exists', () => {
getPage.mockImplementation(mockApi({ payload: HOMEPAGE_PAYLOAD }));
return fetchIfPageExists('homepage').then((exists) => {
expect(exists).toBe(true);
});
});

it('fetchIfPageExists promise should resolve false if page does not exist', () => {
getPage.mockImplementation(mockApi({ errors: true }));
return fetchIfPageExists('homepage').then((exists) => {
expect(exists).toBe(false);
});
});
});
2 changes: 2 additions & 0 deletions test/ui/app-tour/AppTour.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const props = {
onAddBannerWidget: jest.fn(),
onAddContentListWidget: jest.fn(),
onAddSitemapMenu: jest.fn(),
checkIfWizardCanBeShown: jest.fn(),
wizardCanBeShown: true,
};

jest.unmock('react-redux');
Expand Down

0 comments on commit 2b40179

Please sign in to comment.