import React, { useEffect, useRef, type FormEvent } from 'react' import './auth.scss' import { UserRole, UserStatus, type User } from '../../../types/user.type' import { Apis } from '../../../apis' import { message, Modal } from 'antd' import { FormUtil } from '../../../utils/form.util' import type { UserSignInDTO } from '../../../apis/core/user.api' import { useSelector } from 'react-redux' import type { StoreType } from '../../../stores' export default function Auth() { const containerRef = useRef(null) async function handleSignup(e: FormEvent) { e.preventDefault() try { let newUser = { id: String(Date.now()), userName: (e.target as any).userName.value, password: (e.target as any).password.value, role: UserRole.USER, displayName: (e.target as any).displayName.value, email: (e.target as any).email.value, phoneNumber: (e.target as any).phoneNumber.value, status: UserStatus.ACTIVE, banReason: "" } let newUserResult = (await Apis.user.signUp(newUser)) as User Modal.confirm({ title: "Thông báo", content: `Bạn đã đăng ký thành công với tên đăng nhập là: ${newUserResult.userName}, id của bạn là: ${newUserResult.id}`, onOk: () => { FormUtil.resetForm(e) containerRef.current.classList.remove("right-panel-active"); } }) } catch (err) { message.error(err.mes) } } async function handleSignin(e: FormEvent) { e.preventDefault() let loginData: UserSignInDTO = { emailOrUserName: (e.target as any).emailOrUserName.value, password: (e.target as any).password.value } console.log("loginData", loginData) try { let data = await Apis.user.signIn(loginData) localStorage.setItem("token", data) Modal.confirm({ title: `Chào mừng bạn đã quay trở lại`, content: ``, onOk: () => { window.location.href = "/" }, onCancel: () => { window.location.href = "/" } }) } catch (err) { message.error(err.message) } } const userStore = useSelector((store: StoreType) => { return store.user }) useEffect(() => { if (!userStore.loading && userStore.data) { window.location.href = "/" } }, [userStore.data, userStore.loading]) return ( <> { userStore.data ? <>Đã đăng nhập> : (
To keep connected with us please login with your personal info
Enter your personal details and start journey with us