All files / src/screens/authentificationSection/subscription useSubscriptionController.ts

47.61% Statements 10/21
0% Branches 0/8
50% Functions 1/2
47.61% Lines 10/21

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 81 82 83 84 85 86 87 88 89 90 91 92 93                                                                                    1x 1x 1x 1x 1x 1x 1x 1x 1x                                               1x                                    
/**
 * @fileoverview Subscription screen controller.
 * @module useSubscriptionController
 * @description Controller for the Subscription screen.
 * @requires react react
 */
 
import { useState } from 'react'
import useRegister from '@helpers/httpClient/queries/auth/useRegister'
 
interface SubscriptionController {
  email: string
  setEmail: (email: string) => void
  username: string
  setUsername: (username: string) => void
  password: string
  setPassword: (password: string) => void
  passwordConfirmation: string
  setPasswordConfirmation: (passwordConfirmation: string) => void
  showPassword: boolean
  setShowPassword: (showPassword: boolean) => void
  showPasswordConfirmation: boolean
  setShowPasswordConfirmation: (showPasswordConfirmation: boolean) => void
  subscribe: () => void
  error: string | undefined
  isLoading: boolean
}
 
interface useSubscriptionControllerProps {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  navigation: any
}
 
/**
 * @function useSubscriptionController
 * @description Controller for the Subscription screen.
 * @param navigation Object containing the navigation prop.
 * @returns {SubscriptionController} Object containing the email, setEmail, password, setPassword, passwordConfirmation, setPasswordConfirmation, showPassword, setShowPassword, showPasswordConfirmation, setShowPasswordConfirmation, subscribe, and error.
 */
export default function useSubscriptionController({
  navigation,
}: useSubscriptionControllerProps): SubscriptionController {
  const [email, setEmail] = useState<string>('')
  const [username, setUsername] = useState<string>('')
  const [password, setPassword] = useState<string>('')
  const [passwordConfirmation, setPasswordConfirmation] = useState<string>('')
  const [showPassword, setShowPassword] = useState<boolean>(false)
  const [showPasswordConfirmation, setShowPasswordConfirmation] = useState<boolean>(false)
  const [error, setError] = useState<string | undefined>(undefined)
  const emailRegex = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/
  const regitrationMutation = useRegister({ formUsername: username, formEmail: email, password, navigation, setError })
 
  /**
   * @function subscribeUser
   * @description Send the user's email and password to the server to subscribe them to the app.
   * @returns {Promise<void>} Promise of void
   */
  async function subscribeUser(): Promise<void> {
    setError(undefined)
    if (emailRegex.test(email) === false) {
      setError('Veuillez rentrer un email valide')
      return
    }
    if (!__DEV__ && password.length < 8) {
      setError('Mot de passe trop court')
      return
    }
    if (password !== passwordConfirmation) {
      setError('Mots de passe différents')
      return
    }
    regitrationMutation.mutate()
  }
 
  return {
    email,
    setEmail,
    username,
    setUsername,
    password,
    setPassword,
    passwordConfirmation,
    setPasswordConfirmation,
    showPassword,
    setShowPassword,
    showPasswordConfirmation,
    setShowPasswordConfirmation,
    subscribe: subscribeUser,
    error,
    isLoading: regitrationMutation.isPending,
  }
}