Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix updating phone number #900

Merged
merged 3 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions packages/app/features/account/settings/personal-info.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import {
SubmitButton,
YStack,
Button,
isWeb,
Paragraph,
SubmitButton,
Text,
useToastController,
XStack,
Button,
Text,
Paragraph,
YStack,
} from '@my/ui'
import { SchemaForm } from 'app/utils/SchemaForm'
import { useSupabase } from 'app/utils/supabase/useSupabase'
import { useUser } from 'app/utils/useUser'
import { useRouter } from 'solito/router'
import type { z } from 'zod'
import { FormProvider, useForm } from 'react-hook-form'
import { VerifyCode } from 'app/features/auth/components/VerifyCode'
Expand All @@ -26,32 +25,39 @@ enum FormState {

export const PersonalInfoScreen = () => {
const { user, profile } = useUser()

const supabase = useSupabase()
const toast = useToastController()
const router = useRouter()
const form = useForm<z.infer<typeof AuthUserSchema>>() // Using react-hook-form
const { mutateAsync: mutateAuthAsync } = useAuthUserMutation()
const { mutateAsync: mutateProfileAsync } = useProfileMutation()
const [formState, setFormState] = useState<FormState>(FormState.PersonalInfoForm)
const [errorMessage, setErrorMessage] = useState<string | null>(null)

function handleSuccessAuthUpdate() {
async function handleSuccessCodeVerification() {
setFormState(FormState.PersonalInfoForm)
toast.show('Phone number updated')

if (!isWeb) {
await supabase.auth.refreshSession()
}
}

async function handleUserUpdate() {
const values = form.getValues()
setFormState(FormState.VerificationCode)
await mutateAuthAsync(values)
}

async function handleSubmit() {
setErrorMessage(null)
const values = form.getValues()
const shouldUpdateUser = user?.phone !== values.phone

try {
if (profile && profile.x_username !== values.xUsername) {
await mutateProfileAsync(values)
}
await mutateProfileAsync(values)

if (user && user.phone !== values.phone) {
await mutateAuthAsync(values)
handleSuccessAuthUpdate()
if (shouldUpdateUser) {
await handleUserUpdate()
}
} catch (error) {
console.error(error)
Expand All @@ -70,13 +76,7 @@ export const PersonalInfoScreen = () => {
<VerifyCode
type={'phone_change'}
phone={form.getValues().phone}
onSuccess={async () => {
toast.show('Phone number updated')
router.back()
if (!isWeb) {
await supabase.auth.refreshSession()
}
}}
onSuccess={handleSuccessCodeVerification}
/>
)

Expand Down
3 changes: 2 additions & 1 deletion packages/app/utils/useAuthUserMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export const useAuthUserMutation = () => {
}
},
async onSuccess() {
await queryClient.invalidateQueries({ queryKey: ['profile'] })
await queryClient.invalidateQueries({ queryKey: ['user'] })

toast.show('Check your phone', {
message: 'We sent you a confirmation code to your phone.',
})
Expand Down
17 changes: 16 additions & 1 deletion packages/app/utils/useUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,22 @@ import { useSupabase } from './supabase/useSupabase'

export const useUser = () => {
const { session, isLoading: isLoadingSession } = useSessionContext()
const user = session?.user

const { data: user } = useQuery({
queryKey: ['user'],
enabled: !isLoadingSession,
queryFn: async () => {
const { data, error } = await supabase.auth.getUser()

if (error) {
await supabase.auth.signOut()
throw new Error(error.message)
}

return data.user
},
})

const supabase = useSupabase()
const {
data: profile,
Expand Down
Loading