Skip to content

Commit

Permalink
Merge pull request #149 from InseeFr/develop
Browse files Browse the repository at this point in the history
fix MFE issue
  • Loading branch information
SimonDmz authored Aug 22, 2024
2 parents 3843742 + 4688ce6 commit a27c16d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pearl",
"version": "2.1.2",
"version": "2.1.3",
"private": true,
"dependencies": {
"@emotion/react": "^11.11.1",
Expand Down
11 changes: 8 additions & 3 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { lazy, Suspense } from 'react';
import { createBrowserRouter, Outlet, RouterProvider } from 'react-router-dom';
import { loadConfiguration, useConfiguration } from './utils/hooks/useConfiguration';
import { PearlTheme } from './ui/PearlTheme';
Expand All @@ -14,15 +14,20 @@ import './app.css';
import { SuiviPage } from './pages/SuiviPage';
import { LocalizationProvider } from '@mui/x-date-pickers';
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
import { QueenPage } from './pages/QueenPage';
import { ServiceWorkerStatus } from './ui/ServiceWorkerStatus';
import { ResetData } from './pages/ResetData';
import { enUS, fr } from 'date-fns/locale';

const QueenPage = lazy(() => import('./pages/QueenPage'));

const router = createBrowserRouter([
{
path: '/queen/*',
element: <QueenPage />,
element: (
<Suspense>
<QueenPage />
</Suspense>
),
},
{
path: '/',
Expand Down
37 changes: 34 additions & 3 deletions src/pages/QueenPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,50 @@ import { useLocation, useNavigate } from 'react-router-dom';
import { mount } from 'dramaQueen/DramaIndex';
import { useQueenListener } from '../utils/hooks/useQueenListener';

const queenPathname = '/queen';

/**
* Mount Queen to sync data
* @returns {JSX.Element}
* @constructor
*/
export function QueenPage() {
/** @var {import('react').Ref<HTMLDivElement>} ref */

export default function QueenPage() {
const ref = useRef(null);
const location = useLocation();

const navigate = useNavigate();

useQueenListener(navigate);

// Listen to navigation events dispatched inside Drama Queen mfe.
useEffect(() => {
const dramaQueenNavigationEventHandler = event => {
const pathname = event.detail;
const newPathname = `${queenPathname}${pathname}`;
if (newPathname === location.pathname) {
return;
}
navigate(newPathname);
};
window.addEventListener('[Drama Queen] navigated', dramaQueenNavigationEventHandler);

return () => {
window.removeEventListener('[Drama Queen] navigated', dramaQueenNavigationEventHandler);
};
}, [location]);

// Listen for Pearl location changes and dispatch a notification.
useEffect(() => {
if (location.pathname.startsWith(queenPathname)) {
window.dispatchEvent(
new CustomEvent('[Pearl] navigated', {
detail: location.pathname.replace(queenPathname, ''),
})
);
}
}, [location]);

const isFirstRunRef = useRef(true);
const unmountRef = useRef(() => {});

Expand All @@ -25,7 +56,7 @@ export function QueenPage() {
}
unmountRef.current = mount({
mountPoint: ref.current,
initialPathname: location.pathname.replace('', ''),
initialPathname: location.pathname.replace(queenPathname, ''),
});
isFirstRunRef.current = false;
}, [location]);
Expand Down
6 changes: 4 additions & 2 deletions src/ui/Questionnaire/Questionnaires.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { Row } from '../Row';
import Stack from '@mui/material/Stack';
import React from 'react';
import Button from '@mui/material/Button';
import { Link as RouterLink } from 'react-router-dom';

import LibraryBooksIcon from '@mui/icons-material/LibraryBooks';
import StickyNote2Icon from '@mui/icons-material/StickyNote2';
import D from 'i18n';
Expand All @@ -28,8 +30,8 @@ export function Questionnaires({ surveyUnit }) {
variant="contained"
disabled={!isAvailable}
startIcon={<LibraryBooksIcon />}
component="a"
href={`/queen/survey-unit/${id}`}
component={RouterLink}
to={`/queen/survey-unit/${id}`}
>
{D.accessTheQuestionnaire}
</Button>
Expand Down

0 comments on commit a27c16d

Please sign in to comment.