Skip to content

Commit

Permalink
fix: toasts in dialogs
Browse files Browse the repository at this point in the history
  • Loading branch information
OliwiaGowor committed Dec 16, 2024
1 parent 0c6d5ab commit b90c740
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ResourceForm } from 'shared/ResourceForm';
import { ComboboxInput } from 'shared/ResourceForm/inputs';
import { CopiableText } from 'shared/components/CopiableText/CopiableText';
import { Editor } from 'shared/components/MonacoEditorESM/Editor';
import { useRef } from 'react';

const expirationSecondsOptions = [
{
Expand Down Expand Up @@ -71,14 +72,20 @@ export function TokenRequestModal({
}: TokenRequestModalProps) {
const { t } = useTranslation();
const downloadKubeconfig = useDownloadKubeconfigWithToken();
const modalRef = useRef(null);

const {
kubeconfigYaml,
token,
generateTokenRequest,
tokenRequest,
setTokenRequest,
} = useGenerateTokenRequest(isModalOpen, namespace, serviceAccountName);
} = useGenerateTokenRequest(
isModalOpen,
namespace,
serviceAccountName,
modalRef,
);

const isExpirationSecondsValueANumber = () =>
!Number(tokenRequest.spec.expirationSeconds);
Expand All @@ -100,6 +107,7 @@ export function TokenRequestModal({
open={isModalOpen}
onClose={handleCloseModal}
headerText={t('service-accounts.token-request.generate')}
ref={modalRef}
footer={
<Bar
design="Footer"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useTranslation } from 'react-i18next';
import { useNotification } from 'shared/contexts/NotificationContext';
import { useCreateKubeconfig } from 'hooks/useCreateKubeconfig';
import { useState, useEffect, useMemo } from 'react';
import { useState, useEffect, useMemo, RefObject } from 'react';
import { usePost } from 'shared/hooks/BackendAPI/usePost';
import jsyaml from 'js-yaml';

Expand All @@ -19,6 +19,7 @@ export const useGenerateTokenRequest = (
generate: boolean,
namespace: string,
serviceAccountName: string,
modalRef?: RefObject<HTMLElement>,
) => {
const { t } = useTranslation();
const post = usePost();
Expand All @@ -44,6 +45,7 @@ export const useGenerateTokenRequest = (
notifyToast(
{
content: t('service-accounts.token-request.notification.success'),
parentContainer: generate ? modalRef?.current : undefined,
},
3000,
);
Expand Down
1 change: 1 addition & 0 deletions src/shared/contexts/ErrorModal/ErrorModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import './ErrorModal.scss';

export type ToastProps = {
content: React.ReactNode;
parentContainer?: HTMLElement | null;
};

type CloseFn = () => void;
Expand Down
30 changes: 26 additions & 4 deletions src/shared/contexts/NotificationContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const NotificationProvider = ({
const methods = {
notifySuccess: function(notificationProps: ToastProps) {
setToastProps(notificationProps);
if (toast.current) {
if (toast.current && !toastProps?.parentContainer) {
toast.current.open = true;
}
},
Expand All @@ -58,9 +58,31 @@ export const NotificationProvider = ({
...methods,
}}
>
<Toast ref={toast} duration={defaultVisibilityTime} style={{ zIndex: 1 }}>
{toastProps?.content}
</Toast>
{toastProps?.parentContainer &&
createPortal(
<Toast
open={!!toastProps}
ref={toast}
duration={defaultVisibilityTime}
style={{ zIndex: 1 }}
onClose={e => {
setToastProps(null);
e.stopPropagation();
}}
>
{toastProps?.content}
</Toast>,
toastProps.parentContainer,
)}
{!toastProps?.parentContainer && (
<Toast
ref={toast}
duration={defaultVisibilityTime}
style={{ zIndex: 1 }}
>
{toastProps?.content}
</Toast>
)}
{errorProps &&
createPortal(<ErrorModal {...errorProps} />, document.body)}
{children}
Expand Down

0 comments on commit b90c740

Please sign in to comment.