Skip to content

chore(site): migrate a few services to react-query used in the DashboardProvider #9667

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

Merged
merged 21 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Use mutate async
  • Loading branch information
BrunoQuaresma committed Sep 13, 2023
commit a75c079a52f394fc832c5609cea752df9c74499c
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,9 @@ const LicensesSettingsPage: FC = () => {
const success = searchParams.get("success");
const [confettiOn, toggleConfettiOn] = useToggle(false);
const entitlementsQuery = useQuery(entitlements());
const refreshEntitlementsMutationOptions = refreshEntitlements(queryClient);
const refreshEntitlementsMutation = useMutation({
...refreshEntitlementsMutationOptions,
onSuccess: async () => {
displaySuccess("Successfully refreshed licenses");
await refreshEntitlementsMutationOptions.onSuccess();
},
onError: (error) => {
displayError(getErrorMessage(error, "Failed to refresh entitlements"));
},
});
const refreshEntitlementsMutation = useMutation(
refreshEntitlements(queryClient),
);

useEffect(() => {
if (entitlementsQuery.error) {
Expand Down Expand Up @@ -80,7 +72,14 @@ const LicensesSettingsPage: FC = () => {
licenses={licenses}
isRemovingLicense={isRemovingLicense}
removeLicense={(licenseId: number) => removeLicenseApi(licenseId)}
refreshEntitlements={refreshEntitlementsMutation.mutate}
refreshEntitlements={async () => {
try {
await refreshEntitlementsMutation.mutateAsync();
displaySuccess("Successfully removed license");
} catch (error) {
displayError(getErrorMessage(error, "Failed to remove license"));
}
}}
/>
</>
);
Expand Down
29 changes: 16 additions & 13 deletions site/src/pages/UserSettingsPage/SSHKeysPage/SSHKeysPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { ConfirmDialog } from "../../../components/Dialogs/ConfirmDialog/Confirm
import { Section } from "../../../components/SettingsLayout/Section";
import { SSHKeysPageView } from "./SSHKeysPageView";
import { regenerateUserSSHKey, userSSHKey } from "api/queries/sshKeys";
import { displaySuccess } from "components/GlobalSnackbar/utils";
import { displayError, displaySuccess } from "components/GlobalSnackbar/utils";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { getErrorMessage } from "api/errors";

export const Language = {
title: "SSH keys",
Expand All @@ -20,18 +21,9 @@ export const SSHKeysPage: FC<PropsWithChildren<unknown>> = () => {
useState(false);
const queryClient = useQueryClient();
const userSSHKeyQuery = useQuery(userSSHKey("me"));
const regenerateSSHKeyMutationOptions = regenerateUserSSHKey(
"me",
queryClient,
const regenerateSSHKeyMutation = useMutation(
regenerateUserSSHKey("me", queryClient),
);
const regenerateSSHKeyMutation = useMutation({
...regenerateSSHKeyMutationOptions,
onSuccess: (newKey) => {
regenerateSSHKeyMutationOptions.onSuccess(newKey);
displaySuccess("SSH Key regenerated successfully.");
setIsConfirmingRegeneration(false);
},
});

return (
<>
Expand All @@ -54,7 +46,18 @@ export const SSHKeysPage: FC<PropsWithChildren<unknown>> = () => {
confirmLoading={regenerateSSHKeyMutation.isLoading}
title={Language.regenerateDialogTitle}
confirmText={Language.confirmLabel}
onConfirm={regenerateSSHKeyMutation.mutate}
onConfirm={async () => {
try {
await regenerateSSHKeyMutation.mutateAsync();
displaySuccess("SSH Key regenerated successfully.");
} catch (error) {
displayError(
getErrorMessage(error, "Failed to regenerate SSH key"),
);
} finally {
setIsConfirmingRegeneration(false);
}
}}
onClose={() => {
setIsConfirmingRegeneration(false);
}}
Expand Down