diff --git a/src/pages/auth/Signup.jsx b/src/pages/auth/Signup.jsx index 42ecbac..3ecdd15 100644 --- a/src/pages/auth/Signup.jsx +++ b/src/pages/auth/Signup.jsx @@ -1,5 +1,5 @@ import React, { useState } from 'react'; -import { Flex, Input, Form, Button, Card, Space, Image, Row, Col, Typography } from 'antd'; +import { Flex, Input, Form, Button, Card, Space, Image, Row, Col } from 'antd'; import { useNavigate } from 'react-router-dom'; import sypiu_ggcp from 'assets/sypiu_ggcp.jpg'; import logo from 'assets/freepik/LOGOPIU.png'; @@ -7,179 +7,192 @@ import { register } from '../../api/auth'; import { NotifOk, NotifAlert } from '../../components/Global/ToastNotif'; const SignUp = () => { - const [loading, setLoading] = useState(false); - const navigate = useNavigate(); + const [loading, setLoading] = useState(false); + const [form] = Form.useForm(); + const navigate = useNavigate(); - const moveToSignin = () => { - navigate('/signin'); - }; + const [isRegistered, setIsRegistered] = useState(false); - const handleSignUp = async (values) => { - const { fullname, username, email, phone, password, confirmPassword } = values; + const moveToSignin = () => { + navigate('/signin'); + }; - if (password !== confirmPassword) { - NotifAlert({ - icon: 'error', - title: 'Password Tidak Sama', - message: 'Password dan confirm password harus sama', - }); - return; - } + const handleSignUp = async (values) => { + const { fullname, username, email, phone, password, confirmPassword } = values; - const phoneRegex = /^(?:\+62|62|0)8[1-9][0-9]{6,11}$/; - if (!phoneRegex.test(phone)) { - NotifAlert({ - icon: 'error', - title: 'Format Telepon Salah', - message: 'Nomor telepon tidak valid (harus nomor Indonesia)', - }); - return; - } + // Validasi confirm password + if (password !== confirmPassword) { + NotifAlert({ + icon: 'error', + title: 'Password Tidak Sama', + message: 'Password dan confirm password harus sama', + }); + form.resetFields(['password', 'confirmPassword']); + return; + } - const passwordErrors = []; - if (password.length < 8) passwordErrors.push('Minimal 8 karakter'); - if (!/[A-Z]/.test(password)) passwordErrors.push('Harus ada huruf kapital'); - if (!/[0-9]/.test(password)) passwordErrors.push('Harus ada angka'); - if (!/[!@#$%^&*(),.?":{}|<>]/.test(password)) passwordErrors.push('Harus ada karakter spesial'); - if (passwordErrors.length) { - NotifAlert({ - icon: 'error', - title: 'Password Tidak Valid', - message: passwordErrors.join(', '), - }); - return; - } + // Validasi nomor telepon Indonesia + const phoneRegex = /^(?:\+62|62|0)8[1-9][0-9]{6,11}$/; + if (!phoneRegex.test(phone)) { + NotifAlert({ + icon: 'error', + title: 'Format Telepon Salah', + message: 'Nomor telepon tidak valid (harus nomor Indonesia)', + }); + form.resetFields(['phone']); + return; + } - setLoading(true); - try { - const res = await register({ fullname, username, email, phone, password }); - // const user = res?.data?.user; - // // const tokens = res?.data?.tokens; + // Validasi password kompleks + const passwordErrors = []; + if (password.length < 8) passwordErrors.push('Minimal 8 karakter'); + if (!/[A-Z]/.test(password)) passwordErrors.push('Harus ada huruf kapital'); + if (!/[0-9]/.test(password)) passwordErrors.push('Harus ada angka'); + if (!/[!@#$%^&*(),.?":{}|<>]/.test(password)) + passwordErrors.push('Harus ada karakter spesial'); + if (passwordErrors.length) { + NotifAlert({ + icon: 'error', + title: 'Password Tidak Valid', + message: passwordErrors.join(', '), + }); + form.resetFields(['password', 'confirmPassword']); + return; + } + setLoading(true); + try { + const res = await register({ fullname, username, email, phone, password }); - NotifOk({ - icon: 'success', - title: 'Registrasi Berhasil', - message: res?.data?.message || 'Berhasil menambahkan user.', - }); + NotifOk({ + icon: 'success', + title: 'Registrasi Berhasil', + message: res?.data?.message || 'Berhasil menambahkan user.', + }); + form.resetFields(); + setIsRegistered(true); + // navigate('/signin'); + } catch (err) { + console.error('Register error:', err); + const errorMessage = err?.response?.data?.message || err.message || 'Terjadi kesalahan'; - // if (user) { - // // localStorage.setItem('token', tokens.accessToken); - // // localStorage.setItem('refreshToken', tokens.refreshToken); - // // localStorage.setItem('user', JSON.stringify(user)); + NotifAlert({ + icon: 'error', + title: 'Registrasi Gagal', + message: errorMessage || 'Terjadi kesalahan', + }); + if (errorMessage.toLowerCase().includes('already')) { + form.resetFields(); + } + } finally { + setLoading(false); + } + }; - // NotifOk({ - // icon: 'success', - // title: 'Registrasi Berhasil', - // message: res?.data?.message || 'Selamat datang! Anda akan diarahkan ke dashboard.', - // }); - - // navigate('/signin'); - // } else { - // NotifAlert({ - // icon: 'error', - // title: 'Registrasi Gagal', - // message: res?.data?.message || 'Terjadi kesalahan', - // }); - // } - } catch (err) { - console.error('Register error:', err); - NotifAlert({ - icon: 'error', - title: 'Registrasi Gagal', - message: err?.response?.data?.message || err.message || 'Terjadi kesalahan', - }); - } finally { - setLoading(false); - } - }; - - return ( - - - {/* Logo di dalam Card */} - logo - -

Registration

- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - -
-
- ); + + logo + +

Registration

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + +
+ + ); }; export default SignUp;