-
Notifications
You must be signed in to change notification settings - Fork 253
Expand file tree
/
Copy pathdisconnect-oidc-provider-dialog.tsx
More file actions
118 lines (109 loc) · 3.59 KB
/
Copy pathdisconnect-oidc-provider-dialog.tsx
File metadata and controls
118 lines (109 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import {
AlertDialog,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from '@/components/ui/alert-dialog';
import { EnumStatusCode } from '@wundergraph/cosmo-connect/dist/common/common_pb';
import { useState } from 'react';
import { useIsAdmin } from '@/hooks/use-is-admin';
import { Button, buttonVariants } from '@/components/ui/button';
import { deleteOIDCProvider } from '@wundergraph/cosmo-connect/dist/platform/v1/platform-PlatformService_connectquery';
import { useMutation } from '@connectrpc/connect-query';
import { useToast } from '@/components/ui/use-toast';
export interface DisconnectOIDCProviderDialogProps {
isProviderConnected: boolean;
refetch(): Promise<unknown>;
}
export function DisconnectOIDCProviderDialog({ isProviderConnected, refetch }: DisconnectOIDCProviderDialogProps) {
const isAdmin = useIsAdmin();
const [open, setOpen] = useState(false);
const [isPending, setPending] = useState(false);
const { toast } = useToast();
const { mutate } = useMutation(deleteOIDCProvider);
const onDialogOpenChange = (open: boolean) => {
if (isPending && !open) {
return;
}
setOpen(isProviderConnected && open);
if (open) {
setPending(false);
}
};
const onSubmit = () => {
if (!isProviderConnected || !isAdmin || isPending) {
return;
}
setPending(true);
mutate(
{},
{
onSuccess(data) {
if (data.response?.code === EnumStatusCode.OK) {
refetch().finally(() => {
setOpen(false);
toast({
description: 'OIDC provider disconnected successfully.',
duration: 4000,
});
});
} else {
setPending(false);
toast({
description: data.response?.details || 'Could not disconnect the OIDC provider. Please try again.',
duration: 4000,
});
}
},
onError() {
setPending(false);
toast({
description: 'Could not disconnect the OIDC provider. Please try again.',
duration: 4000,
});
},
},
);
};
return (
<AlertDialog open={isProviderConnected && isAdmin && open} onOpenChange={onDialogOpenChange}>
{isProviderConnected && (
<AlertDialogTrigger asChild>
<Button className="md:ml-auto" variant="destructive">
Disconnect
</Button>
</AlertDialogTrigger>
)}
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Are you sure you want to disconnect the OIDC provider?</AlertDialogTitle>
<AlertDialogDescription className="flex flex-col gap-y-1" asChild>
<div>
<p>
All members who are connected to the OIDC provider will be logged out and downgraded to the viewer role.
</p>
<p>Reconnecting will result in a new login url.</p>
<p>This action cannot be undone.</p>
</div>
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel disabled={isPending}>Cancel</AlertDialogCancel>
<Button
className={buttonVariants({ variant: 'destructive' })}
type="button"
disabled={!isAdmin || isPending}
isLoading={isPending}
onClick={onSubmit}
>
Disconnect
</Button>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
);
}