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
16 changes: 13 additions & 3 deletions src/api/axios/custom-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import { getApiError } from '@baca/utils'
import Axios, { AxiosError, AxiosRequestConfig } from 'axios'
import i18n from 'i18next'
import qs from 'qs'
import { notify } from 'react-native-notificated'

import { injectTokenToRequest } from './interceptors'

export type ApiError = {
message: string
errors: {
error?: string
errors?: {
[key: string]: string[]
}
}
Expand All @@ -35,9 +36,18 @@ AXIOS_INSTANCE.interceptors.response.use(
return response
},
async (error: AxiosError<ApiError>) => {
const errorMessage = error?.response?.data?.message
const errorMessage = error?.response?.data?.error
const formErrors = error?.response?.data?.errors

if (formErrors) {
throw formErrors
}

// TODO: we should handle certain error type
if (errorMessage) {
notify('error', {
params: { title: 'ERROR', description: i18n.t('errors.something_went_wrong') },
})
//CONFIG: Add errors in getApiError
const api_error = getApiError(errorMessage)

Expand Down
15 changes: 12 additions & 3 deletions src/hooks/forms/useSignUpForm.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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'
Expand All @@ -20,14 +21,14 @@ const defaultValues: AuthRegisterLoginDto = {
export const useSignUpForm = () => {
const [error, setError] = useState('')
const [isSubmitting, setIsSubmitting] = useState(false)
const { mutate } = useAuthControllerRegister()

const { mutateAsync } = useAuthControllerRegister()
const { t } = useTranslation()

const {
control,
formState: { errors },
handleSubmit,
setError: setFormError,
setFocus,
} = useForm<AuthRegisterLoginDto>({
mode: 'onTouched',
Expand All @@ -39,13 +40,21 @@ export const useSignUpForm = () => {
setIsSubmitting(true)
setError('')

mutate({ data })
await mutateAsync({ data })
} 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)
Expand Down
9 changes: 0 additions & 9 deletions src/screens/SignUpScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ControlledField, KeyboardAwareScrollView } from '@baca/components'
import { REGEX } from '@baca/constants'
import { Button, Center, Spacer } from '@baca/design-system'
import { useScreenOptions, useSignUpForm, useTranslation } from '@baca/hooks'
import { useCallback, useEffect } from 'react'
Expand Down Expand Up @@ -67,10 +66,6 @@ export const SignUpScreen = () => {
placeholder={t('sign_up_screen.email_placeholder')}
rules={{
required: t('form.required'),
pattern: {
value: REGEX.EMAIL,
message: t('form.invalid_email_format'),
},
}}
/>
<ControlledField.Input
Expand All @@ -85,10 +80,6 @@ export const SignUpScreen = () => {
placeholder={t('sign_up_screen.password_placeholder')}
rules={{
required: t('form.required'),
pattern: {
value: REGEX.REGISTRATION_PASSWORD,
message: t('form.invalid_password_format'),
},
}}
type="password"
/>
Expand Down
21 changes: 21 additions & 0 deletions src/utils/handleFormErrors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
type BackendErrorResponseType<TField extends string> =
| {
[key in TField]: string
}
| string

type CallbackArgType<TField> = {
field: TField
description: string
}

type CallbackType<TField> = (arg: CallbackArgType<TField>) => void

export const handleFormError = <TField extends string>(
e: BackendErrorResponseType<TField>,
callback: CallbackType<TField>
) => {
Object.entries(e).forEach(([key, value]) => {
callback({ field: key as TField, description: value as string })
})
}