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 all 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
55 changes: 17 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 { AlertMedLukkeknapp } from '../Visningskomponenter/Alerts';

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

const ToastAlert: React.FC<{ toast: EToast }> = ({ toast }) => {
const variant = toast === EToast.REDIRECT_ANNEN_RELASJON_FEILET ? 'error' : 'success';

return (
<ContainerTopMiddle>
<AlertMedLukkeknapp variant={variant} keyProp={toast}>
{toastTilTekst[toast]}
</AlertMedLukkeknapp>
</ContainerTopMiddle>
);
};

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

Expand All @@ -32,33 +35,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} />;
};
29 changes: 28 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 @@ -14,12 +14,39 @@ export const AlertSuccess = forwardRef<HTMLDivElement, Omit<AlertProps, 'variant
export const AlertInfo = forwardRef<HTMLDivElement, Omit<AlertProps, 'variant'>>((props, ref) => {
return <Alert variant={'info'} {...props} ref={ref} />;
});

export const AlertWarning = forwardRef<HTMLDivElement, Omit<AlertProps, 'variant'>>(
(props, ref) => {
return <Alert variant={'warning'} {...props} ref={ref} />;
}
);

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

useEffect(() => {
settSkalVise(true);
}, [keyProp]);

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

AlertError.displayName = 'AlertError';
AlertSuccess.displayName = 'AlertSuccess';
AlertInfo.displayName = 'AlertInfo';
AlertWarning.displayName = 'AlertWarning';
AlertMedLukkeknapp.displayName = 'AlertMedLukkeknapp';