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
|
import axios from 'axios'
import { useRef, useState } from 'react'
import registerApi from '../api/registerApi'
const useRegister = () => {
const [isLoading, setIsLoading] = useState(false)
const [alert, setAlert] = useState(null)
const [isValid, setIsValid] = useState(false)
const [tnd, setTnd] = useState(false)
const fullnameRef = useRef(null)
const emailRef = useRef(null)
const passwordRef = useRef(null)
const companyNameRef = useRef(null)
const inputVal = () => ({
fullname: fullnameRef.current.value,
email: emailRef.current.value,
password: passwordRef.current.value,
companyName: companyNameRef.current.value
})
const handleChangeInput = () => {
const { fullname, email, password } = inputVal()
const isValidInput = email && password && fullname
setIsValid(isValidInput)
}
const resetInput = () => {
fullnameRef.current.value = ''
emailRef.current.value = ''
passwordRef.current.value = ''
companyNameRef.current.value = ''
}
const handleSubmit = async (e) => {
e.preventDefault()
setAlert(null)
setIsLoading(true)
const { fullname, email, password, companyName } = inputVal()
const isRegistered = await registerApi({
name: fullname,
company: companyName,
email,
password
})
setIsLoading(false)
if (isRegistered.register) {
await axios.post(`${process.env.NEXT_PUBLIC_SELF_HOST}/api/activation-request`, { email })
setAlert({
children: 'Berhasil mendaftarkan akun anda, cek email untuk melakukan aktivasi akun',
type: 'success'
})
resetInput()
} else {
switch (isRegistered.reason) {
case 'EMAIL_USED':
setAlert({
children: 'Email telah digunakan',
type: 'info'
})
break
}
}
}
return {
handleChangeInput,
handleSubmit,
isLoading,
isValid,
alert,
companyNameRef,
fullnameRef,
emailRef,
passwordRef,
tnd,
setTnd
}
}
export default useRegister
|