Skip to content
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ GoogleService-Info.plist

scripts/doppler_variables.sh
.env
env.json
env.json

.vercel
1 change: 1 addition & 0 deletions src/hooks/forms/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './useSignInForm'
export * from './useSignUpForm'
export * from './useTestForm'
export * from './useUpdateProfileForm'
19 changes: 15 additions & 4 deletions src/hooks/forms/useSignInForm.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { useAuthControllerLogin } from '@baca/api/query/auth/auth'
import { AuthEmailLoginDto } from '@baca/api/types'
import { setToken } from '@baca/services'
import { isSignedInAtom } from '@baca/store/auth'
import { SignInFormValues } from '@baca/types/authForms'
import { hapticImpact } from '@baca/utils'
import { handleFormError } from '@baca/utils/handleFormErrors'
import { useSetAtom } from 'jotai'
import { useForm } from 'react-hook-form'
import { notify } from 'react-native-notificated'

type SignInFormValues = AuthEmailLoginDto & {
confirm: boolean
}

const defaultValues: SignInFormValues = {
// TODO: Reset this values when building production app
Expand All @@ -24,8 +28,9 @@ export const useSignInForm = () => {
const {
control,
formState: { errors },
setFocus,
handleSubmit,
setError: setFormError,
setFocus,
} = useForm<SignInFormValues>({
mode: 'onTouched',
defaultValues,
Expand All @@ -40,7 +45,13 @@ export const useSignInForm = () => {
// FIXME: add proper notification handling, generate some global config
{
onError: (e) => {
notify('error', { params: { title: 'ERROR', description: e?.message } })
handleFormError<keyof AuthEmailLoginDto>(
e as unknown as keyof AuthEmailLoginDto,
({ field, description }) => {
setFormError(field, { message: description })
}
)

hapticImpact()
},
onSuccess: async (response) => {
Expand Down
65 changes: 27 additions & 38 deletions src/hooks/forms/useSignUpForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { useAuthControllerRegister } from '@baca/api/query/auth/auth'
import { AuthRegisterLoginDto } from '@baca/api/types'
import { hapticImpact } from '@baca/utils'
import { handleFormError } from '@baca/utils/handleFormErrors'
import { isError } from '@tanstack/react-query'
import { useState } from 'react'
import { useForm } from 'react-hook-form'
import { useTranslation } from 'react-i18next'
import { notify } from 'react-native-notificated'
Expand All @@ -20,9 +18,7 @@ const defaultValues: AuthRegisterLoginDto = {
}

export const useSignUpForm = () => {
const [error, setError] = useState('')
const [isSubmitting, setIsSubmitting] = useState(false)
const { mutateAsync } = useAuthControllerRegister()
const { mutate: signUpMutation, isLoading } = useAuthControllerRegister()
const { t } = useTranslation()

const {
Expand All @@ -36,46 +32,39 @@ export const useSignUpForm = () => {
defaultValues,
})

const onSubmit = async (data: AuthRegisterLoginDto) => {
try {
setIsSubmitting(true)
setError('')

await mutateAsync({ data })
notify('success', {
params: {
style: { multiline: 100 },
title: 'SUCCESS',
description: t('sign_up_screen.created_new_account', { userEmail: data.email }),
const onSubmit = (data: AuthRegisterLoginDto) => {
signUpMutation(
{
data,
},
{
onSuccess: () => {
notify('success', {
params: {
style: { multiline: 100 },
title: 'SUCCESS',
description: t('toast.success.new_account_created', { userEmail: data.email }),
},
})
},
onError: (e) => {
handleFormError<keyof AuthRegisterLoginDto>(
e as unknown as keyof AuthRegisterLoginDto,
({ field, description }) => {
setFormError(field, { message: description })
}
)
hapticImpact()
},
})
} catch (e) {
if (isError(e)) {
setError(e.message)
} else {
setError(t('errors.something_went_wrong'))
}

handleFormError<keyof AuthRegisterLoginDto>(
e as keyof AuthRegisterLoginDto,
({ field, description }) => {
setFormError(field as keyof AuthRegisterLoginDto, { message: description })
}
)

hapticImpact()
} finally {
setIsSubmitting(false)
}
)
}

return {
control,
error,
errors,
isRegisterLoading: isSubmitting,
setFocus,
setIsSubmitting,
isSubmitting: isLoading,
register: handleSubmit(onSubmit),
setFocus,
}
}
74 changes: 74 additions & 0 deletions src/hooks/forms/useUpdateProfileForm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { useAuthControllerUpdate, useAuthControllerMe } from '@baca/api/query/auth/auth'
import { AuthUpdateDto } from '@baca/api/types'
import { hapticImpact } from '@baca/utils'
import { handleFormError } from '@baca/utils/handleFormErrors'
import { useMemo, useEffect } from 'react'
import { useForm } from 'react-hook-form'
import { useTranslation } from 'react-i18next'
import { notify } from 'react-native-notificated'

export const useUpdateProfileForm = () => {
const { data: userData } = useAuthControllerMe()
const { t } = useTranslation()
const { mutate: updateUserMutation, isLoading } = useAuthControllerUpdate()

const defaultValues: AuthUpdateDto = useMemo(
() => ({
email: userData?.email || '',
firstName: userData?.firstName || '',
lastName: userData?.lastName || '',
}),
[userData]
)

const {
control,
formState: { errors },
handleSubmit,
reset,
setError: setFormError,
setFocus,
} = useForm<AuthUpdateDto>({
mode: 'onTouched',
defaultValues,
})

useEffect(() => {
reset(defaultValues)
}, [reset, defaultValues])

const onSubmit = (data: AuthUpdateDto) => {
updateUserMutation(
{ data },
{
onSuccess: () => {
notify('success', {
params: {
style: { multiline: 100 },
title: 'SUCCESS',
description: t('toast.success.profile_updated'),
},
})
},
onError: (e) => {
handleFormError<keyof AuthUpdateDto>(
e as unknown as keyof AuthUpdateDto,
({ field, description }) => {
setFormError(field, { message: description })
}
)

hapticImpact()
},
}
)
}

return {
control,
errors,
isSubmitting: isLoading,
setFocus,
submit: handleSubmit(onSubmit),
}
}
Loading