Skip to content

Commit 025c633

Browse files
committed
Simplify effect
1 parent 8538ec4 commit 025c633

File tree

2 files changed

+23
-34
lines changed

2 files changed

+23
-34
lines changed

site/src/api/queries/notifications.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,5 +151,10 @@ export const disableNotification = (
151151
});
152152
return result;
153153
},
154+
onSuccess: () => {
155+
queryClient.invalidateQueries(
156+
userNotificationPreferences(userId).queryKey,
157+
);
158+
},
154159
} satisfies UseMutationOptions<NotificationPreference[], unknown, string>;
155160
};

site/src/pages/UserSettingsPage/NotificationsPage/NotificationsPage.tsx

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -63,47 +63,31 @@ export const NotificationsPage: FC = () => {
6363
const updatePreferences = useMutation(
6464
updateUserNotificationPreferences(user.id, queryClient),
6565
);
66+
67+
// Notification emails contain a link to disable a specific notification
68+
// template. This functionality is achieved using the query string parameter
69+
// "disabled".
6670
const disableMutation = useMutation(
6771
disableNotification(user.id, queryClient),
6872
);
6973
const [searchParams] = useSearchParams();
7074
const disabledId = searchParams.get("disabled");
71-
7275
useEffect(() => {
73-
if (disabledId && templatesByGroup.isSuccess && templatesByGroup.data) {
74-
searchParams.delete("disabled");
75-
disableMutation
76-
.mutateAsync(disabledId)
77-
.then(() => {
78-
const allTemplates = Object.values(
79-
templatesByGroup.data ?? {},
80-
).flat();
81-
const template = allTemplates.find((t) => t.id === disabledId);
82-
83-
if (template) {
84-
displaySuccess(`${template.name} notification has been disabled`);
85-
} else {
86-
displaySuccess("Notification has been disabled");
87-
}
88-
queryClient.invalidateQueries(
89-
userNotificationPreferences(user.id).queryKey,
90-
);
91-
})
92-
.catch(() => {
93-
displayError(
94-
"An error occurred when attempting to disable the requested notification",
95-
);
96-
});
76+
if (!disabledId) {
77+
return;
9778
}
98-
}, [
99-
disabledId,
100-
templatesByGroup.isSuccess,
101-
templatesByGroup.data,
102-
disableMutation,
103-
queryClient,
104-
user.id,
105-
searchParams,
106-
]);
79+
searchParams.delete("disabled");
80+
disableMutation
81+
.mutateAsync(disabledId)
82+
.then(() => {
83+
displaySuccess("Notification has been disabled");
84+
})
85+
.catch(() => {
86+
displayError(
87+
"An error occurred when attempting to disable the requested notification",
88+
);
89+
});
90+
}, [searchParams.delete, disabledId, disableMutation]);
10791

10892
const ready =
10993
disabledPreferences.data && templatesByGroup.data && dispatchMethods.data;

0 commit comments

Comments
 (0)