# All files / src/hooks useRegistrationForm.js

100% Statements 43/43
100% Branches 12/12
100% Functions 11/11
100% Lines 41/41

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
    94
    95
    96

    399x
    399x

    69x
    65x

    67x
    64x

    67x
    43x

    65x
    54x

    65x
    54x

    65x
    54x

    1x

    335x

    64x

    51x
    51x
    306x
    306x
    47x

    51x

    219x
    219x

    219x
    92x
    92x
    92x

    92x

    92x

    219x
    4x
    4x

    219x
    838x
    219x
    181x

    38x

    219x

    12x

     import { useState } from 'react';
    import {
      validateCodePostal,
      validateDateOfBirth,
      validateEmail,
      validateName,
      validatePrenom,
      validateVille
    } from '../module/module';
    import { EMPTY_FORM_VALUES } from '../constants/formFields';

    export function validateRegistrationField(name, value) {
      try {
        switch (name) {
          case 'nom':
            validateName(value);
            break;
          case 'prenom':
            validatePrenom(value);
            break;
          case 'email':
            validateEmail(value);
            break;
          case 'dateOfBirth':
            validateDateOfBirth(value);
            break;
          case 'ville':
            validateVille(value);
            break;
          case 'codePostal':
            validateCodePostal(value);
            break;
          default:
            break;
        }
        return '';
      } catch (error) {
        return error.message;
      }
    }

    export function validateAllRegistrationFields(values) {
      const errors = {};
      Object.keys(values).forEach((fieldName) => {
        const error = validateRegistrationField(fieldName, values[fieldName]);
        if (error) {
          errors[fieldName] = error;
        }
      });
      return errors;
    }

    export function useRegistrationForm(initialValues = EMPTY_FORM_VALUES) {
      const [formValues, setFormValues] = useState(initialValues);
      const [fieldErrors, setFieldErrors] = useState({});

      const onChange = (e) => {
        const { name, value } = e.target;
        setFormValues((prevValues) => {
          const nextValues = {
            ...prevValues,
            [name]: value
          };
          setFieldErrors((prevErrors) => ({
            ...prevErrors,
            [name]: validateRegistrationField(name, value)
          }));
          return nextValues;
        });
      };

      const resetForm = () => {
        setFormValues({ ...EMPTY_FORM_VALUES });
        setFieldErrors({});
      };

      const isFormValid = () => {
        const hasEmptyField = Object.values(formValues).some((value) => !value.trim());
        if (hasEmptyField) {
          return false;
        }
        return Object.keys(validateAllRegistrationFields(formValues)).length === 0;
      };

      return {
        formValues,
        fieldErrors,
        setFormValues,
        setFieldErrors,
        onChange,
        resetForm,
        validateAllFields: () => validateAllRegistrationFields(formValues),
        isSubmitDisabled: !isFormValid()
      };
    }