Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/dashboard/src/data/auth-providers/delete-user-auth-provider-mutation.ts
2501 views
1
/**
2
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { useMutation, useQueryClient } from "@tanstack/react-query";
8
import { authProviderClient } from "../../service/public-api";
9
import { AuthProvider, DeleteAuthProviderRequest } from "@gitpod/public-api/lib/gitpod/v1/authprovider_pb";
10
import { getUserAuthProvidersQueryKey } from "./user-auth-providers-query";
11
import { useCurrentUser } from "../../user-context";
12
13
type DeleteAuthProviderArgs = {
14
providerId: string;
15
};
16
export const useDeleteUserAuthProviderMutation = () => {
17
const queryClient = useQueryClient();
18
const user = useCurrentUser();
19
20
return useMutation({
21
mutationFn: async ({ providerId }: DeleteAuthProviderArgs) => {
22
if (!user) {
23
throw new Error("No current user");
24
}
25
26
const response = await authProviderClient.deleteAuthProvider(
27
new DeleteAuthProviderRequest({
28
authProviderId: providerId,
29
}),
30
);
31
32
return response;
33
},
34
onSuccess: (_, { providerId }) => {
35
if (!user) {
36
throw new Error("No current user");
37
}
38
39
const queryKey = getUserAuthProvidersQueryKey(user.id);
40
queryClient.setQueryData<AuthProvider[]>(queryKey, (providers) => {
41
return providers?.filter((p) => p.id !== providerId);
42
});
43
44
queryClient.invalidateQueries({ queryKey });
45
},
46
});
47
};
48
49