Skip to content

Commit a27c16d

Browse files
authored
Merge pull request #149 from InseeFr/develop
fix MFE issue
2 parents 3843742 + 4688ce6 commit a27c16d

File tree

4 files changed

+47
-9
lines changed

4 files changed

+47
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pearl",
3-
"version": "2.1.2",
3+
"version": "2.1.3",
44
"private": true,
55
"dependencies": {
66
"@emotion/react": "^11.11.1",

src/App.jsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { lazy, Suspense } from 'react';
22
import { createBrowserRouter, Outlet, RouterProvider } from 'react-router-dom';
33
import { loadConfiguration, useConfiguration } from './utils/hooks/useConfiguration';
44
import { PearlTheme } from './ui/PearlTheme';
@@ -14,15 +14,20 @@ import './app.css';
1414
import { SuiviPage } from './pages/SuiviPage';
1515
import { LocalizationProvider } from '@mui/x-date-pickers';
1616
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
17-
import { QueenPage } from './pages/QueenPage';
1817
import { ServiceWorkerStatus } from './ui/ServiceWorkerStatus';
1918
import { ResetData } from './pages/ResetData';
2019
import { enUS, fr } from 'date-fns/locale';
2120

21+
const QueenPage = lazy(() => import('./pages/QueenPage'));
22+
2223
const router = createBrowserRouter([
2324
{
2425
path: '/queen/*',
25-
element: <QueenPage />,
26+
element: (
27+
<Suspense>
28+
<QueenPage />
29+
</Suspense>
30+
),
2631
},
2732
{
2833
path: '/',

src/pages/QueenPage.jsx

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,50 @@ import { useLocation, useNavigate } from 'react-router-dom';
33
import { mount } from 'dramaQueen/DramaIndex';
44
import { useQueenListener } from '../utils/hooks/useQueenListener';
55

6+
const queenPathname = '/queen';
7+
68
/**
79
* Mount Queen to sync data
810
* @returns {JSX.Element}
911
* @constructor
1012
*/
11-
export function QueenPage() {
12-
/** @var {import('react').Ref<HTMLDivElement>} ref */
13+
14+
export default function QueenPage() {
1315
const ref = useRef(null);
1416
const location = useLocation();
1517

1618
const navigate = useNavigate();
19+
1720
useQueenListener(navigate);
1821

22+
// Listen to navigation events dispatched inside Drama Queen mfe.
23+
useEffect(() => {
24+
const dramaQueenNavigationEventHandler = event => {
25+
const pathname = event.detail;
26+
const newPathname = `${queenPathname}${pathname}`;
27+
if (newPathname === location.pathname) {
28+
return;
29+
}
30+
navigate(newPathname);
31+
};
32+
window.addEventListener('[Drama Queen] navigated', dramaQueenNavigationEventHandler);
33+
34+
return () => {
35+
window.removeEventListener('[Drama Queen] navigated', dramaQueenNavigationEventHandler);
36+
};
37+
}, [location]);
38+
39+
// Listen for Pearl location changes and dispatch a notification.
40+
useEffect(() => {
41+
if (location.pathname.startsWith(queenPathname)) {
42+
window.dispatchEvent(
43+
new CustomEvent('[Pearl] navigated', {
44+
detail: location.pathname.replace(queenPathname, ''),
45+
})
46+
);
47+
}
48+
}, [location]);
49+
1950
const isFirstRunRef = useRef(true);
2051
const unmountRef = useRef(() => {});
2152

@@ -25,7 +56,7 @@ export function QueenPage() {
2556
}
2657
unmountRef.current = mount({
2758
mountPoint: ref.current,
28-
initialPathname: location.pathname.replace('', ''),
59+
initialPathname: location.pathname.replace(queenPathname, ''),
2960
});
3061
isFirstRunRef.current = false;
3162
}, [location]);

src/ui/Questionnaire/Questionnaires.jsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { Row } from '../Row';
55
import Stack from '@mui/material/Stack';
66
import React from 'react';
77
import Button from '@mui/material/Button';
8+
import { Link as RouterLink } from 'react-router-dom';
9+
810
import LibraryBooksIcon from '@mui/icons-material/LibraryBooks';
911
import StickyNote2Icon from '@mui/icons-material/StickyNote2';
1012
import D from 'i18n';
@@ -28,8 +30,8 @@ export function Questionnaires({ surveyUnit }) {
2830
variant="contained"
2931
disabled={!isAvailable}
3032
startIcon={<LibraryBooksIcon />}
31-
component="a"
32-
href={`/queen/survey-unit/${id}`}
33+
component={RouterLink}
34+
to={`/queen/survey-unit/${id}`}
3335
>
3436
{D.accessTheQuestionnaire}
3537
</Button>

0 commit comments

Comments
 (0)