Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refaktorert Toast - Skal legge seg på midten og kunne krysses ut #2876

Merged
merged 4 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/frontend/Felles/Toast/Toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useEffect } from 'react';
import styled from 'styled-components';
import { useApp } from '../../App/context/AppContext';
import { EToast, toastTilTekst } from '../../App/typer/toast';
import { AlertError, AlertSuccess } from '../Visningskomponenter/Alerts';
import { AlertErrorMedLukkeknapp, AlertSuccessMedLukkeknapp } from '../Visningskomponenter/Alerts';

const ContainerTopMiddle = styled.div`
z-index: 9999;
Expand All @@ -14,7 +14,10 @@ const ContainerTopMiddle = styled.div`
`;

const ToastAlert: React.FC<{ toast: EToast }> = ({ toast }) => {
const Alert = toast === EToast.REDIRECT_ANNEN_RELASJON_FEILET ? AlertError : AlertSuccess;
const Alert =
toast === EToast.REDIRECT_ANNEN_RELASJON_FEILET
? AlertErrorMedLukkeknapp
: AlertSuccessMedLukkeknapp;

return (
<ContainerTopMiddle>
Expand All @@ -31,7 +34,7 @@ export const Toast: React.FC = () => {
settToast(undefined);
}, 5000);
return () => clearTimeout(timer);
}, [settToast]);
});

if (toast === null || toast === undefined) {
return null;
Expand Down
39 changes: 38 additions & 1 deletion src/frontend/Felles/Visningskomponenter/Alerts.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { forwardRef } from 'react';
import React, { forwardRef, ReactNode, useEffect, useState } from 'react';
import { Alert, AlertProps } from '@navikt/ds-react';

export const AlertError = forwardRef<HTMLDivElement, Omit<AlertProps, 'variant'>>((props, ref) => {
Expand All @@ -19,7 +19,44 @@ export const AlertWarning = forwardRef<HTMLDivElement, Omit<AlertProps, 'variant
return <Alert variant={'warning'} {...props} ref={ref} />;
}
);

export const AlertErrorMedLukkeknapp = forwardRef<HTMLDivElement, Omit<AlertProps, 'variant'>>(
(props) => {
return <AlertMedLukkeknapp variant={'error'} {...props} />;
}
);

export const AlertSuccessMedLukkeknapp = forwardRef<HTMLDivElement, Omit<AlertProps, 'variant'>>(
(props) => {
return <AlertMedLukkeknapp variant={'success'} {...props} />;
}
);

const AlertMedLukkeknapp = ({
children,
variant,
}: {
children?: ReactNode;
variant: AlertProps['variant'];
}) => {
const [skalVise, settSkalVise] = useState(true);

useEffect(() => {
settSkalVise(true);
}, [children]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Er det meningsbærende å ha en reactNode som avhengighet i en useEffect?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Var ikke klar over at det er dårlig praksis. Har gjort endringer


return (
skalVise && (
<Alert variant={variant} closeButton onClose={() => settSkalVise(false)}>
{children}
</Alert>
)
);
};

AlertError.displayName = 'AlertError';
AlertSuccess.displayName = 'AlertSuccess';
AlertInfo.displayName = 'AlertInfo';
AlertWarning.displayName = 'AlertWarning';
AlertErrorMedLukkeknapp.displayName = 'AlertErrorMedLukkeknapp';
AlertSuccessMedLukkeknapp.displayName = 'AlertSuccessMedLukkeknapp';