Skip to content

Commit

Permalink
ENG-4852 fix admin user was seeing root also
Browse files Browse the repository at this point in the history
  • Loading branch information
ichalagashvili committed May 19, 2023
1 parent ced4d74 commit 16ca000
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
11 changes: 2 additions & 9 deletions src/state/pages/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
SET_DASHBOARD_PAGES,
SET_VIRTUAL_ROOT,
} from 'state/pages/types';
import { HOMEPAGE_CODE } from './const';

// creates a map from an array
const toMap = (array, propKey) => array.reduce((acc, page) => {
Expand Down Expand Up @@ -140,15 +139,9 @@ const childrenMap = (state = {}, action = {}) => {
const titlesMap = (state = {}, action = {}) => {
switch (action.type) {
case ADD_PAGES: {
const mapOfTitles = toMap(action.payload.pages, 'titles');
if (mapOfTitles[HOMEPAGE_CODE]) {
Object.keys(mapOfTitles[HOMEPAGE_CODE]).forEach((key) => {
mapOfTitles[HOMEPAGE_CODE][key] = 'Root';
});
}
return {
...state,
...mapOfTitles,
...toMap(action.payload.pages, 'titles'),
};
}
case UPDATE_PAGE: {
Expand Down Expand Up @@ -293,7 +286,7 @@ export const dashboard = (state = [], action = {}) => {
}
};

export const virtualRoot = (state = {}, action = {}) => {
export const virtualRoot = (state = false, action = {}) => {
switch (action.type) {
case SET_VIRTUAL_ROOT:
return action.payload;
Expand Down
11 changes: 8 additions & 3 deletions src/state/pages/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ const PAGE_STATUS_DEFAULTS = {
};

export const getPageTreePages = createSelector(
[getPagesMap, getChildrenMap, getStatusMap, getTitlesMap, getLocale, getDefaultLanguage],
(pages, pageChildren, pagesStatus, pagesTitles, locale, defaultLang) => (
[getPagesMap, getChildrenMap, getStatusMap, getTitlesMap, getLocale, getDefaultLanguage,
getIsVirtualRootOn],
(pages, pageChildren, pagesStatus, pagesTitles, locale, defaultLang, virtualRootOn) => (
getPagesOrder(pageChildren)
.filter(pageCode => isVisible(pageCode, pages, pagesStatus))
.map((pageCode) => {
Expand All @@ -118,12 +119,16 @@ export const getPageTreePages = createSelector(
.some(el => pages[el] && pages[el].status === PAGE_STATUS_PUBLISHED);
}

const title = pagesTitles[pageCode][locale]
let title = pagesTitles[pageCode][locale]
|| pagesTitles[pageCode][defaultLang]
|| pagesTitles[pageCode][
Object.keys(pagesTitles[pageCode]).find(langCode => pagesTitles[pageCode][langCode])
];

if (pageCode === HOMEPAGE_CODE && virtualRootOn) {
title = 'Root';
}

return ({
...pages[pageCode],
...PAGE_STATUS_DEFAULTS,
Expand Down
10 changes: 10 additions & 0 deletions test/state/pages/selectors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ describe('state/pages/selectors', () => {
let pageTreePages;
beforeEach(() => {
pageTreePages = getPageTreePages(MOCK_STATE);
// make virtualRoot false
MOCK_STATE.pages.virtualRoot = false;
});
it('only returns expanded rows and their children', () => {
expect(pageTreePages.length).toBe(4); // homepage and its 3 children
Expand Down Expand Up @@ -160,6 +162,14 @@ describe('state/pages/selectors', () => {
expect(pageTreePages[2].loaded).toBe(false);
expect(pageTreePages[3].loaded).toBe(false);
});
it('shows Root for home if virtualRoot is true', () => {
MOCK_STATE.pages.virtualRoot = true;
pageTreePages = getPageTreePages(MOCK_STATE);
expect(pageTreePages[0].title).toBe('Root');
expect(pageTreePages[1].title).toBe(pageTreePages[1].titles[LOCALE_MOCK]);
expect(pageTreePages[2].title).toBe(pageTreePages[2].titles[LOCALE_MOCK]);
expect(pageTreePages[3].title).toBe(pageTreePages[3].titles[LOCALE_MOCK]);
});
});

describe('getCharsets(state)', () => {
Expand Down

0 comments on commit 16ca000

Please sign in to comment.