Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | 1x 1x 1x | /**
* @fileoverview useRegister hook to register a user
* @module useRegister
* @requires react
* @requires @tanstack/react-query
*/
import { useContext } from 'react'
import { useMutation } from '@tanstack/react-query'
import { Header } from '@global/types/httpClient/Header'
import { AccountContext } from '@global/contexts/AccountProvider'
import RegisterJSON from '@global/types/httpClient/auth/Registration'
import { subscribe } from '@helpers/httpClient/queries/auth/auth'
interface RegisterProps {
url?: string
formUsername: string
formEmail: string
password: string
// eslint-disable-next-line @typescript-eslint/no-explicit-any
navigation: any
headers?: Header
setError: (error: string) => void
}
type RegisterUserProps = {
username: string
email: string
status: number
message: string
}
/**
* @function useRegister Handles the register mutation
* @param props The username, email, password, navigation, and setError function
* @param props.formUsername The user's username
* @param props.formEmail The user's email
* @param props.password The user's password
* @param props.navigation The navigation object
* @param props.setError The function to set the error
* @returns The mutation object
*/
export default function useRegister({ formEmail, formUsername, password, navigation, setError }: RegisterProps) {
const { account, setAccount } = useContext(AccountContext)
function registerUser({ username, email, status, message }: RegisterUserProps) {
if (status === 201) {
setAccount({
...account,
email,
username,
})
navigation.navigate('VerifyEmail')
} else {
setError(message)
}
}
const mutation = useMutation<RegisterJSON>({
mutationFn: () => subscribe({ email: formEmail, username: formUsername, password }),
onSuccess: data => {
try {
registerUser({
username: data.json.username,
email: data.json.email,
status: data.status,
message: data.message,
})
} catch (error) {
// @ts-expect-error - error is a string
setError(error.message)
}
},
onError: error => {
setError(error.message)
},
})
return mutation
}
|