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 3 commits
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
56 changes: 18 additions & 38 deletions src/frontend/Felles/Toast/Toast.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
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';

const ContainerTopRight = styled.div`
z-index: 9999;
position: fixed;
right: 2rem;
top: 4rem;
`;
import { AlertErrorMedLukkeknapp, AlertSuccessMedLukkeknapp } from '../Visningskomponenter/Alerts';

const ContainerTopMiddle = styled.div`
z-index: 9999;
Expand All @@ -22,6 +13,19 @@ const ContainerTopMiddle = styled.div`
transform: translate(-50%, 0%);
`;

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

return (
<ContainerTopMiddle>
<Alert>{toastTilTekst[toast]}</Alert>
</ContainerTopMiddle>
);
};

export const Toast: React.FC = () => {
const { toast, settToast } = useApp();

Expand All @@ -32,33 +36,9 @@ export const Toast: React.FC = () => {
return () => clearTimeout(timer);
});

switch (toast) {
case null:
case undefined:
return null;
case EToast.REDIRECT_ANNEN_RELASJON_FEILET:
return (
<ContainerTopMiddle>
<AlertError>{toastTilTekst[toast]}</AlertError>
</ContainerTopMiddle>
);
case EToast.INNGANGSVILKÅR_GJENBRUKT:
return (
<ContainerTopMiddle>
<AlertSuccess>{toastTilTekst[toast]}</AlertSuccess>
</ContainerTopMiddle>
);
case EToast.TILDEL_OPPGAVE_VELlYKKET:
return (
<ContainerTopMiddle>
<AlertSuccess>{toastTilTekst[toast]}</AlertSuccess>
</ContainerTopMiddle>
);
default:
return (
<ContainerTopRight>
<AlertSuccess>{toastTilTekst[toast]}</AlertSuccess>
</ContainerTopRight>
);
if (toast === null || toast === undefined) {
return null;
}

return <ToastAlert toast={toast} />;
};
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';