Skip to content

Commit

Permalink
Hotfix modal (#1199)
Browse files Browse the repository at this point in the history
* Proper unmount
  • Loading branch information
tu55eladd authored Oct 9, 2023
1 parent 96d457a commit d0f9864
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 313 deletions.
495 changes: 218 additions & 277 deletions package-lock.json

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
"dependencies": {
"@amplitude/analytics-browser": "^2.2.0",
"@hookform/resolvers": "^3.2.0",
"@navikt/aksel-icons": "^4.12.1",
"@navikt/aksel-icons": "^5.6.4",
"@navikt/dab-spraksjekk": "^1.0.9",
"@navikt/ds-css": "^5.6.1",
"@navikt/ds-react": "^5.6.1",
"@navikt/ds-tailwind": "^5.6.1",
"@navikt/ds-tokens": "^5.6.1",
"@navikt/ds-css": "^5.6.4",
"@navikt/ds-react": "^5.6.4",
"@navikt/ds-tailwind": "^5.6.4",
"@navikt/ds-tokens": "^5.6.4",
"@reduxjs/toolkit": "^1.9.5",
"@sentry/integrations": "^7.64.0",
"@sentry/react": "^7.64.0",
Expand All @@ -38,12 +38,12 @@
"classnames": "^2.3.2",
"date-fns": "^2.30.0",
"prop-types": "^15.8.1",
"react": "^17.0.2",
"react": "^18.2.0",
"react-collapse": "^5.1.1",
"react-custom-scrollbars-2": "^4.5.0",
"react-dnd": "^16.0.1",
"react-dnd-html5-backend": "^16.0.1",
"react-dom": "^17.0.2",
"react-dom": "^18.2.0",
"react-hook-form": "^7.45.4",
"react-modal": "^3.16.1",
"react-redux": "^8.1.2",
Expand Down Expand Up @@ -86,13 +86,13 @@
"devDependencies": {
"@swc/cli": "^0.1.62",
"@swc/core": "^1.3.78",
"@testing-library/react": "^12.1.5",
"@testing-library/user-event": "^14.4.3",
"@testing-library/react": "^14.0.0",
"@testing-library/user-event": "^14.5.1",
"@types/moment-duration-format": "^2.2.3",
"@types/node": "^18.11.9",
"@types/react": "^17.0.45",
"@types/react": "^18.2.25",
"@types/react-collapse": "^5.0.1",
"@types/react-dom": "^17.0.17",
"@types/react-dom": "^18.2.11",
"@types/react-modal": "^3.16.0",
"@types/react-redux": "^7.1.25",
"@typescript-eslint/eslint-plugin": "^6.4.1",
Expand Down
7 changes: 7 additions & 0 deletions src/Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ export const useErVeileder = (): boolean => {
export const FnrContext = React.createContext<undefined | string>(undefined);
export const useFnr = () => useContext(FnrContext);

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const noOp = (_: string | undefined) => {};
const Provider = ({ children, setFnrRef, fnr: propFnr }: Props) => {
const [fnr, setFnr] = useState(propFnr);
useEffect(() => {
if (setFnrRef) setFnrRef(setFnr);
return () => {
if (setFnrRef) {
setFnrRef(noOp);
}
};
}, []);

const store = useMemo(createStore, [fnr]);
Expand Down
2 changes: 0 additions & 2 deletions src/felles-komponenter/modal/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ const Modal = (props: Props) => {
minstEnAvhengighet = false,
feilmeldinger,
tilbakeLenke,
// ariaLabelledby,
// contentLabel,
} = props;

const navigate = useNavigate();
Expand Down
20 changes: 7 additions & 13 deletions src/rootWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,21 @@ import '@navikt/ds-css';
import './tailwind.css';
import './index.less';

import { Provider as AkselModalMountProvider } from '@navikt/ds-react';
import * as Sentry from '@sentry/react';
import React from 'react';
import * as ReactDOM from 'react-dom';

import App from './app';
import { hash } from './felles-komponenter/utils/hash';
import Provider from './Provider';
import { SentryRoutes } from './sentry';
import { createRoot } from 'react-dom/client';

export const renderAsReactRoot = (appElement: HTMLElement, props?: { fnr?: string }) => {
const rootElement = document.getElementById('root') || undefined;
Sentry.setUser({
id: hash(props?.fnr),
});
ReactDOM.render(
<AkselModalMountProvider rootElement={rootElement} appElement={appElement}>
<Provider fnr={props?.fnr}>
<App Routes={SentryRoutes} key={'1'} />
</Provider>
</AkselModalMountProvider>,
appElement,
const rootElement = createRoot(appElement || document.getElementById('root')!);
Sentry.setUser({ id: hash(props?.fnr) });
rootElement.render(
<Provider fnr={props?.fnr}>
<App Routes={SentryRoutes} key={'1'} />
</Provider>,
);
};
1 change: 1 addition & 0 deletions src/setupTests.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Ikkje bra!
import.meta.env.VITE_API_URL_BASE = 'http://localhost:3000'; // Dette er det som ligger på window.location i jsdom

HTMLDialogElement.prototype.showModal = () => {};
window.IntersectionObserver = vi.fn();
// Mocked because react-dnd uses es6 import and have to be transpiled to work in these tests
vi.mock('react-dnd', () => ({
Expand Down
25 changes: 15 additions & 10 deletions src/webcomponentWrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import dsStyles from '@navikt/ds-css/dist/index.css?inline';
import { Provider as ModalProvider } from '@navikt/ds-react';
import React from 'react';
import * as ReactDOM from 'react-dom';
import { Routes } from 'react-router-dom';

import App from './app';
Expand All @@ -10,9 +8,16 @@ import { LocalStorageElement, settLocalStorage } from './mocks/demo/localStorage
import modulesCss from './moduler/aktivitet/aktivitet-kort/Aktivitetskort.module.less?inline';
import Provider from './Provider';
import tailwindCss from './tailwind.css?inline';
import { createRoot, Root } from 'react-dom/client';

export class DabAktivitetsplan extends HTMLElement {
setFnr?: (fnr: string) => void;
root: Root | undefined;

disconnectedCallback() {
this.root?.unmount();
}

connectedCallback() {
// Cant mount on shadowRoot, create a extra div for mounting modal
const shadowDomFirstChild = document.createElement('div');
Expand All @@ -29,14 +34,14 @@ export class DabAktivitetsplan extends HTMLElement {
shadowRoot.appendChild(styleElem);

const fnr = this.getAttribute('data-fnr') ?? undefined;
settLocalStorage(LocalStorageElement.FNR, fnr);
ReactDOM.render(
<ModalProvider appElement={appRoot} rootElement={shadowDomFirstChild}>
<Provider key={fnr} fnr={fnr} setFnrRef={(setFnr) => (this.setFnr = setFnr)}>
<App Routes={Routes} key={'1'} />
</Provider>
</ModalProvider>,
appRoot,
if (fnr) {
settLocalStorage(LocalStorageElement.FNR, fnr);
}
this.root = createRoot(appRoot);
this.root.render(
<Provider key={fnr} fnr={fnr} setFnrRef={(setFnr) => (this.setFnr = setFnr)}>
<App Routes={Routes} key={'1'} />
</Provider>,
);
}

Expand Down

0 comments on commit d0f9864

Please sign in to comment.