diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-02-17 17:07:50 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-02-17 17:07:50 +0700 |
| commit | f99e0aba70efad0deb907d8e27f09fc9f527c8a4 (patch) | |
| tree | f0ac96e4e736a1d385e32553f0e641ee27e11fd3 /src2/pages/login.js | |
| parent | 90e1edab9b6a8ccc09a49fed3addbec2cbc4e4c3 (diff) | |
Refactor
Diffstat (limited to 'src2/pages/login.js')
| -rw-r--r-- | src2/pages/login.js | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/src2/pages/login.js b/src2/pages/login.js new file mode 100644 index 00000000..e80de44e --- /dev/null +++ b/src2/pages/login.js @@ -0,0 +1,97 @@ +import axios from "axios"; +import Head from "next/head"; +import Image from "next/image"; +import Link from "@/components/elements/Link"; +import { useRouter } from "next/router"; +import { useEffect, useState } from "react"; +import Alert from "@/components/elements/Alert"; +import Layout from "@/components/layouts/Layout"; +import Spinner from "@/components/elements/Spinner"; +import { setAuth } from "@/core/utils/auth"; +import Logo from "@/images/logo.png"; + +export default function Login() { + const router = useRouter(); + const [email, setEmail] = useState(''); + const [password, setPassword] = useState(''); + const [isInputFulfilled, setIsInputFulfilled] = useState(false); + const [isLoading, setIsLoading] = useState(false); + const [alert, setAlert] = useState(); + + useEffect(() => { + setIsInputFulfilled(email && password); + }, [email, password]); + + const login = async (e) => { + e.preventDefault(); + setIsLoading(true); + let login = await axios.post(`${process.env.SELF_HOST}/api/login`, {email, password}); + if (login.data.is_auth) { + setAuth(login.data.user); + router.push('/'); + } else { + switch (login.data.reason) { + case 'NOT_FOUND': + setAlert({ + component: <>Email atau password tidak cocok</>, + type: 'info' + }); + break; + case 'NOT_ACTIVE': + setAlert({ + component: <>Email belum diaktivasi, <Link className="text-gray-900" href={`/activate?email=${email}`}>aktivasi sekarang</Link></>, + type: 'info' + }); + break; + } + setIsLoading(false); + } + } + + return ( + <> + <Head> + <title>Masuk - Indoteknik</title> + </Head> + <Layout className="max-w-lg mx-auto flex flex-col items-center px-4 pb-8"> + <Link href="/" className="mt-16"> + <Image src={Logo} alt="Logo Indoteknik" width={165} height={42} /> + </Link> + <h1 className="text-2xl mt-4 text-center">Mulai Belanja Sekarang</h1> + <h2 className="text-gray_r-11 font-normal mt-2 mb-4">Masuk ke akun kamu untuk belanja</h2> + {alert ? ( + <Alert className="text-center" type={alert.type}>{alert.component}</Alert> + ) : ''} + <form onSubmit={login} className="w-full"> + <label className="form-label mt-4 mb-2">Alamat Email</label> + <input + type="email" + className="form-input bg-gray_r-2" + placeholder="johndoe@gmail.com" + value={email} + onChange={(e) => setEmail(e.target.value)} + /> + <label className="form-label mt-4 mb-2">Kata Sandi</label> + <input + type="password" + className="form-input bg-gray_r-2" + placeholder="••••••••" + value={password} + onChange={(e) => setPassword(e.target.value)} + /> + <div className="flex justify-end mt-4 w-full"> + <Link href="/forgot-password">Lupa kata sandi</Link> + </div> + <button type="submit" disabled={!isInputFulfilled} className="btn-yellow font-semibold mt-4 w-full"> + {isLoading ? ( + <div className="flex justify-center items-center gap-x-2"> + <Spinner className="w-4 h-4 text-gray-600 fill-gray-900" /> <span>Loading...</span> + </div> + ) : 'Masuk'} + </button> + </form> + <p className="text-gray-700 mt-4">Belum punya akun Indoteknik? <Link href="/register">Daftar</Link></p> + </Layout> + </> + ) +}
\ No newline at end of file |
