summaryrefslogtreecommitdiff
path: root/src-migrate/modules/register/components/Form.tsx
blob: 38e9c8108986a7c1d3a59935ed05b1edcfd37573 (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { ChangeEvent, useMemo, useEffect, useRef, useState } from 'react';
import { useMutation } from 'react-query';
import { useRegisterStore } from '../stores/useRegisterStore';
import { RegisterProps } from '~/types/auth';
import { registerUser } from '~/services/auth';
import TermCondition from './TermCondition';
import FormCaptcha from './FormCaptcha';
import { useRouter } from 'next/router';
import { UseToastOptions, useToast } from '@chakra-ui/react';
import Link from 'next/link';

interface FormProps {
  type: string;
  required: boolean;
  isBisnisRegist: boolean;
  chekValid: boolean;
  buttonSubmitClick: boolean;
}

const Form: React.FC<FormProps> = ({
  type,
  required,
  isBisnisRegist = false,
  chekValid,
  buttonSubmitClick,
}) => {
  const { form, isCheckedTNC, isValidCaptcha, errors, updateForm, validate } =
    useRegisterStore();
  const isFormValid = useMemo(() => Object.keys(errors).length === 0, [errors]);

  const router = useRouter();
  const toast = useToast();

  const emailRef = useRef<HTMLInputElement>(null);
  const nameRef = useRef<HTMLInputElement>(null);
  const passwordRef = useRef<HTMLInputElement>(null);
  const phoneRef = useRef<HTMLInputElement>(null);

  const handleInputChange = (event: ChangeEvent<HTMLInputElement>) => {
    const { name, value } = event.target;
    updateForm(name, value);
    validate();
  };
  useEffect(() => {
    const loadIndustries = async () => {
      if (!isFormValid) {
        const options: ScrollIntoViewOptions = {
          behavior: 'smooth',
          block: 'center',
        };

        if (errors.email_partner && emailRef.current) {
          emailRef.current.scrollIntoView(options);
          return;
        }
        if (errors.name && nameRef.current) {
          nameRef.current.scrollIntoView(options);
          return;
        }

        if (errors.password && passwordRef.current) {
          passwordRef.current.scrollIntoView(options);
          return;
        }

        if (errors.phone && phoneRef.current) {
          phoneRef.current.scrollIntoView(options);
          return;
        }
      }
    };
    loadIndustries();
  }, [buttonSubmitClick, chekValid]);

  return (
    <form className='mt-6 grid grid-cols-1 gap-y-4'>
      <div>
        <label htmlFor='name' className='text-black font-bold'>
          Nama Lengkap
        </label>

        <input
          type='text'
          id='name'
          name='name'
          className='form-input mt-3 transition-all duration-700'
          placeholder='Masukan nama lengkap anda'
          value={form.name}
          ref={nameRef}
          onChange={handleInputChange}
          aria-invalid={chekValid && !!errors.name}
        />

        {chekValid && !!errors.name && (
          <span className='form-msg-danger'>{errors.name}</span>
        )}
      </div>

      <div>
        <label htmlFor='email' className='text-black font-bold'>
          Alamat Email
        </label>

        <input
          type='text'
          id='email'
          name='email'
          className='form-input mt-3 transition-all duration-500'
          placeholder='Masukan alamat email anda'
          value={form.email}
          ref={emailRef}
          onChange={handleInputChange}
          autoComplete='username'
          aria-invalid={chekValid && !!errors.email}
        />

        {chekValid && !!errors.email && (
          <span className='form-msg-danger'>{errors.email}</span>
        )}
      </div>

      <div>
        <label htmlFor='password' className='text-black font-bold'>
          Kata Sandi
        </label>
        <input
          type='password'
          name='password'
          id='password'
          className='form-input mt-3 transition-all duration-500'
          placeholder='••••••••••••'
          value={form.password}
          ref={passwordRef}
          onChange={handleInputChange}
          autoComplete='current-password'
          aria-invalid={chekValid && !!errors.password}
        />

        {chekValid && !!errors.password && (
          <span className='form-msg-danger'>{errors.password}</span>
        )}
      </div>

      <div>
        <label htmlFor='phone' className='text-black font-bold'>
          No Handphone
        </label>

        <input
          type='tel'
          id='phone'
          name='phone'
          className='form-input mt-3 transition-all duration-500'
          placeholder='08xxxxxxxx'
          value={form.phone}
          ref={phoneRef}
          onChange={handleInputChange}
          aria-invalid={chekValid && !!errors.phone}
        />

        {chekValid && !!errors.phone && (
          <span className='form-msg-danger'>{errors.phone}</span>
        )}
      </div>
    </form>
  );
};

export default Form;