Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/dashboard/src/data/oidc-clients/upsert-oidc-client-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 { oidcService } from "../../service/public-api";
9
import { getOIDCClientsQueryKey } from "./oidc-clients-query";
10
import {
11
CreateClientConfigResponse,
12
UpdateClientConfigResponse,
13
} from "@gitpod/public-api/lib/gitpod/experimental/v1/oidc_pb";
14
15
// TODO: find a better way to type this against the API
16
type UpsertOIDCClientMutationArgs =
17
| Parameters<typeof oidcService.updateClientConfig>[0]
18
| Parameters<typeof oidcService.createClientConfig>[0];
19
20
export const useUpsertOIDCClientMutation = () => {
21
const queryClient = useQueryClient();
22
23
return useMutation<CreateClientConfigResponse | UpdateClientConfigResponse, Error, UpsertOIDCClientMutationArgs>({
24
mutationFn: async ({ config = {} }: UpsertOIDCClientMutationArgs) => {
25
if ("id" in config) {
26
return await oidcService.updateClientConfig({
27
config,
28
});
29
} else {
30
return await oidcService.createClientConfig({
31
config,
32
});
33
}
34
},
35
onSuccess(resp, { config = {} }) {
36
if (!config || !config.organizationId) {
37
return;
38
}
39
40
queryClient.invalidateQueries({ queryKey: getOIDCClientsQueryKey(config.organizationId || "") });
41
},
42
});
43
};
44
45