connect-account-modal.tsx92 lines, 2.5 KB
| 1 | "use client"; |
| 2 | |
| 3 | import { useModalStore } from "@/store/zustand"; |
| 4 | import { useMutation } from "@tanstack/react-query"; |
| 5 | import { useState } from "react"; |
| 6 | import { toast } from "sonner"; |
| 7 | import { |
| 8 | Dialog, |
| 9 | DialogContent, |
| 10 | DialogDescription, |
| 11 | DialogHeader, |
| 12 | DialogTitle, |
| 13 | } from "../ui/dialog"; |
| 14 | import { Button } from "../ui/button"; |
| 15 | import { Loader2 } from "lucide-react"; |
| 16 | import { Checkbox } from "../ui/checkbox"; |
| 17 | import { Label } from "../ui/label"; |
| 18 | |
| 19 | function googleSignIn(): Promise<void> { |
| 20 | return new Promise((resolve) => { |
| 21 | window.location.href = `${process.env.NEXT_PUBLIC_API_URL}/auth/google`; |
| 22 | resolve(); |
| 23 | }); |
| 24 | } |
| 25 | |
| 26 | export function ConnectAccountModal() { |
| 27 | const [isAgreed, setIsAgreed] = useState(false); |
| 28 | const modalKey = "connectAccountModal"; |
| 29 | const { isOpen, closeModal } = useModalStore(); |
| 30 | |
| 31 | const mutation = useMutation({ |
| 32 | mutationFn: googleSignIn, |
| 33 | onSuccess: () => { |
| 34 | closeModal(modalKey); |
| 35 | }, |
| 36 | onError: (error: Error) => { |
| 37 | toast.error(error.message); |
| 38 | }, |
| 39 | }); |
| 40 | |
| 41 | const handleGoogleSignIn = async () => { |
| 42 | if (isAgreed) { |
| 43 | mutation.mutate(); |
| 44 | } else { |
| 45 | toast.error("Please agree to the terms and conditions"); |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | return ( |
| 50 | <Dialog |
| 51 | open={isOpen(modalKey)} |
| 52 | onOpenChange={() => closeModal(modalKey)} |
| 53 | key={modalKey} |
| 54 | > |
| 55 | <DialogContent> |
| 56 | <DialogHeader> |
| 57 | <DialogTitle>Connect Google Account</DialogTitle> |
| 58 | <DialogDescription> |
| 59 | Please connect your Google account to continue. |
| 60 | </DialogDescription> |
| 61 | </DialogHeader> |
| 62 | |
| 63 | <div className="grid gap-4 py-4"> |
| 64 | <Button |
| 65 | onClick={handleGoogleSignIn} |
| 66 | disabled={!isAgreed || mutation.isPending} |
| 67 | className="w-full" |
| 68 | > |
| 69 | {mutation.isPending ? ( |
| 70 | <Loader2 className="mr-2 size-4 animate-spin" /> |
| 71 | ) : ( |
| 72 | <>Sign in with Google</> |
| 73 | )} |
| 74 | </Button> |
| 75 | <div className="flex items-center space-x-2"> |
| 76 | <Checkbox |
| 77 | id="terms" |
| 78 | checked={isAgreed} |
| 79 | onCheckedChange={(checked) => setIsAgreed(checked as boolean)} |
| 80 | /> |
| 81 | <Label |
| 82 | htmlFor="terms" |
| 83 | className="text-sm text-gray-500 leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70" |
| 84 | > |
| 85 | I agree to the terms and conditions |
| 86 | </Label> |
| 87 | </div> |
| 88 | </div> |
| 89 | </DialogContent> |
| 90 | </Dialog> |
| 91 | ); |
| 92 | } |