From e4ac8e33b9f05ad4863cae6d9b7e6cdca1788e6a Mon Sep 17 00:00:00 2001 From: gudnuf Date: Fri, 13 Jun 2025 12:26:42 -0700 Subject: [PATCH] add google option to upgrade guest --- .../settings/profile/edit-profile.tsx | 4 +- ...grade-guest-form.tsx => upgrade-guest.tsx} | 104 +++++++++++++++--- 2 files changed, 93 insertions(+), 15 deletions(-) rename app/features/settings/profile/{upgrade-guest-form.tsx => upgrade-guest.tsx} (58%) diff --git a/app/features/settings/profile/edit-profile.tsx b/app/features/settings/profile/edit-profile.tsx index 54d6b9fad..bded2f24c 100644 --- a/app/features/settings/profile/edit-profile.tsx +++ b/app/features/settings/profile/edit-profile.tsx @@ -1,6 +1,6 @@ import { PageContent } from '~/components/page'; import { Separator } from '~/components/ui/separator'; -import { UpgradeGuestForm } from '~/features/settings/profile/upgrade-guest-form'; +import { UpgradeGuest } from '~/features/settings/profile/upgrade-guest'; import { SettingsViewHeader } from '~/features/settings/ui/settings-view-header'; import { useUser } from '~/features/user/user-hooks'; import EditableUsername from './editable-username'; @@ -23,7 +23,7 @@ export default function EditProfile() { {isGuest && ( <> - + )} diff --git a/app/features/settings/profile/upgrade-guest-form.tsx b/app/features/settings/profile/upgrade-guest.tsx similarity index 58% rename from app/features/settings/profile/upgrade-guest-form.tsx rename to app/features/settings/profile/upgrade-guest.tsx index ed69cf40f..2d22a1477 100644 --- a/app/features/settings/profile/upgrade-guest-form.tsx +++ b/app/features/settings/profile/upgrade-guest.tsx @@ -1,16 +1,56 @@ +import { useState } from 'react'; import { type SubmitHandler, useForm } from 'react-hook-form'; import { Button } from '~/components/ui/button'; import { Input } from '~/components/ui/input'; import { Label } from '~/components/ui/label'; +import { getErrorMessage } from '~/features/shared/error'; +import { useAuthActions } from '~/features/user/auth'; import { useUpgradeGuestToFullAccount } from '~/features/user/user-hooks'; import { useToast } from '~/hooks/use-toast'; import { buildEmailValidator } from '~/lib/validation'; type FormValues = { email: string; password: string; confirmPassword: string }; +type UpgradeOption = 'email' | 'google'; + const validateEmail = buildEmailValidator('Invalid email'); -export function UpgradeGuestForm() { +function UpgradeOptions({ + onSelect, +}: { onSelect: (option: UpgradeOption) => Promise }) { + const [submitting, setSubmitting] = useState(null); + + const handleSelect = async (option: UpgradeOption) => { + if (submitting) return; + + try { + setSubmitting(option); + await onSelect(option); + } finally { + setSubmitting(null); + } + }; + + return ( +
+

Backup and Sync

+ + +
+ ); +} + +function UpgradeWithEmailForm({ onBack }: { onBack: () => void }) { const { toast } = useToast(); const upgradeGuestToFullAccount = useUpgradeGuestToFullAccount(); @@ -41,15 +81,9 @@ export function UpgradeGuestForm() { }; return ( -
-
-

Upgrade

-

- Enter your email and password to backup your account and sync across - devices. -

-
-
+
+

Backup and Sync

+
)}
- +
+ + +
); } + +type UpgradeStep = 'pick-option' | 'upgrade-with-email'; + +export function UpgradeGuest() { + const [step, setStep] = useState('pick-option'); + const { initiateGoogleAuth } = useAuthActions(); + const { toast } = useToast(); + + const handleUpgradeWithGoogle = async () => { + try { + const response = await initiateGoogleAuth(); + console.debug('Initiate google upgrade response: ', response); + window.location.href = response.authUrl; + } catch (error) { + console.error('Failed to initiate google upgrade', { cause: error }); + toast({ + variant: 'destructive', + title: 'Error! Google upgrade failed', + description: getErrorMessage(error), + }); + } + }; + + if (step === 'pick-option') { + return ( + { + if (option === 'email') { + setStep('upgrade-with-email'); + } else if (option === 'google') { + await handleUpgradeWithGoogle(); + } + }} + /> + ); + } + + return setStep('pick-option')} />; +}