summaryrefslogtreecommitdiff
path: root/src/lib/auth/hooks/useRegister.js
diff options
context:
space:
mode:
authorRafi Zadanly <zadanlyr@gmail.com>2023-03-20 14:33:21 +0700
committerRafi Zadanly <zadanlyr@gmail.com>2023-03-20 14:33:21 +0700
commit833488811b4164d7fbdce9bd70e171f06d62bf8d (patch)
treec781a3ced1f27b8f1c81ed5401fd758d79c1de1c /src/lib/auth/hooks/useRegister.js
parent87e7292941a251f09b5af39d9020896a3bfb0f97 (diff)
login and register
Diffstat (limited to 'src/lib/auth/hooks/useRegister.js')
-rw-r--r--src/lib/auth/hooks/useRegister.js80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/lib/auth/hooks/useRegister.js b/src/lib/auth/hooks/useRegister.js
new file mode 100644
index 00000000..7642a666
--- /dev/null
+++ b/src/lib/auth/hooks/useRegister.js
@@ -0,0 +1,80 @@
+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 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
+ }
+}
+
+export default useRegister