|
@ -1,61 +1,58 @@ |
|
|
"use client"; |
|
|
"use client"; |
|
|
|
|
|
|
|
|
import React, { FC, useContext, useState } from "react"; |
|
|
|
|
|
import googleSvg from "@/images/Google.svg"; |
|
|
|
|
|
|
|
|
import React, { FC, useState, useEffect } from "react"; |
|
|
import Input from "@/shared/Input"; |
|
|
import Input from "@/shared/Input"; |
|
|
import ButtonPrimary from "@/shared/ButtonPrimary"; |
|
|
import ButtonPrimary from "@/shared/ButtonPrimary"; |
|
|
import Image from "next/image"; |
|
|
|
|
|
import Link from "next/link"; |
|
|
import Link from "next/link"; |
|
|
import axiosInstance from "@/components/api/axios"; |
|
|
import axiosInstance from "@/components/api/axios"; |
|
|
import { useRouter } from "next/navigation"; |
|
|
import { useRouter } from "next/navigation"; |
|
|
import { useUserContext } from "@/components/contexts/userContext"; |
|
|
import { useUserContext } from "@/components/contexts/userContext"; |
|
|
export interface PageLoginProps {} |
|
|
|
|
|
|
|
|
|
|
|
const PageLogin: FC<PageLoginProps> = ({}) => { |
|
|
|
|
|
|
|
|
export interface PageLoginProps {} |
|
|
|
|
|
|
|
|
|
|
|
const PageLogin: FC<PageLoginProps> = () => { |
|
|
|
|
|
const { user, setUser } = useUserContext(); |
|
|
const router = useRouter(); |
|
|
const router = useRouter(); |
|
|
let user = JSON.parse(localStorage.getItem("user")); |
|
|
|
|
|
if (user) { |
|
|
|
|
|
return router.replace("/") |
|
|
|
|
|
} |
|
|
|
|
|
const { setStatus } = useUserContext(); |
|
|
|
|
|
|
|
|
|
|
|
const [phoneNumber, setPhoneNumber] = useState(""); |
|
|
|
|
|
const [password, setPassword] = useState(""); |
|
|
|
|
|
const [countryCode, setCountryCode] = useState(); |
|
|
|
|
|
const [error, setError] = useState(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const [phoneNumber, setPhoneNumber] = useState<string>(""); |
|
|
|
|
|
const [password, setPassword] = useState<string>(""); |
|
|
|
|
|
const [countryCode, setCountryCode] = useState<string>(""); |
|
|
|
|
|
const [error, setError] = useState<string>(""); |
|
|
|
|
|
|
|
|
|
|
|
// Redirect to home if the user is already logged in
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
if (Object.keys(user).length) { |
|
|
|
|
|
router.replace("/"); |
|
|
|
|
|
} |
|
|
|
|
|
}, [user, router]); |
|
|
|
|
|
|
|
|
const countryCodeHandler = (e) => { |
|
|
|
|
|
|
|
|
const countryCodeHandler = (e: React.ChangeEvent<HTMLInputElement>) => { |
|
|
if (e.target.value.length <= 3) { |
|
|
if (e.target.value.length <= 3) { |
|
|
setCountryCode(e.target.value); |
|
|
setCountryCode(e.target.value); |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
const submitHandler = async (phoneNumber, password) => { |
|
|
|
|
|
|
|
|
const submitHandler = async () => { |
|
|
setError(""); |
|
|
setError(""); |
|
|
|
|
|
|
|
|
await axiosInstance |
|
|
|
|
|
.post(`/api/account/login/`, { |
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
const response = await axiosInstance.post(`/api/account/login/`, { |
|
|
phone_number: phoneNumber, |
|
|
phone_number: phoneNumber, |
|
|
password, |
|
|
password, |
|
|
}) |
|
|
|
|
|
.then((response) => { |
|
|
|
|
|
if (response.status === 201) { |
|
|
|
|
|
|
|
|
|
|
|
localStorage.setItem("user", JSON.stringify(response.data)); |
|
|
|
|
|
setStatus(true) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
if (response.status === 201) { |
|
|
|
|
|
setUser(response.data); |
|
|
} else { |
|
|
} else { |
|
|
setError("something went wrong"); |
|
|
|
|
|
|
|
|
setError("Something went wrong"); |
|
|
|
|
|
} |
|
|
|
|
|
} catch (error) { |
|
|
|
|
|
if (error instanceof Error) { |
|
|
|
|
|
setError(error.message); |
|
|
|
|
|
} else { |
|
|
|
|
|
setError("An unknown error occurred"); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
}) |
|
|
|
|
|
.catch((error) => { |
|
|
|
|
|
setError(error.message) |
|
|
|
|
|
}); |
|
|
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
return ( |
|
|
return ( |
|
@ -65,36 +62,23 @@ const PageLogin: FC<PageLoginProps> = ({}) => { |
|
|
Login |
|
|
Login |
|
|
</h2> |
|
|
</h2> |
|
|
<div className="max-w-md mx-auto space-y-6"> |
|
|
<div className="max-w-md mx-auto space-y-6"> |
|
|
{/* Email/Password Form */} |
|
|
|
|
|
<form className="grid grid-cols-1 gap-6" action="#" method="post"> |
|
|
|
|
|
|
|
|
<form className="grid grid-cols-1 gap-6" onSubmit={(e) => e.preventDefault()}> |
|
|
<label className="block"> |
|
|
<label className="block"> |
|
|
<span className="text-neutral-800 dark:text-neutral-200"> |
|
|
|
|
|
Phone Number |
|
|
|
|
|
</span> |
|
|
|
|
|
|
|
|
<span className="text-neutral-800 dark:text-neutral-200">Phone Number</span> |
|
|
<div className="flex items-center mt-1 rounded-2xl border border-neutral-200 bg-white dark:border-neutral-700 dark:bg-neutral-900 focus-within:ring-0"> |
|
|
<div className="flex items-center mt-1 rounded-2xl border border-neutral-200 bg-white dark:border-neutral-700 dark:bg-neutral-900 focus-within:ring-0"> |
|
|
{/* Plus Sign as Static Text */} |
|
|
|
|
|
<span className="px-2 mr-[-15px] text-neutral-800 dark:text-neutral-200"> |
|
|
|
|
|
+ |
|
|
|
|
|
</span> |
|
|
|
|
|
{/* Country Code Input */} |
|
|
|
|
|
|
|
|
<span className="px-2 mr-[-15px] text-neutral-800 dark:text-neutral-200">+</span> |
|
|
<input |
|
|
<input |
|
|
value={countryCode} |
|
|
|
|
|
onChange={(e) => { |
|
|
|
|
|
countryCodeHandler(e); |
|
|
|
|
|
}} |
|
|
|
|
|
|
|
|
value={countryCode || ""} |
|
|
|
|
|
onChange={countryCodeHandler} |
|
|
type="number" |
|
|
type="number" |
|
|
placeholder="98" |
|
|
placeholder="98" |
|
|
maxLength={3} |
|
|
maxLength={3} |
|
|
className="w-[50px] [appearance:textfield] [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none p-2 mr-[-10px] text-center border-none outline-none focus:ring-0 focus:border-none bg-transparent text-neutral-800 dark:text-neutral-200" |
|
|
className="w-[50px] [appearance:textfield] [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none p-2 mr-[-10px] text-center border-none outline-none focus:ring-0 focus:border-none bg-transparent text-neutral-800 dark:text-neutral-200" |
|
|
/> |
|
|
/> |
|
|
{/* Divider Line */} |
|
|
|
|
|
<span className="px-2 text-neutral-500">|</span> |
|
|
<span className="px-2 text-neutral-500">|</span> |
|
|
{/* Phone Number Input */} |
|
|
|
|
|
<input |
|
|
<input |
|
|
value={phoneNumber} |
|
|
value={phoneNumber} |
|
|
onChange={(e) => { |
|
|
|
|
|
setPhoneNumber(e.target.value); |
|
|
|
|
|
}} |
|
|
|
|
|
|
|
|
onChange={(e) => setPhoneNumber(e.target.value)} |
|
|
type="number" |
|
|
type="number" |
|
|
placeholder="26363687" |
|
|
placeholder="26363687" |
|
|
className="[appearance:textfield] [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none flex-1 p-2 border-none outline-none focus:ring-0 focus:border-none bg-transparent text-neutral-800 dark:text-neutral-200" |
|
|
className="[appearance:textfield] [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none flex-1 p-2 border-none outline-none focus:ring-0 focus:border-none bg-transparent text-neutral-800 dark:text-neutral-200" |
|
@ -104,10 +88,7 @@ const PageLogin: FC<PageLoginProps> = ({}) => { |
|
|
<label className="block"> |
|
|
<label className="block"> |
|
|
<span className="flex justify-between items-center text-neutral-800 dark:text-neutral-200"> |
|
|
<span className="flex justify-between items-center text-neutral-800 dark:text-neutral-200"> |
|
|
Password |
|
|
Password |
|
|
<Link |
|
|
|
|
|
href="/forgot-password" |
|
|
|
|
|
className="text-sm underline font-medium" |
|
|
|
|
|
> |
|
|
|
|
|
|
|
|
<Link href="/forgot-password" className="text-sm underline font-medium"> |
|
|
Forgot password? |
|
|
Forgot password? |
|
|
</Link> |
|
|
</Link> |
|
|
</span> |
|
|
</span> |
|
@ -115,23 +96,13 @@ const PageLogin: FC<PageLoginProps> = ({}) => { |
|
|
type="password" |
|
|
type="password" |
|
|
className="mt-1" |
|
|
className="mt-1" |
|
|
value={password} |
|
|
value={password} |
|
|
onChange={(e) => { |
|
|
|
|
|
setPassword(e.target.value); |
|
|
|
|
|
}} |
|
|
|
|
|
|
|
|
onChange={(e) => setPassword(e.target.value)} |
|
|
/> |
|
|
/> |
|
|
</label> |
|
|
</label> |
|
|
{error && <p className="text-red-500 text-xs">{error}</p>} |
|
|
{error && <p className="text-red-500 text-xs">{error}</p>} |
|
|
<ButtonPrimary |
|
|
|
|
|
onClick={(e) => { |
|
|
|
|
|
e.preventDefault(); |
|
|
|
|
|
submitHandler(phoneNumber, password); |
|
|
|
|
|
}} |
|
|
|
|
|
> |
|
|
|
|
|
Continue |
|
|
|
|
|
</ButtonPrimary> |
|
|
|
|
|
|
|
|
<ButtonPrimary onClick={submitHandler}>Continue</ButtonPrimary> |
|
|
</form> |
|
|
</form> |
|
|
|
|
|
|
|
|
{/* Sign Up Link */} |
|
|
|
|
|
<span className="block text-center text-neutral-700 dark:text-neutral-300"> |
|
|
<span className="block text-center text-neutral-700 dark:text-neutral-300"> |
|
|
<Link href="/signup" className="text-bronze font-semibold"> |
|
|
<Link href="/signup" className="text-bronze font-semibold"> |
|
|
Create an account |
|
|
Create an account |
|
|