Skip to content

Commit cc7bf0c

Browse files
committed
fix: show unsaved changes leaves page
1 parent a6e7a8c commit cc7bf0c

File tree

6 files changed

+50
-10
lines changed

6 files changed

+50
-10
lines changed

ui/dashboard/src/@queries/notification-details.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ export const prefetchNotification = (
5050
});
5151
};
5252

53+
export const invalidateNotificationDetails = (
54+
queryClient: QueryClient,
55+
params: NotificationFetcherPayload
56+
) => {
57+
queryClient.invalidateQueries({
58+
queryKey: [NOTIFICATION_DETAILS_QUERY_KEY, params]
59+
});
60+
};
61+
5362
export const invalidateNotification = (queryClient: QueryClient) => {
5463
queryClient.invalidateQueries({
5564
queryKey: [NOTIFICATION_DETAILS_QUERY_KEY]

ui/dashboard/src/hooks/use-action-with-url.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const useActionWithURL = ({ idKey = '*', addPath, closeModalPath }: Props) => {
2020
}, [location.pathname]);
2121

2222
const isAdd = useMemo(() => lastPath === ID_NEW, [lastPath]);
23-
const isClone = useMemo(() => lastPath?.includes('clone'), [lastPath]);
23+
const isClone = useMemo(() => path?.includes('clone'), [path]);
2424
const isEdit = useMemo(
2525
() => id && path && !isAdd && !isClone,
2626
[path, isAdd, isClone, idKey]

ui/dashboard/src/pages/notifications/notification-modal/notification-create-update-modal/index.tsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
notificationUpdater
1212
} from '@api/notification';
1313
import { yupResolver } from '@hookform/resolvers/yup';
14+
import { invalidateNotificationDetails } from '@queries/notification-details';
1415
import { invalidateNotifications } from '@queries/notifications';
1516
import { useQueryClient } from '@tanstack/react-query';
1617
import { useAuth } from 'auth';
@@ -56,7 +57,9 @@ interface NotificationCreateUpdateModalProps {
5657
isOpen: boolean;
5758
isLoadingNotification: boolean;
5859
notification?: Notification;
59-
onClose: () => void;
60+
notificationEnvironmentId?: string;
61+
resetNotification: () => void;
62+
onClose: (isRefresh?: boolean) => void;
6063
}
6164

6265
export interface NotificationCreateUpdateForm {
@@ -91,6 +94,8 @@ const NotificationCreateUpdateModal = ({
9194
isOpen,
9295
isLoadingNotification,
9396
notification,
97+
notificationEnvironmentId,
98+
resetNotification,
9499
onClose
95100
}: NotificationCreateUpdateModalProps) => {
96101
const { notify, errorNotify } = useToast();
@@ -208,6 +213,10 @@ const NotificationCreateUpdateModal = ({
208213
})
209214
});
210215
invalidateNotifications(queryClient);
216+
invalidateNotificationDetails(queryClient, {
217+
id: notificationId as string,
218+
environmentId: notificationEnvironmentId as string
219+
});
211220
onClose();
212221
}
213222
} catch (error) {
@@ -217,12 +226,15 @@ const NotificationCreateUpdateModal = ({
217226
[notification, isEditNotification]
218227
);
219228

220-
useUnsavedLeavePage({ isShow: isDirty && !isSubmitting });
229+
useUnsavedLeavePage({
230+
isShow: isDirty && !isSubmitting,
231+
callBackCancel: resetNotification
232+
});
221233
return (
222234
<SlideModal
223235
title={t(isEditNotification ? 'update-notification' : 'new-notification')}
224236
isOpen={isOpen}
225-
onClose={onClose}
237+
onClose={() => onClose(false)}
226238
>
227239
{isLoadingNotification ? (
228240
<FormLoading />
@@ -521,7 +533,11 @@ const NotificationCreateUpdateModal = ({
521533
<div className="absolute left-0 bottom-0 bg-gray-50 w-full rounded-b-lg">
522534
<ButtonBar
523535
primaryButton={
524-
<Button type="button" variant="secondary" onClick={onClose}>
536+
<Button
537+
type="button"
538+
variant="secondary"
539+
onClick={() => onClose(false)}
540+
>
525541
{t(`cancel`)}
526542
</Button>
527543
}

ui/dashboard/src/pages/notifications/page-loader.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,13 @@ const PageLoader = () => {
9292
[commonPath]
9393
);
9494

95-
const handleOnCloseModal = useCallback(() => {
95+
const handleOnCloseModal = useCallback((isRefresh?: boolean) => {
96+
const isResetNotification = isRefresh ?? true;
9697
onCloseConfirmModal();
9798
setIsDeleteNotification(false);
98-
setSelectedNotification(undefined);
99+
if (isResetNotification) {
100+
setSelectedNotification(undefined);
101+
}
99102
onCloseActionModal();
100103
}, []);
101104

@@ -132,6 +135,11 @@ const PageLoader = () => {
132135
}
133136
}, [selectedNotification]);
134137

138+
const onHandleAddNew = useCallback(() => {
139+
setSelectedNotification(undefined);
140+
onOpenAddModal();
141+
}, []);
142+
135143
useEffect(() => {
136144
if (notificationCollection) {
137145
setSelectedNotification(notificationCollection.subscription);
@@ -149,7 +157,7 @@ const PageLoader = () => {
149157
<>
150158
<PageContent
151159
disabled={!editable}
152-
onAdd={onOpenAddModal}
160+
onAdd={onHandleAddNew}
153161
onHandleActions={onHandleActions}
154162
/>
155163

@@ -160,6 +168,8 @@ const PageLoader = () => {
160168
isOpen={!!isAdd || !!isEdit}
161169
isLoadingNotification={isLoadingNotification}
162170
notification={selectedNotification}
171+
notificationEnvironmentId={notificationEnvironmentId as string}
172+
resetNotification={() => setSelectedNotification(undefined)}
163173
onClose={handleOnCloseModal}
164174
/>
165175
)}

ui/dashboard/src/pages/organizations/organization-modal/organization-create-update-modal/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ const OrganizationCreateUpdateModal = ({
313313
<div className="absolute left-0 bottom-0 bg-gray-50 w-full rounded-b-lg">
314314
<ButtonBar
315315
primaryButton={
316-
<Button variant="secondary" onClick={onClose}>
316+
<Button type="button" variant="secondary" onClick={onClose}>
317317
{t(`cancel`)}
318318
</Button>
319319
}

ui/dashboard/src/pages/pushes/page-loader.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ const PageLoader = () => {
8080
[commonPath]
8181
);
8282

83+
const onHandleAddNew = useCallback(() => {
84+
setSelectedPush(undefined);
85+
onOpenAddModal();
86+
}, [commonPath]);
87+
8388
const handleOnCloseModal = useCallback((isRefresh?: boolean) => {
8489
const checkReset = isRefresh ?? true;
8590
onCloseActionModal();
@@ -141,7 +146,7 @@ const PageLoader = () => {
141146
<>
142147
<PageContent
143148
disabled={!editable}
144-
onAdd={onOpenAddModal}
149+
onAdd={onHandleAddNew}
145150
onHandleActions={onHandleActions}
146151
/>
147152
{(!!isAdd || !!isEdit) && (

0 commit comments

Comments
 (0)